/
cli-config-utils.ts
80 lines (72 loc) · 2.07 KB
/
cli-config-utils.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
import { Tree } from '@angular-devkit/schematics';
import { readJsonInTree } from './ast-utils';
import type { NxJsonConfiguration } from '@nrwl/devkit';
export function getWorkspacePath(host: Tree) {
const possibleFiles = ['/angular.json', '/workspace.json'];
return possibleFiles.filter((path) => host.exists(path))[0];
}
export function getNpmScope(host: Tree): string {
return readJsonInTree<NxJsonConfiguration>(host, 'nx.json').npmScope;
}
export function parseTarget(targetString: string) {
const [project, target, config] = targetString.split(':');
return {
project,
target,
config,
};
}
export function editTarget(targetString: string, callback) {
const parsedTarget = parseTarget(targetString);
return serializeTarget(callback(parsedTarget));
}
export function serializeTarget({ project, target, config }) {
return [project, target, config].filter((part) => !!part).join(':');
}
export function replaceAppNameWithPath(
node: any,
appName: string,
root: string
): any {
if (typeof node === 'string') {
const matchPattern = new RegExp(
`([^a-z0-9]*(${appName}))|((${appName})[^a-z0-9:]*)`,
'gi'
);
if (
!!node.match(matchPattern) &&
node !== 'application' &&
node !== 'library'
) {
const r = node.replace(appName, root);
return r.startsWith('/apps') || r.startsWith('/libs')
? r.substring(1)
: r;
} else {
return node;
}
} else if (Array.isArray(node)) {
return node.map((j) => replaceAppNameWithPath(j, appName, root));
} else if (typeof node === 'object' && node) {
const forbiddenPropertyList: string[] = [
'prefix',
'builder',
'executor',
'browserTarget',
'tags',
'defaultConfiguration',
'maximumError',
]; // Some of the properties should not be renamed
return Object.keys(node).reduce(
(m, c) => (
(m[c] = !forbiddenPropertyList.includes(c)
? replaceAppNameWithPath(node[c], appName, root)
: node[c]),
m
),
{} as any
);
} else {
return node;
}
}