fix(monorepo/server): use different mechanism for obtaining dependencies

This commit is contained in:
Elian Doran 2025-04-19 15:05:15 +03:00
parent e408f7d8f1
commit 75bdb3117f
No known key found for this signature in database

View File

@ -13,6 +13,7 @@ function log(...args: any[]) {
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import { dirname } from "path"; import { dirname } from "path";
import { execSync } from "child_process";
const scriptDir = dirname(fileURLToPath(import.meta.url)); const scriptDir = dirname(fileURLToPath(import.meta.url));
const rootDir = path.resolve(scriptDir, "..", "..", ".."); const rootDir = path.resolve(scriptDir, "..", "..", "..");
@ -29,34 +30,26 @@ function copyAssets(baseDir: string, destDir: string, files: string[]) {
} }
/** /**
* Copies the dependencies from the node_modules directory to the build directory.
* 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 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 * @param packageJsonPath
*/ */
function copyNodeModules(packageJsonPath: string) { function copyNodeModules(packageJsonPath: string) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
const dependencies = packageJson.dependencies || {};
for (const dependency of Object.keys(dependencies)) { // Skip monorepo packages
if (dependency.startsWith("@triliumnext/")) { packageJson.dependencies = Object.fromEntries(
// Skip copying @triliumnext dependencies since they are symlinked in the monorepo. Object.entries(packageJson.dependencies).filter(([key]) => {
continue; return !key.startsWith("@triliumnext");
} }));
const src = path.join(rootDir, "node_modules", dependency); // Trigger an npm install to obtain the dependencies.
if (!fs.existsSync(src)) { fs.writeFileSync(path.join(DEST_DIR, "package.json"), JSON.stringify(packageJson));
console.warn(`Dependency ${dependency} not found in node_modules. Skipping.`); execSync(`npm install --omit=dev --omit=optional --omit=peer`, {
continue; cwd: DEST_DIR,
} stdio: "inherit",
});
const dest = path.join(DEST_DIR, "node_modules", dependency);
log(`${src} -> ${dest}`);
fs.copySync(src, dest);
// Copy sub-dependencies as well.
copyNodeModules(path.join(src, "package.json"));
}
} }
try { try {
@ -90,6 +83,7 @@ try {
"README.md" "README.md"
]; ];
fs.mkdirpSync(DEST_DIR);
copyNodeModules(path.join(serverDir, "package.json")); copyNodeModules(path.join(serverDir, "package.json"));
copyAssets(clientDir, path.join(DEST_DIR, "src", "public"), clientAssets); copyAssets(clientDir, path.join(DEST_DIR, "src", "public"), clientAssets);
copyAssets(serverDir, path.join(DEST_DIR), serverAssets); copyAssets(serverDir, path.join(DEST_DIR), serverAssets);