forked from nrwl/nx
/
convert-to-nx-project.ts
91 lines (79 loc) · 2.41 KB
/
convert-to-nx-project.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
import {
convertNxGenerator,
formatFiles,
getProjects,
getWorkspacePath,
logger,
normalizePath,
ProjectConfiguration,
readProjectConfiguration,
readWorkspaceConfiguration,
Tree,
updateJson,
writeJson,
} from '@nrwl/devkit';
import { prompt } from 'enquirer';
import { getRelativeProjectJsonSchemaPath } from 'nx/src/generators/utils/project-configuration';
import { dirname } from 'path';
import { Schema } from './schema';
import { getProjectConfigurationPath } from './utils/get-project-configuration-path';
export const SCHEMA_OPTIONS_ARE_MUTUALLY_EXCLUSIVE =
'--project and --all are mutually exclusive';
export async function validateSchema(schema: Schema) {
if (schema.project && schema.all) {
throw SCHEMA_OPTIONS_ARE_MUTUALLY_EXCLUSIVE;
}
if (!schema.project && !schema.all) {
schema.project = (
await prompt<{ project: string }>([
{
message: 'What project should be converted?',
type: 'input',
name: 'project',
},
])
).project;
}
}
export async function convertToNxProjectGenerator(host: Tree, schema: Schema) {
const workspace = readWorkspaceConfiguration(host);
if (workspace.version < 2) {
logger.error(`
NX Only workspaces with version 2+ support project.json files.
To upgrade change the version number at the top of ${getWorkspacePath(
host
)} and run 'nx format'.
`);
throw new Error('v2+ Required');
}
await validateSchema(schema);
const projects = schema.all
? getProjects(host).entries()
: ([[schema.project, readProjectConfiguration(host, schema.project)]] as [
string,
ProjectConfiguration
][]);
for (const [project, configuration] of projects) {
const configPath = getProjectConfigurationPath(configuration);
if (host.exists(configPath)) {
logger.warn(`Skipping ${project} since ${configPath} already exists.`);
continue;
}
writeJson(host, configPath, {
$schema: getRelativeProjectJsonSchemaPath(host, configuration),
...configuration,
root: undefined,
});
updateJson(host, getWorkspacePath(host), (value) => {
value.projects[project] = normalizePath(dirname(configPath));
return value;
});
}
if (!schema.skipFormat) {
await formatFiles(host);
}
}
export default convertToNxProjectGenerator;
export const convertToNxProjectSchematic = convertNxGenerator(
convertToNxProjectGenerator
);