/
set-project-names.ts
32 lines (29 loc) · 1.22 KB
/
set-project-names.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
import { Tree } from '../../generators/tree';
import { readNxJson } from '../../generators/utils/project-configuration';
import { globForProjectFiles } from '../../config/workspaces';
import { dirname } from 'path';
import { readJson, writeJson } from '../../generators/utils/json';
import { formatChangedFilesWithPrettierIfAvailable } from '../../generators/internal-utils/format-changed-files-with-prettier-if-available';
export default async function (tree: Tree) {
const nxJson = readNxJson(tree);
const projectFiles = globForProjectFiles(tree.root, nxJson);
const projectJsons = projectFiles.filter((f) => f.endsWith('project.json'));
for (let f of projectJsons) {
const projectJson = readJson(tree, f);
if (!projectJson.name) {
projectJson.name = toProjectName(dirname(f), nxJson);
writeJson(tree, f, projectJson);
}
}
await formatChangedFilesWithPrettierIfAvailable(tree);
}
function toProjectName(directory: string, nxJson: any): string {
let { appsDir, libsDir } = nxJson?.workspaceLayout || {};
appsDir ??= 'apps';
libsDir ??= 'libs';
const parts = directory.split(/[\/\\]/g);
if ([appsDir, libsDir].includes(parts[0])) {
parts.splice(0, 1);
}
return parts.join('-').toLowerCase();
}