forked from ulixee/secret-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare-build.js
41 lines (34 loc) · 1.4 KB
/
prepare-build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const fs = require('fs');
const buildDir = `${__dirname}/build`;
function processPackageJson(packagePath) {
const packageJson = JSON.parse(fs.readFileSync(`${packagePath}/package.json`, 'utf8'));
let overridesJson = null;
if (fs.existsSync(`${packagePath}/package.build.json`)) {
overridesJson = JSON.parse(fs.readFileSync(`${packagePath}/package.build.json`, 'utf8'));
console.log('Has package.json overrides', packagePath, overridesJson);
}
if (!overridesJson) return;
const finalPackageJson = {
...packageJson,
name: overridesJson.name || packageJson.name,
scripts: overridesJson.scripts,
dependencies: overridesJson.dependencies || packageJson.dependencies,
devDependencies: overridesJson.devDependencies || packageJson.devDependencies,
workspaces: overridesJson.workspaces || packageJson.workspaces,
};
console.log('writing', `${packagePath}/package.json`);
fs.writeFileSync(`${packagePath}/package.json`, JSON.stringify(finalPackageJson, null, 2));
}
function processDir(path) {
for (const dirname of fs.readdirSync(path)) {
if (dirname === 'node_modules' || dirname.startsWith('.')) continue;
const fullpath = `${path}/${dirname}`;
const stat = fs.lstatSync(fullpath);
if (stat.isDirectory()) {
processDir(fullpath);
} else if (stat.isFile() && dirname === 'package.json') {
processPackageJson(path);
}
}
}
processDir(buildDir);