Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/prepare-build.js
1028 views
1
const fs = require('fs');
2
3
const buildDir = `${__dirname}/build`;
4
5
function processPackageJson(packagePath) {
6
const packageJson = JSON.parse(fs.readFileSync(`${packagePath}/package.json`, 'utf8'));
7
let overridesJson = null;
8
if (fs.existsSync(`${packagePath}/package.build.json`)) {
9
overridesJson = JSON.parse(fs.readFileSync(`${packagePath}/package.build.json`, 'utf8'));
10
console.log('Has package.json overrides', packagePath, overridesJson);
11
}
12
13
if (!overridesJson) return;
14
15
const finalPackageJson = {
16
...packageJson,
17
name: overridesJson.name || packageJson.name,
18
scripts: overridesJson.scripts,
19
dependencies: overridesJson.dependencies || packageJson.dependencies,
20
devDependencies: overridesJson.devDependencies || packageJson.devDependencies,
21
workspaces: overridesJson.workspaces || packageJson.workspaces,
22
};
23
24
console.log('writing', `${packagePath}/package.json`);
25
fs.writeFileSync(`${packagePath}/package.json`, JSON.stringify(finalPackageJson, null, 2));
26
}
27
28
function processDir(path) {
29
for (const dirname of fs.readdirSync(path)) {
30
if (dirname === 'node_modules' || dirname.startsWith('.')) continue;
31
const fullpath = `${path}/${dirname}`;
32
const stat = fs.lstatSync(fullpath);
33
if (stat.isDirectory()) {
34
processDir(fullpath);
35
} else if (stat.isFile() && dirname === 'package.json') {
36
processPackageJson(path);
37
}
38
}
39
}
40
41
processDir(buildDir);
42
43