/
library.ts
157 lines (133 loc) · 4.24 KB
/
library.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import {
convertNxGenerator,
formatFiles,
generateFiles,
getWorkspaceLayout,
joinPathFragments,
names,
offsetFromRoot,
readProjectConfiguration,
toJS,
Tree,
updateProjectConfiguration,
updateTsConfigsToJs,
} from '@nrwl/devkit';
import { Schema } from './schema';
import { libraryGenerator as workspaceLibraryGenerator } from '@nrwl/workspace/generators';
import { join } from 'path';
export interface NormalizedSchema extends Schema {
name: string;
prefix: string;
fileName: string;
projectRoot: string;
projectDirectory: string;
parsedTags: string[];
compiler: 'swc' | 'tsc';
}
export async function libraryGenerator(tree: Tree, schema: Schema) {
const options = normalizeOptions(tree, schema);
if (options.publishable === true && !schema.importPath) {
throw new Error(
`For publishable libs you have to provide a proper "--importPath" which needs to be a valid npm package name (e.g. my-awesome-lib or @myorg/my-lib)`
);
}
const libraryInstall = await workspaceLibraryGenerator(tree, {
...schema,
importPath: options.importPath,
testEnvironment: 'node',
skipFormat: true,
setParserOptionsProject: options.setParserOptionsProject,
});
createFiles(tree, options);
if (options.js) {
updateTsConfigsToJs(tree, options);
}
updateProject(tree, options);
if (!schema.skipFormat) {
await formatFiles(tree);
}
return libraryInstall;
}
export default libraryGenerator;
export const librarySchematic = convertNxGenerator(libraryGenerator);
function normalizeOptions(tree: Tree, options: Schema): NormalizedSchema {
const { npmScope, libsDir } = getWorkspaceLayout(tree);
const defaultPrefix = npmScope;
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
: name;
const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-');
const fileName = getCaseAwareFileName({
fileName: options.simpleModuleName ? name : projectName,
pascalCaseFiles: options.pascalCaseFiles,
});
const projectRoot = joinPathFragments(libsDir, projectDirectory);
const parsedTags = options.tags
? options.tags.split(',').map((s) => s.trim())
: [];
const importPath =
options.importPath || `@${defaultPrefix}/${projectDirectory}`;
return {
...options,
prefix: defaultPrefix, // we could also allow customizing this
fileName,
name: projectName,
projectRoot,
projectDirectory,
parsedTags,
importPath,
};
}
function getCaseAwareFileName(options: {
pascalCaseFiles: boolean;
fileName: string;
}) {
const normalized = names(options.fileName);
return options.pascalCaseFiles ? normalized.className : normalized.fileName;
}
function createFiles(tree: Tree, options: NormalizedSchema) {
const { className, name, propertyName } = names(options.fileName);
generateFiles(tree, join(__dirname, './files/lib'), options.projectRoot, {
...options,
className,
name,
propertyName,
tmpl: '',
offsetFromRoot: offsetFromRoot(options.projectRoot),
});
if (options.unitTestRunner === 'none') {
tree.delete(
join(options.projectRoot, `./src/lib/${options.fileName}.spec.ts`)
);
}
if (!options.publishable && !options.buildable) {
tree.delete(join(options.projectRoot, 'package.json'));
}
if (options.js) {
toJS(tree);
}
}
function updateProject(tree: Tree, options: NormalizedSchema) {
if (!options.publishable && !options.buildable) {
return;
}
const project = readProjectConfiguration(tree, options.name);
const { libsDir } = getWorkspaceLayout(tree);
project.targets = project.targets || {};
project.targets.build = {
executor: `@nrwl/js:${options.compiler}`,
outputs: ['{options.outputPath}'],
options: {
outputPath: `dist/${libsDir}/${options.projectDirectory}`,
tsConfig: `${options.projectRoot}/tsconfig.lib.json`,
packageJson: `${options.projectRoot}/package.json`,
main: `${options.projectRoot}/src/index` + (options.js ? '.js' : '.ts'),
assets: [`${options.projectRoot}/*.md`],
},
};
if (options.rootDir) {
project.targets.build.options.srcRootForCompilationRoot = options.rootDir;
}
updateProjectConfiguration(tree, options.name, project);
}