diff --git a/.gitignore b/.gitignore index 8dead59..347140b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ node_modules dist -entry-asar/*.js* -entry-asar/*.ts +entry-asar/cjs/*.?js* +entry-asar/cjs/*.d.?ts +entry-asar/esm/*.?js* +entry-asar/esm/*.d.?ts *.app test/fixtures/apps coverage \ No newline at end of file diff --git a/entry-asar/has-asar.ts b/entry-asar/cjs/has-asar.ts similarity index 100% rename from entry-asar/has-asar.ts rename to entry-asar/cjs/has-asar.ts diff --git a/entry-asar/no-asar.ts b/entry-asar/cjs/no-asar.ts similarity index 100% rename from entry-asar/no-asar.ts rename to entry-asar/cjs/no-asar.ts diff --git a/entry-asar/cjs/tsconfig.json b/entry-asar/cjs/tsconfig.json new file mode 100644 index 0000000..ea4d52f --- /dev/null +++ b/entry-asar/cjs/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": ".", + }, + "include": [ + ".", + "../ambient.d.ts" + ], + "exclude": [] +} diff --git a/entry-asar/esm/has-asar.mts b/entry-asar/esm/has-asar.mts new file mode 100644 index 0000000..3bee10f --- /dev/null +++ b/entry-asar/esm/has-asar.mts @@ -0,0 +1,28 @@ +import { app } from 'electron'; +import { createRequire } from 'node:module'; +import path from 'node:path'; + +if (process.arch === 'arm64') { + await setPaths('arm64'); +} else { + await setPaths('x64'); +} + +async function setPaths(platform: string) { + // This should return the full path, ending in something like + // Notion.app/Contents/Resources/app.asar + const appPath = app.getAppPath(); + const asarFile = `app-${platform}.asar`; + + // Maybe we'll handle this in Electron one day + if (path.basename(appPath) === 'app.asar') { + const platformAppPath = path.join(path.dirname(appPath), asarFile); + + // This is an undocumented API. It exists. + app.setAppPath(platformAppPath); + } + + const require = createRequire(import.meta.url); + process._archPath = require.resolve(`../${asarFile}`); + await import(process._archPath); +} diff --git a/entry-asar/esm/tsconfig.json b/entry-asar/esm/tsconfig.json new file mode 100644 index 0000000..00475c1 --- /dev/null +++ b/entry-asar/esm/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.esm.json", + "compilerOptions": { + "module": "ESNext", + "target":"ESNext", + "outDir": ".", + }, + "include": [ + ".", + "../ambient.d.ts" + ], + "exclude": [] +} diff --git a/package.json b/package.json index 03b8ef2..e3ec4f1 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ ], "author": "Samuel Attard", "scripts": { - "build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && tsc -p tsconfig.entry-asar.json", + "build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && tsc -p tsconfig.entry-asar.json && tsc -p tsconfig.entry-asar.esm.json", "lint": "prettier --check \"{src,entry-asar,test}/**/*.ts\" \"*.ts\"", "prettier:write": "prettier --write \"{src,entry-asar,test}/**/*.ts\" \"*.ts\"", "prepublishOnly": "npm run build", diff --git a/src/index.ts b/src/index.ts index a0bd4f7..feb537a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -290,10 +290,6 @@ export const makeUniversalApp = async (opts: MakeUniversalOpts): Promise = const entryAsar = path.resolve(tmpDir, 'entry-asar'); await fs.mkdir(entryAsar); - await fs.copy( - path.resolve(__dirname, '..', '..', 'entry-asar', 'has-asar.js'), - path.resolve(entryAsar, 'index.js'), - ); let pj = JSON.parse( ( await asar.extractFile( @@ -302,7 +298,23 @@ export const makeUniversalApp = async (opts: MakeUniversalOpts): Promise = ) ).toString('utf8'), ); - pj.main = 'index.js'; + + if (pj.type === 'module') { + d('ERICK TEST!!!!'); + await fs.copy( + path.resolve(__dirname, '..', '..', 'entry-asar', 'esm', 'has-asar.mjs'), + path.resolve(entryAsar, 'index.mjs'), + ); + pj.main = 'index.mjs'; + } else { + await fs.copy( + path.resolve(__dirname, '..', '..', 'entry-asar', 'has-asar.js'), + path.resolve(entryAsar, 'index.js'), + ); + pj.main = 'index.js'; + } + d(JSON.stringify(pj, null, 2)); + await fs.writeJson(path.resolve(entryAsar, 'package.json'), pj); const asarPath = path.resolve(tmpApp, 'Contents', 'Resources', 'app.asar'); await asar.createPackage(entryAsar, asarPath); diff --git a/tsconfig.entry-asar.json b/tsconfig.entry-asar.json deleted file mode 100644 index 33a1928..0000000 --- a/tsconfig.entry-asar.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "outDir": "entry-asar", - }, - "include": [ - "entry-asar" - ], - "exclude": [] -} diff --git a/tsconfig.json b/tsconfig.json index fcac990..706f3f7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,6 @@ "declaration": true }, "include": [ - "src", - "entry-asar" - ] + "src" + ], }