-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
update-ts-configs-to-js.ts
55 lines (45 loc) · 1.4 KB
/
update-ts-configs-to-js.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
import type { Tree } from 'nx/src/generators/tree';
import { updateJson } from 'nx/src/generators/utils/json';
export function updateTsConfigsToJs(
tree: Tree,
options: { projectRoot: string }
): void {
let updateConfigPath: string;
const paths = {
tsConfig: `${options.projectRoot}/tsconfig.json`,
tsConfigLib: `${options.projectRoot}/tsconfig.lib.json`,
tsConfigApp: `${options.projectRoot}/tsconfig.app.json`,
};
const getProjectType = (tree: Tree) => {
if (tree.exists(paths.tsConfigApp)) {
return 'application';
}
if (tree.exists(paths.tsConfigLib)) {
return 'library';
}
throw new Error(
`project is missing tsconfig.lib.json or tsconfig.app.json`
);
};
updateJson(tree, paths.tsConfig, (json) => {
if (json.compilerOptions) {
json.compilerOptions.allowJs = true;
} else {
json.compilerOptions = { allowJs: true };
}
return json;
});
const projectType = getProjectType(tree);
if (projectType === 'library') {
updateConfigPath = paths.tsConfigLib;
}
if (projectType === 'application') {
updateConfigPath = paths.tsConfigApp;
}
updateJson(tree, updateConfigPath, (json) => {
json.include = uniq([...json.include, '**/*.js']);
json.exclude = uniq([...json.exclude, '**/*.spec.js', '**/*.test.js']);
return json;
});
}
const uniq = <T extends string[]>(value: T) => [...new Set(value)] as T;