nx/packages/angular/src/utils/cli-config-utils.ts
Colum Ferry 14dd8e1e27
feat(angular): move replaceAppNameWithPath to Angular (#15493)
Co-authored-by: Michael Ziluck <michael@ziluck.com>
2023-03-08 18:28:14 -05:00

52 lines
1.3 KiB
TypeScript

export function replaceAppNameWithPath(
node: string | string[] | object | unknown,
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',
'name',
'type',
'outputHashing',
'buildTarget',
]; // 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;
}
}