/
normalize-options.ts
100 lines (86 loc) · 2.72 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {
extractLayoutDirectory,
getImportPath,
getProjects,
getWorkspaceLayout,
joinPathFragments,
logger,
names,
normalizePath,
Tree,
} from '@nrwl/devkit';
import { assertValidStyle } from '../../../utils/assertion';
import { NormalizedSchema, Schema } from '../schema';
export function normalizeOptions(
host: Tree,
options: Schema
): NormalizedSchema {
const name = names(options.name).fileName;
const { projectDirectory, layoutDirectory } = extractLayoutDirectory(
options.directory
);
const fullProjectDirectory = projectDirectory
? `${names(projectDirectory).fileName}/${name}`
: name;
const projectName = fullProjectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = projectName;
const { libsDir: defaultLibsDir, npmScope } = getWorkspaceLayout(host);
const libsDir = layoutDirectory ?? defaultLibsDir;
const projectRoot = joinPathFragments(libsDir, fullProjectDirectory);
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];
const importPath =
options.importPath || getImportPath(npmScope, fullProjectDirectory);
let bundler = options.bundler ?? 'none';
if (bundler === 'none') {
if (options.publishable) {
logger.warn(
`Publishable libraries cannot be used with bundler: 'none'. Defaulting to 'rollup'.`
);
bundler = 'rollup';
}
if (options.buildable) {
logger.warn(
`Buildable libraries cannot be used with bundler: 'none'. Defaulting to 'rollup'.`
);
bundler = 'rollup';
}
}
const normalized = {
...options,
compiler: options.compiler ?? 'babel',
bundler,
fileName,
routePath: `/${name}`,
name: projectName,
projectRoot,
projectDirectory: fullProjectDirectory,
parsedTags,
importPath,
libsDir,
} as NormalizedSchema;
// Libraries with a bundler or is publishable must also be buildable.
normalized.buildable = Boolean(
normalized.bundler !== 'none' || options.buildable || options.publishable
);
normalized.inSourceTests === normalized.minimal || normalized.inSourceTests;
if (options.appProject) {
const appProjectConfig = getProjects(host).get(options.appProject);
if (appProjectConfig.projectType !== 'application') {
throw new Error(
`appProject expected type of "application" but got "${appProjectConfig.projectType}"`
);
}
try {
normalized.appMain = appProjectConfig.targets.build.options.main;
normalized.appSourceRoot = normalizePath(appProjectConfig.sourceRoot);
} catch (e) {
throw new Error(
`Could not locate project main for ${options.appProject}`
);
}
}
assertValidStyle(normalized.style);
return normalized;
}