diff --git a/apps/desktop/scripts/copy-dist.ts b/apps/desktop/scripts/copy-dist.ts index e6d18f439..9f1643557 100644 --- a/apps/desktop/scripts/copy-dist.ts +++ b/apps/desktop/scripts/copy-dist.ts @@ -13,17 +13,20 @@ function log(...args: any[]) { } try { + fs.mkdirpSync(DEST_DIR); + copyNodeModules("./package.json"); + copyPackageJson(); + /** * Copy the server. */ fs.copySync("../server/build", path.join(DEST_DIR, "node_modules", "@triliumnext/server")); - copyPackageJson(); - /** * Copy assets. */ const assetsToCopy = new Set([ + "./tsconfig.json", "./forge.config.cjs", "./scripts/electron-forge/desktop.ejs", "./scripts/electron-forge/sign-windows.cjs", @@ -50,6 +53,29 @@ try { process.exit(1) } +/** + * We cannot copy the node_modules directory directly because we are in a monorepo and all the packages are gathered at root level. + * We cannot copy the files manually because we'd have to implement all the npm lookup logic, especially since there are issues with the same library having multiple versions across dependencies. + * + * @param packageJsonPath + */ +function copyNodeModules(packageJsonPath: string) { + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); + + // Skip monorepo packages + packageJson.dependencies = Object.fromEntries( + Object.entries(packageJson.dependencies).filter(([key]) => { + return !key.startsWith("@triliumnext"); + })); + + // Trigger an npm install to obtain the dependencies. + fs.writeFileSync(path.join(DEST_DIR, "package.json"), JSON.stringify(packageJson)); + execSync(`npm install --omit=dev`, { + cwd: DEST_DIR, + stdio: "inherit", + }); +} + /** * Rewrite the name field of `package.json` since electron-forge does not support forward slashes in the name. * Other attempts to rewrite the name field in the forge config have failed.