diff --git a/.gitignore b/.gitignore index 347140b..a4d488f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ node_modules dist -entry-asar/cjs/*.?js* -entry-asar/cjs/*.d.?ts +entry-asar/cjs/*.js* +entry-asar/cjs/*.d.ts entry-asar/esm/*.?js* entry-asar/esm/*.d.?ts *.app diff --git a/entry-asar/esm/no-asar.mts b/entry-asar/esm/no-asar.mts new file mode 100644 index 0000000..72b28e7 --- /dev/null +++ b/entry-asar/esm/no-asar.mts @@ -0,0 +1,29 @@ +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 + const appPath = app.getAppPath(); + const appFolder = `app-${platform}`; + + // Maybe we'll handle this in Electron one day + if (path.basename(appPath) === 'app') { + const platformAppPath = path.join(path.dirname(appPath), appFolder); + + // This is an undocumented private API. It exists. + app.setAppPath(platformAppPath); + } + + const require = createRequire(import.meta.url); + process._archPath = require.resolve(`../${appFolder}`); + + await import(process._archPath); +} diff --git a/package.json b/package.json index e3ec4f1..9c1f424 100644 --- a/package.json +++ b/package.json @@ -21,11 +21,12 @@ "dist/*", "entry-asar/*", "!entry-asar/**/*.ts", + "!entry-asar/**/tsconfig.json", "README.md" ], "author": "Samuel Attard", "scripts": { - "build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && tsc -p tsconfig.entry-asar.json && tsc -p tsconfig.entry-asar.esm.json", + "build": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json && tsc -p entry-asar/esm/tsconfig.json && tsc -p entry-asar/cjs/tsconfig.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 feb537a..876ec05 100644 --- a/src/index.ts +++ b/src/index.ts @@ -210,14 +210,27 @@ 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', 'no-asar.js'), - path.resolve(entryAsar, 'index.js'), - ); + let pj = await fs.readJson( path.resolve(opts.x64AppPath, 'Contents', 'Resources', 'app', 'package.json'), ); - pj.main = 'index.js'; + + // Load a shim that redirects to the correct folder for the architecture. + // This needs to be a different file depending on if the app entrypoint is CommonJS or ESM. + if (pj.type === 'module' || pj.main.endsWith('.mjs')) { + await fs.copy( + path.resolve(__dirname, '..', '..', 'entry-asar', 'esm', 'no-asar.mjs'), + path.resolve(entryAsar, 'index.mjs'), + ); + pj.main = 'index.mjs'; + } else { + await fs.copy( + path.resolve(__dirname, '..', '..', 'entry-asar', 'cjs', 'no-asar.js'), + path.resolve(entryAsar, 'index.js'), + ); + pj.main = 'index.js'; + } + await fs.writeJson(path.resolve(entryAsar, 'package.json'), pj); await asar.createPackage( entryAsar, @@ -299,8 +312,9 @@ export const makeUniversalApp = async (opts: MakeUniversalOpts): Promise = ).toString('utf8'), ); - if (pj.type === 'module') { - d('ERICK TEST!!!!'); + // Load a shim that redirects to the correct `app.asar` for the architecture. + // This needs to be a different file depending on if the app entrypoint is CommonJS or ESM. + if (pj.type === 'module' || pj.main.endsWith('.mjs')) { await fs.copy( path.resolve(__dirname, '..', '..', 'entry-asar', 'esm', 'has-asar.mjs'), path.resolve(entryAsar, 'index.mjs'), @@ -308,12 +322,11 @@ export const makeUniversalApp = async (opts: MakeUniversalOpts): Promise = pj.main = 'index.mjs'; } else { await fs.copy( - path.resolve(__dirname, '..', '..', 'entry-asar', 'has-asar.js'), + path.resolve(__dirname, '..', '..', 'entry-asar', 'cjs', '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');