forked from webhintio/hint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-tsconfig-references.ts
100 lines (77 loc) · 3.35 KB
/
update-tsconfig-references.ts
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
import * as shell from 'shelljs';
type PackageJSON = {
dependencies?: { [name: string ]: string };
devDependencies?: { [name: string ]: string };
peerDependencies?: { [name: string ]: string };
};
type TSConfig = {
references?: { path: string }[];
};
const getReferencesFromDependencies = (packagePath: string): string[] => {
const packageJSONPath = `${packagePath}/package.json`;
const packageJSON: PackageJSON = require(`../../${packageJSONPath}`);
const dependencies = Object.keys(packageJSON.dependencies || {});
const devDependencies = Object.keys(packageJSON.devDependencies || {});
const peerDependencies = Object.keys(packageJSON.peerDependencies || {});
const allDependencies = [...dependencies, ...devDependencies, ...peerDependencies];
const uniqueDependencies = Array.from(new Set(allDependencies));
// Map `hint` and `@hint/*` dependencies from `package.json` to `tsconfig.json` reference names.
return uniqueDependencies
.filter((name) => {
return name === 'hint' || name.startsWith('@hint/');
})
.map((name) => {
return name.replace('@hint/', '');
});
};
const compactReferences = (json: string): string => {
// Condense JSON-serialized references to fit on a single line.
return json.replace(/\{\r?\n\s*("path": "[^"]+")\r?\n\s*}/g, '{ $1 }');
};
const updateFile = (filePath: string, content: string) => {
const writeContent = (shell as any)['ShellString']; // eslint-disable-line dot-notation
writeContent(content).to(filePath);
};
const main = () => {
// Include all sub-packages (except configurations which don't use TypeScript).
const subPackages = Array.from(shell.ls('-d', 'packages/!(configuration-*)'));
// For the root and every package in this repo.
['.', ...subPackages].forEach((packagePath: string) => {
const tsconfigPath = `${packagePath}/tsconfig.json`;
const tsconfigJSON: TSConfig = require(`../../${tsconfigPath}`);
let prefix: string;
let references: string[];
if (packagePath === '.') {
// Include all sub-packages as references in the root `tsconfig.json`.
prefix = '';
references = subPackages;
/**
* references = subPackages.filter((path) => {
* // Exclude any package as needed here
* return path !== 'packages/package-to-ignore';
* });
*
*/
} else {
// Only include explicit dependencies in sub-packages.
prefix = '../';
references = getReferencesFromDependencies(packagePath);
}
// Convert references to the expected `tsconfig.json` format.
tsconfigJSON.references = references
.sort()
.map((reference) => {
return { path: `${prefix}${reference}` };
});
// Omit the references section if none exist.
if (!tsconfigJSON.references.length) {
delete tsconfigJSON.references;
}
// Serialize the updated `tsconfig.json`, keeping references on one line each.
const json = JSON.stringify(tsconfigJSON, null, 4);
const compactedJSON = compactReferences(json);
// Save the changes to `tsconfig.json`.
updateFile(tsconfigPath, `${compactedJSON}\n`);
});
};
main();