const { existsSync, readFileSync, realpathSync } = require("fs");
const { join } = require("path");
const { execSync } = require("child_process");
if (existsSync("package.json")) {
const package = JSON.parse(readFileSync("package.json").toString());
const root = realpathSync(".");
const MAKE_DEP_EXCLUDE = process.env.MAKE_DEP_EXCLUDE ?? "";
for (const key of ["devDependencies", "dependencies"]) {
for (const name in package[key] ?? {}) {
if (package[key][name].startsWith("workspace:")) {
if (MAKE_DEP_EXCLUDE.includes(`:${name}:`)) {
throw Error(
`CIRCULAR DEPENDENCY: MAKE_DEP_EXCLUDE=${MAKE_DEP_EXCLUDE}, name=${name}`
);
}
if (existsSync(join(root, "node_modules", name, "dist"))) {
continue;
}
console.log(`\nDEPENDENCY: ${name} - make`);
const cwd = realpathSync(join(root, "node_modules", name));
console.log(`cd '${cwd}'`);
execSync("make", {
stdio: "inherit",
cwd,
env: {
...process.env,
MAKE_DEP_EXCLUDE: MAKE_DEP_EXCLUDE + ":" + name + ":",
},
});
console.log(`DEPENDENCY: ${name} successfully built\n`);
console.log(`cd "${root}"`);
}
}
}
}