Notes/apps/desktop/scripts/copy-dist.ts
2025-04-21 00:19:58 +03:00

89 lines
2.7 KiB
TypeScript

import { execSync } from "child_process";
import fs from "fs-extra";
import path from "path";
const DEST_DIR = "./build";
const VERBOSE = process.env.VERBOSE;
function log(...args: any[]) {
if (VERBOSE) {
console.log(...args);
}
}
try {
fs.mkdirpSync(DEST_DIR);
copyNodeModules("./package.json");
copyPackageJson();
/**
* Copy the server.
*/
fs.copySync("../server/build", path.join(DEST_DIR, "node_modules", "@triliumnext/server"));
/**
* Copy assets.
*/
const assetsToCopy = new Set([
"./tsconfig.json",
"./forge.config.cjs",
"./scripts/electron-forge/desktop.ejs",
"./scripts/electron-forge/sign-windows.cjs",
]);
for (const asset of assetsToCopy) {
log(`Copying ${asset}`);
fs.copySync(asset, path.join(DEST_DIR, asset));
}
/**
* Directories to be copied relative to the project root into <resource_dir>/src/public/app-dist.
*/
const publicDirsToCopy = ["../server/src/public/app/doc_notes"];
const PUBLIC_DIR = path.join(DEST_DIR, "src", "public", "app-dist");
for (const dir of publicDirsToCopy) {
fs.copySync(dir, path.join(PUBLIC_DIR, path.basename(dir)));
}
console.log("Copying complete!")
} catch(err) {
console.error("Error during copy:", err)
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.
*/
function copyPackageJson() {
const packageJsonPath = path.join("package.json");
const packageJson = fs.readJSONSync(packageJsonPath);
packageJson.name = "trilium";
fs.writeJSONSync(path.join(DEST_DIR, "package.json"), packageJson, { spaces: 2 });
}