/
normalize-options.ts
34 lines (31 loc) · 1.01 KB
/
normalize-options.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
import { joinPathFragments, names, readJson, Tree } from '@nrwl/devkit';
import { GeneratorOptions } from '../schema';
import { WorkspaceProjects } from './types';
export function normalizeOptions(
tree: Tree,
options: GeneratorOptions,
projects: WorkspaceProjects
): GeneratorOptions {
let npmScope = options.npmScope ?? options.name;
if (npmScope) {
npmScope = names(npmScope).fileName;
} else if (projects.libs.length > 0) {
// try get the scope from any library that have one
for (const lib of projects.libs) {
const { name } = readJson(
tree,
joinPathFragments(lib.config.root, 'package.json')
);
if (name.startsWith('@')) {
npmScope = name.split('/')[0].substring(1);
break;
}
}
}
if (!npmScope) {
// use the name (scope if exists) in the root package.json
const { name } = readJson(tree, 'package.json');
npmScope = name.startsWith('@') ? name.split('/')[0].substring(1) : name;
}
return { ...options, npmScope };
}