-
Notifications
You must be signed in to change notification settings - Fork 124
/
sync-tsconfig-deps.js
129 lines (113 loc) · 4.56 KB
/
sync-tsconfig-deps.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const fs = require('fs');
const packages = fs.readdirSync('packages/');
const packageConfigs = {};
for (const name of packages) {
const path = `packages/${name}`;
if (!fs.lstatSync(path).isDirectory()) continue;
const packageJsonPath = `${path}/package.json`;
const tsConfigPath = `${path}/tsconfig.json`;
const tsConfigESMPath = `${path}/tsconfig.esm.json`;
if (!fs.existsSync(packageJsonPath)) throw new Error(`package ${name} has no package.json`);
if (!fs.existsSync(tsConfigPath)) continue;
packageConfigs[name] = {
path: path,
}
try {
packageConfigs[name].package = JSON.parse(fs.readFileSync(packageJsonPath, {encoding: 'utf8'}));
} catch (error) {
throw new Error(`Could not read ${packageJsonPath}: ${error}`)
}
try {
packageConfigs[name].tsConfig = JSON.parse(fs.readFileSync(tsConfigPath, {encoding: 'utf8'}));
} catch (error) {
throw new Error(`Could not read ${tsConfigPath}: ${error}`)
}
try {
if (fs.existsSync(tsConfigESMPath)) {
packageConfigs[name].tsConfigESM = JSON.parse(fs.readFileSync(tsConfigESMPath, {encoding: 'utf8'}));
}
} catch (error) {
throw new Error(`Could not read ${tsConfigESMPath}: ${error}`)
}
}
for (const [name, config] of Object.entries(packageConfigs)) {
const deps = Array.from(new Set([
...Object.keys(config.package.dependencies || {}),
...Object.keys(config.package.devDependencies || {}),
...Object.keys(config.package.peerDependencies || {})
])).filter(v => {
const [, depName] = v.split('/');
return v.startsWith('@deepkit/') && !fs.existsSync(`packages/${depName}/angular.json`)
});
const path = `packages/${name}`;
const tsConfigPath = `${path}/tsconfig.json`;
const tsConfigESMPath = `${path}/tsconfig.esm.json`;
let tsConfigChanged = false;
config.tsConfig.references = [];
for (const dep of deps) {
const [, depName] = dep.split('/');
config.tsConfig.references.push({path: `../${depName}/tsconfig.json`});
}
fs.writeFileSync(tsConfigPath, JSON.stringify(config.tsConfig, undefined, 2));
if (config.tsConfigESM) {
let tsConfigESMChanged = false;
config.tsConfigESM.references = [];
for (const dep of deps) {
const [, depName] = dep.split('/');
const requiredReference = fs.existsSync(`packages/${depName}/tsconfig.esm.json`)
? `../${depName}/tsconfig.esm.json` : `../${depName}/tsconfig.json`;
config.tsConfigESM.references.push({path: requiredReference});
}
fs.writeFileSync(tsConfigESMPath, JSON.stringify(config.tsConfigESM, undefined, 2));
}
}
//
// const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' }));
// const tsReferences = new Set();
// const tsConfigString = fs.readFileSync(tsConfigPath, { encoding: 'utf8' });
// try {
// const tsConfig = JSON.parse(tsConfigString);
//
// if (tsConfig['references']) {
// try {
// if (fs.existsSync(tsConfigESMPath)) {
// const tsConfigCjs = JSON.parse(fs.readFileSync(tsConfigESMPath, {encoding: 'utf8'}));
// tsConfigCjs['references'] = tsConfig['references'].map(v => {
// return {path: v.path.replace('tsconfig.json', 'tsconfig.esm.json')}
// });
// fs.writeFileSync(tsConfigESMPath, JSON.stringify(tsConfigCjs, undefined, 2));
// }
// } catch (error) {
// console.log('tsconfig.esm.json', tsConfigString);
// throw error;
// }
//
// for (const dep of tsConfig['references']) {
// let path = dep['path'];
// if (path.startsWith('../')) {
// path = path.substr(path.indexOf('/') + 1);
// path = path.substr(0, path.lastIndexOf('/'));
// tsReferences.add('@deepkit/' + path);
// }
// }
// } else {
// continue;
// }
// } catch (error) {
// console.log('tsconfig.json', tsConfigString);
// throw error;
// }
//
// let deps = [...Object.keys(packageJson['dependencies'] || {}), ...Object.keys(packageJson['devDependencies'] || {})];
//
// // console.log(' -> ts references', [...tsReferences]);
// // console.log(' -> deps', deps);
//
// for (const dep of deps) {
// if (dep.startsWith('@deepkit/')) {
// if (!tsReferences.has(dep)) {
// console.log(` ERR: ${dep} as dependency, but not in tsconfig.json references.`);
// }
// }
// }
// }