forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project-configuration.ts
415 lines (384 loc) · 11.5 KB
/
project-configuration.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import type { Tree } from '@nrwl/tao/src/shared/tree';
import {
ProjectConfiguration,
RawWorkspaceJsonConfiguration,
toNewFormat,
WorkspaceJsonConfiguration,
} from '@nrwl/tao/src/shared/workspace';
import { readJson, updateJson, writeJson } from '../utils/json';
import type {
NxJsonConfiguration,
NxJsonProjectConfiguration,
} from '@nrwl/tao/src/shared/nx';
import {
getWorkspaceLayout,
getWorkspacePath,
} from '../utils/get-workspace-layout';
import { joinPathFragments } from '../utils/path';
export type WorkspaceConfiguration = Omit<
WorkspaceJsonConfiguration,
'projects'
> &
Omit<NxJsonConfiguration, 'projects'>;
/**
* Adds project configuration to the Nx workspace.
*
* The project configuration is stored in workspace.json and nx.json. The utility will update
* both files.
*
* @param host - the file system tree
* @param projectName - unique name. Often directories are part of the name (e.g., mydir-mylib)
* @param projectConfiguration - project configuration
* @param standalone - should the project use package.json? If false, the project config is inside workspace.json
*/
export function addProjectConfiguration(
host: Tree,
projectName: string,
projectConfiguration: ProjectConfiguration & NxJsonProjectConfiguration,
standalone: boolean = false
): void {
standalone = standalone || getWorkspaceLayout(host).standaloneAsDefault;
setProjectConfiguration(
host,
projectName,
projectConfiguration,
'create',
standalone
);
}
/**
* Updates the configuration of an existing project.
*
* The project configuration is stored in workspace.json and nx.json. The utility will update
* both files.
*
* @param host - the file system tree
* @param projectName - unique name. Often directories are part of the name (e.g., mydir-mylib)
* @param projectConfiguration - project configuration
*/
export function updateProjectConfiguration(
host: Tree,
projectName: string,
projectConfiguration: ProjectConfiguration & NxJsonProjectConfiguration
): void {
setProjectConfiguration(host, projectName, projectConfiguration, 'update');
}
/**
* Removes the configuration of an existing project.
*
* The project configuration is stored in workspace.json and nx.json.
* The utility will update both files.
*/
export function removeProjectConfiguration(
host: Tree,
projectName: string
): void {
setProjectConfiguration(host, projectName, undefined, 'delete');
}
/**
* Get a map of all projects in a workspace.
*
* Use {@link readProjectConfiguration} if only one project is needed.
*/
export function getProjects(
host: Tree
): Map<string, ProjectConfiguration & NxJsonProjectConfiguration> {
const workspace = readWorkspace(host);
const nxJson = readJson<NxJsonConfiguration>(host, 'nx.json');
return new Map(
Object.keys(workspace.projects || {}).map((projectName) => {
return [
projectName,
getProjectConfiguration(host, projectName, workspace, nxJson),
];
})
);
}
/**
* Read general workspace configuration such as the default project or cli settings
*
* This does _not_ provide projects configuration, use {@link readProjectConfiguration} instead.
*/
export function readWorkspaceConfiguration(host: Tree): WorkspaceConfiguration {
const workspace = readWorkspace(host);
delete workspace.projects;
const nxJson = readJson<NxJsonConfiguration>(host, 'nx.json');
delete nxJson.projects;
return {
...workspace,
...nxJson,
};
}
/**
* Update general workspace configuration such as the default project or cli settings.
*
* This does _not_ update projects configuration, use {@link updateProjectConfiguration} or {@link addProjectConfiguration} instead.
*/
export function updateWorkspaceConfiguration(
host: Tree,
workspaceConfig: WorkspaceConfiguration
): void {
const {
// Angular Json Properties
version,
cli,
defaultProject,
generators,
// Nx Json Properties
implicitDependencies,
plugins,
npmScope,
targetDependencies,
workspaceLayout,
tasksRunnerOptions,
affected,
} = workspaceConfig;
const workspace: Omit<Required<WorkspaceJsonConfiguration>, 'projects'> = {
version,
cli,
defaultProject,
generators,
};
const nxJson: Omit<Required<NxJsonConfiguration>, 'projects'> = {
implicitDependencies,
plugins,
npmScope,
targetDependencies,
workspaceLayout,
tasksRunnerOptions,
affected,
};
updateJson<WorkspaceJsonConfiguration>(
host,
getWorkspacePath(host),
(json) => {
return { ...json, ...workspace };
}
);
updateJson<NxJsonConfiguration>(host, 'nx.json', (json) => {
return { ...json, ...nxJson };
});
}
/**
* Reads a project configuration.
*
* The project configuration is stored in workspace.json and nx.json. The utility will read
* both files.
*
* @param host - the file system tree
* @param projectName - unique name. Often directories are part of the name (e.g., mydir-mylib)
* @throws If supplied projectName cannot be found
*/
export function readProjectConfiguration(
host: Tree,
projectName: string
): ProjectConfiguration & NxJsonProjectConfiguration {
const workspace = readWorkspace(host);
if (!workspace.projects[projectName]) {
throw new Error(
`Cannot find configuration for '${projectName}' in ${getWorkspacePath(
host
)}.`
);
}
const nxJson = readJson<NxJsonConfiguration>(host, 'nx.json');
// TODO: Remove after confirming that nx.json should be optional.
// if (!nxJson.projects[projectName]) {
// throw new Error(
// `Cannot find configuration for '${projectName}' in nx.json`
// );
// }
return getProjectConfiguration(host, projectName, workspace, nxJson);
}
/**
* Returns if a project has a standalone configuration (project.json).
*
* @param host - the file system tree
* @param project - the project name
*/
export function isStandaloneProject(host: Tree, project: string): boolean {
const rawWorkspace = readJson<RawWorkspaceJsonConfiguration>(
host,
getWorkspacePath(host)
);
const projectConfig = rawWorkspace.projects?.[project];
return typeof projectConfig === 'string';
}
function getProjectConfiguration(
host: Tree,
projectName: string,
workspace: WorkspaceJsonConfiguration,
nxJson: NxJsonConfiguration
): ProjectConfiguration & NxJsonProjectConfiguration {
return {
...readWorkspaceSection(host, workspace, projectName),
...readNxJsonSection(nxJson, projectName),
};
}
function readWorkspaceSection(
host: Tree,
workspace: WorkspaceJsonConfiguration,
projectName: string
) {
const config = workspace.projects[projectName];
return config;
}
function readNxJsonSection(nxJson: NxJsonConfiguration, projectName: string) {
return nxJson.projects[projectName];
}
function setProjectConfiguration(
host: Tree,
projectName: string,
projectConfiguration: ProjectConfiguration & NxJsonProjectConfiguration,
mode: 'create' | 'update' | 'delete',
standalone: boolean = false
) {
if (mode === 'delete') {
addProjectToNxJson(host, projectName, undefined, mode);
addProjectToWorkspaceJson(host, projectName, undefined, mode);
return;
}
if (!projectConfiguration) {
throw new Error(
`Cannot ${mode} "${projectName}" with value ${projectConfiguration}`
);
}
const { tags, implicitDependencies } = projectConfiguration;
addProjectToWorkspaceJson(
host,
projectName,
projectConfiguration,
mode,
standalone
);
addProjectToNxJson(
host,
projectName,
{
tags,
implicitDependencies,
},
mode
);
}
function addProjectToWorkspaceJson(
host: Tree,
projectName: string,
project: ProjectConfiguration & NxJsonProjectConfiguration,
mode: 'create' | 'update' | 'delete',
standalone: boolean = false
) {
const path = getWorkspacePath(host);
const workspaceJson = readJson<RawWorkspaceJsonConfiguration>(host, path);
validateWorkspaceJsonOperations(mode, workspaceJson, projectName);
const configFile =
mode === 'create' && standalone
? joinPathFragments(project.root, 'project.json')
: getProjectFileLocation(host, projectName);
if (configFile) {
if (mode === 'delete') {
host.delete(configFile);
delete workspaceJson.projects[projectName];
} else {
writeJson(host, configFile, project);
}
if (mode === 'create') {
workspaceJson.projects[projectName] = project.root;
}
} else {
let workspaceConfiguration: ProjectConfiguration;
if (project) {
const { tags, implicitDependencies, ...c } = project;
workspaceConfiguration = c;
}
workspaceJson.projects[projectName] = workspaceConfiguration;
}
writeJson(host, path, workspaceJson);
}
function addProjectToNxJson(
host: Tree,
projectName: string,
config: NxJsonProjectConfiguration,
mode: 'create' | 'update' | 'delete'
) {
// distributed project files do not use nx.json,
// so only proceed if the project does not use them.
if (!getProjectFileLocation(host, projectName)) {
const nxJson = readJson<NxJsonConfiguration>(host, 'nx.json');
if (mode === 'delete') {
delete nxJson.projects[projectName];
} else {
nxJson.projects[projectName] = {
...{
tags: [],
},
...(config || {}),
};
}
writeJson(host, 'nx.json', nxJson);
}
}
function readWorkspace(host: Tree): WorkspaceJsonConfiguration {
const workspaceJson = inlineProjectConfigurationsWithTree(host);
const originalVersion = workspaceJson.version;
return {
...toNewFormat(workspaceJson),
version: originalVersion,
};
}
/**
* This has to be separate from the inline functionality inside tao,
* as the functionality in tao does not use a Tree. Changes made during
* a generator would not be present during runtime execution.
* @returns
*/
function inlineProjectConfigurationsWithTree(
host: Tree
): WorkspaceJsonConfiguration {
const path = getWorkspacePath(host);
const workspaceJson = readJson<RawWorkspaceJsonConfiguration>(host, path);
Object.entries(workspaceJson.projects || {}).forEach(([project, config]) => {
if (typeof config === 'string') {
const configFileLocation = joinPathFragments(config, 'project.json');
workspaceJson.projects[project] = readJson<
ProjectConfiguration & NxJsonProjectConfiguration
>(host, configFileLocation);
}
});
return workspaceJson as WorkspaceJsonConfiguration;
}
/**
* @description Determine where a project's configuration is located.
* @returns file path if separate from root config, null otherwise.
*/
function getProjectFileLocation(host: Tree, project: string): string | null {
const rawWorkspace = readJson<RawWorkspaceJsonConfiguration>(
host,
getWorkspacePath(host)
);
const projectConfig = rawWorkspace.projects?.[project];
return typeof projectConfig === 'string'
? joinPathFragments(projectConfig, 'project.json')
: null;
}
function validateWorkspaceJsonOperations(
mode: 'create' | 'update' | 'delete',
workspaceJson: RawWorkspaceJsonConfiguration | WorkspaceJsonConfiguration,
projectName: string
) {
if (mode == 'create' && workspaceJson.projects[projectName]) {
throw new Error(
`Cannot create Project '${projectName}'. It already exists.`
);
}
if (mode == 'update' && !workspaceJson.projects[projectName]) {
throw new Error(
`Cannot update Project '${projectName}'. It does not exist.`
);
}
if (mode == 'delete' && !workspaceJson.projects[projectName]) {
throw new Error(
`Cannot delete Project '${projectName}'. It does not exist.`
);
}
}