forked from sleeyax/secret-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-dist.js
83 lines (72 loc) · 2.66 KB
/
prepare-dist.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs');
const buildDistDir = `${__dirname}/build-dist`;
const rootPackageJson = require('./package.json');
const defaults = {
repository: rootPackageJson.repository,
license: rootPackageJson.license,
bugs: rootPackageJson.bugs,
author: rootPackageJson.author,
contributors: rootPackageJson.contributors,
homepage: rootPackageJson.homepage,
engines: {
node: '>=12.0.0',
},
publishConfig: {
access: 'public',
},
};
const licensePath = `${__dirname}/LICENSE.md`;
const readmePath = `${__dirname}/README.md`;
function processPackageJson(packagePath) {
const packageJson = JSON.parse(fs.readFileSync(`${packagePath}/package.json`, 'utf8'));
let overridesJson = {};
if (fs.existsSync(`${packagePath}/package.dist.json`)) {
overridesJson = JSON.parse(fs.readFileSync(`${packagePath}/package.dist.json`, 'utf8'));
console.log('Has package.json overrides', packagePath, overridesJson);
fs.unlinkSync(`${packagePath}/package.dist.json`);
}
if (packageJson.private) {
console.log('Private package, skipping', packagePath);
return;
}
fs.copyFileSync(licensePath, `${packagePath}/LICENSE.md`);
const finalPackageJson = {
name: overridesJson.name || packageJson.name,
version: overridesJson.version || packageJson.version,
description: overridesJson.description || packageJson.description,
main: overridesJson.main || packageJson.main,
types: overridesJson.types || packageJson.types,
files: overridesJson.files || packageJson.files,
...defaults,
scripts: overridesJson.scripts,
dependencies: overridesJson.dependencies || packageJson.dependencies,
engine: packageJson.engine, // this is used by emulators
};
// check if index exists
if (!finalPackageJson.files && !finalPackageJson.main) {
if (fs.existsSync(`${packagePath}/index.js`)) {
finalPackageJson.main = 'index.js';
}
}
if (finalPackageJson.main && !finalPackageJson.types) {
finalPackageJson.types = finalPackageJson.main.replace('.js', '.d.ts');
}
if (finalPackageJson.name === 'secret-agent') {
fs.copyFileSync(readmePath, `${packagePath}/README.md`);
}
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}`;
if (fs.existsSync(`${fullpath}/package.json`)) {
processPackageJson(fullpath);
}
if (fs.lstatSync(fullpath).isDirectory()) {
processDir(fullpath);
}
}
}
processDir(buildDistDir);