-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
workspace.ts
187 lines (171 loc) · 4.97 KB
/
workspace.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import {
generateFiles,
Tree,
updateJson,
convertNxGenerator,
names,
writeJson,
formatFiles,
} from '@nrwl/devkit';
import { Schema } from './schema';
import {
angularCliVersion,
nxVersion,
prettierVersion,
typescriptVersion,
} from '../../utils/versions';
import { readFileSync } from 'fs';
import { join, join as pathJoin } from 'path';
import { reformattedWorkspaceJsonOrNull } from 'nx/src/config/workspaces';
import { Preset } from '../utils/presets';
import { deduceDefaultBase } from '../../utilities/default-base';
export const DEFAULT_NRWL_PRETTIER_CONFIG = {
singleQuote: true,
};
function decorateAngularClI(host: Tree, options: Schema) {
const decorateCli = readFileSync(
pathJoin(__dirname as any, '..', 'utils', 'decorate-angular-cli.js__tmpl__')
).toString();
host.write(join(options.directory, 'decorate-angular-cli.js'), decorateCli);
}
function setPresetProperty(tree: Tree, options: Schema) {
updateJson(tree, join(options.directory, 'nx.json'), (json) => {
if (
options.preset === Preset.Core ||
options.preset === Preset.TS ||
options.preset === Preset.NPM
) {
addPropertyWithStableKeys(json, 'extends', 'nx/presets/core.json');
delete json.implicitDependencies;
delete json.targetDependencies;
delete json.workspaceLayout;
}
return json;
});
}
function createAppsAndLibsFolders(host: Tree, options: Schema) {
if (
options.preset === Preset.Core ||
options.preset === Preset.TS ||
options.preset === Preset.NPM
) {
host.write(join(options.directory, 'packages/.gitkeep'), '');
} else {
host.write(join(options.directory, 'apps/.gitkeep'), '');
host.write(join(options.directory, 'libs/.gitkeep'), '');
}
}
function createFiles(host: Tree, options: Schema) {
const npmScope = options.npmScope ?? options.name;
const formattedNames = names(options.name);
generateFiles(host, pathJoin(__dirname, './files'), options.directory, {
formattedNames,
dot: '.',
tmpl: '',
workspaceFile: options.cli === 'angular' ? 'angular' : 'workspace',
cliCommand: options.cli === 'angular' ? 'ng' : 'nx',
nxCli: false,
typescriptVersion,
prettierVersion,
// angular cli is used only when workspace schematics is added to angular cli
angularCliVersion,
...(options as object),
nxVersion,
npmScope,
packageManager: options.packageManager,
});
}
function createPrettierrc(host: Tree, options: Schema) {
writeJson(
host,
join(options.directory, '.prettierrc'),
DEFAULT_NRWL_PRETTIER_CONFIG
);
}
// ensure that pnpm install add all the missing peer deps
function createNpmrc(host: Tree, options: Schema) {
host.write(
join(options.directory, '.npmrc'),
'strict-peer-dependencies=false\nauto-install-peers=true\n'
);
}
function formatWorkspaceJson(host: Tree, options: Schema) {
const path = join(
options.directory,
options.cli === 'angular' ? 'angular.json' : 'workspace.json'
);
try {
updateJson(host, path, (workspaceJson) => {
const reformatted = reformattedWorkspaceJsonOrNull(workspaceJson);
if (reformatted) {
return reformatted;
}
return workspaceJson;
});
} catch (e) {
console.error(`Failed to format: ${path}`);
console.error(e);
}
}
function addNpmScripts(host: Tree, options: Schema) {
if (options.cli === 'angular') {
updateJson(host, join(options.directory, 'package.json'), (json) => {
Object.assign(json.scripts, {
ng: 'nx',
postinstall: 'node ./decorate-angular-cli.js',
});
return json;
});
}
if (
options.preset !== Preset.TS &&
options.preset !== Preset.Core &&
options.preset !== Preset.NPM
) {
updateJson(host, join(options.directory, 'package.json'), (json) => {
Object.assign(json.scripts, {
start: 'nx serve',
build: 'nx build',
test: 'nx test',
});
return json;
});
}
}
export async function workspaceGenerator(host: Tree, options: Schema) {
if (!options.name) {
throw new Error(`Invalid options, "name" is required.`);
}
options = normalizeOptions(options);
createFiles(host, options);
createPrettierrc(host, options);
if (options.cli === 'angular') {
decorateAngularClI(host, options);
}
if (options.packageManager === 'pnpm') {
createNpmrc(host, options);
}
setPresetProperty(host, options);
addNpmScripts(host, options);
createAppsAndLibsFolders(host, options);
await formatFiles(host);
formatWorkspaceJson(host, options);
}
export const workspaceSchematic = convertNxGenerator(workspaceGenerator);
function addPropertyWithStableKeys(obj: any, key: string, value: string) {
const copy = { ...obj };
Object.keys(obj).forEach((k) => {
delete obj[k];
});
obj[key] = value;
Object.keys(copy).forEach((k) => {
obj[k] = copy[k];
});
}
function normalizeOptions(options: Schema) {
let defaultBase = options.defaultBase || deduceDefaultBase();
return {
...options,
defaultBase,
};
}