forked from Olivr/nx-copybara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.js
71 lines (61 loc) · 2.06 KB
/
prepare.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
const fs = require('fs');
const path = require('path');
const rootDir = process.env.PWD;
const extDir = path.join(rootDir, 'externals');
const nxSource = JSON.parse(
fs.readFileSync(path.join(rootDir, 'nx.json'), 'utf-8')
);
const wsSource = JSON.parse(
fs.readFileSync(path.join(rootDir, 'workspace.json'), 'utf-8')
);
const tsSource = JSON.parse(
fs.readFileSync(path.join(rootDir, 'tsconfig.base.json'), 'utf-8')
);
const externals = process.argv.slice(2);
//const externals = nxSource.externals
if (externals.length && !fs.existsSync(extDir)) fs.mkdirSync(extDir);
externals.forEach((tag) => {
const nx = { ...nxSource };
const ws = { ...wsSource };
const ts = { ...tsSource };
const projects = {};
// Determine projects to include
Object.entries(nx.projects).forEach(([k, v]) => {
if (v.tags.includes(tag)) projects[k] = '';
});
// Prepare nx.json
nx.projects = Object.keys(nx.projects)
.filter((key) => Object.keys(projects).includes(key))
.reduce((obj, key) => {
obj[key] = nx.projects[key];
return obj;
}, {});
// Prepare workspace.json
ws.projects = Object.keys(ws.projects)
.filter((key) => Object.keys(projects).includes(key))
.reduce((obj, key) => {
obj[key] = ws.projects[key];
projects[key] = ws.projects[key].root.replace(/\/$/, '').concat('/');
return obj;
}, {});
// Prepare tsconfig.base.json
const paths = new RegExp(`^(${Object.values(projects).join('|')})`);
ts.compilerOptions.paths = Object.entries(ts.compilerOptions.paths)
.filter(([k, v]) => v.filter((path) => paths.test(path)).length == v.length)
.reduce((obj, [k, v]) => {
obj[k] = ts.compilerOptions.paths[k];
return obj;
}, {});
// Save files
const dir = path.join(extDir, tag);
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
fs.writeFileSync(path.join(dir, 'nx.json'), JSON.stringify(nx, null, 2));
fs.writeFileSync(
path.join(dir, 'workspace.json'),
JSON.stringify(ws, null, 2)
);
fs.writeFileSync(
path.join(dir, 'tsconfig.base.json'),
JSON.stringify(ts, null, 2)
);
});