-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-references.mjs
54 lines (47 loc) · 1.41 KB
/
setup-references.mjs
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
import { constants, accessSync } from 'fs';
import { writeFile } from 'fs/promises';
import util from 'util';
import path from 'path';
import childProcess from 'child_process';
const exec = util.promisify(childProcess.exec);
const CONFIG = {
packagesFolder: './packages',
npmNamespace: '@konturio',
};
async function getTopology() {
const { stdout, stderr } = await exec('lerna ls --toposort');
if (stderr) {
console.log(stderr);
}
const topology = stdout.trim().split('\n');
return topology;
}
const pathExists = (path) => {
try {
accessSync(path, constants.R_OK);
return true;
} catch (_) {
return false;
}
};
const takeOnlyTypescriptPackages = (packages) =>
packages.filter((p) => pathExists(path.resolve(p, 'tsconfig.build.json')));
function createTsConfigWithReferences(packages) {
return {
files: [],
references: packages.map((pkg) => ({
path: pkg.replace(`${CONFIG.packagesFolder}/`, '') + '/tsconfig.build.json',
})),
compilerOptions: {
composite: true,
},
};
}
(async () => {
const topology = await getTopology();
const packages = topology.map((pkg) => pkg.replace(CONFIG.npmNamespace, CONFIG.packagesFolder));
const tsPackages = takeOnlyTypescriptPackages(packages);
const tsConfig = createTsConfigWithReferences(tsPackages);
const json = JSON.stringify(tsConfig, null, 2);
writeFile(`${CONFIG.packagesFolder}/tsconfig.json`, json);
})();