/
create-nx-plugin.ts
219 lines (189 loc) · 5.4 KB
/
create-nx-plugin.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
#!/usr/bin/env node
// we can't import from '@nrwl/workspace' because it will require typescript
import { getPackageManagerCommand } from 'nx/src/shared/package-manager';
import type { NxJsonConfiguration } from 'nx/src/shared/nx';
import { readJsonFile, writeJsonFile } from 'nx/src/utils/fileutils';
import { output } from '@nrwl/workspace/src/utilities/output';
import { execSync } from 'child_process';
import { removeSync } from 'fs-extra';
import * as path from 'path';
import { dirSync } from 'tmp';
import { showNxWarning } from './shared';
import {
detectInvokedPackageManager,
PackageManager,
} from './detect-invoked-package-manager';
import enquirer = require('enquirer');
import yargsParser = require('yargs-parser');
const tsVersion = 'TYPESCRIPT_VERSION';
const cliVersion = 'NX_VERSION';
const nxVersion = 'NX_VERSION';
const prettierVersion = 'PRETTIER_VERSION';
const parsedArgs = yargsParser(process.argv, {
string: ['pluginName', 'packageManager', 'importPath'],
alias: {
importPath: 'import-path',
pluginName: 'plugin-name',
packageManager: 'pm',
},
boolean: ['help'],
});
function createSandbox(packageManager: string) {
console.log(`Creating a sandbox with Nx...`);
const tmpDir = dirSync().name;
writeJsonFile(path.join(tmpDir, 'package.json'), {
dependencies: {
'@nrwl/workspace': nxVersion,
nx: cliVersion,
typescript: tsVersion,
prettier: prettierVersion,
},
license: 'MIT',
});
execSync(`${packageManager} install --silent`, {
cwd: tmpDir,
stdio: [0, 1, 2],
});
return tmpDir;
}
function createWorkspace(
tmpDir: string,
packageManager: PackageManager,
parsedArgs: any,
name: string
) {
// Ensure to use packageManager for args
// if it's not already passed in from previous process
if (!parsedArgs.packageManager) {
parsedArgs.packageManager = packageManager;
}
const args = [
name,
...process.argv.slice(parsedArgs._[2] ? 3 : 2).map((a) => `"${a}"`),
].join(' ');
const command = `new ${args} --preset=empty --collection=@nrwl/workspace`;
console.log(command);
const pmc = getPackageManagerCommand(packageManager);
execSync(
`${
pmc.exec
} nx ${command}/generators.json --nxWorkspaceRoot="${process.cwd()}"`,
{
stdio: [0, 1, 2],
cwd: tmpDir,
}
);
execSync(`${packageManager} add -D @nrwl/nx-plugin@${nxVersion}`, {
cwd: name,
stdio: [0, 1, 2],
});
}
function createNxPlugin(
workspaceName,
pluginName,
packageManager,
parsedArgs: any
) {
const importPath = parsedArgs.importPath ?? `${workspaceName}/${pluginName}`;
const command = `nx generate @nrwl/nx-plugin:plugin ${pluginName} --importPath=${importPath}`;
console.log(command);
const pmc = getPackageManagerCommand(packageManager);
execSync(`${pmc.exec} ${command}`, {
cwd: workspaceName,
stdio: [0, 1, 2],
});
}
function updateWorkspace(workspaceName: string) {
const nxJsonPath = path.join(workspaceName, 'nx.json');
const nxJson = readJsonFile<NxJsonConfiguration>(nxJsonPath);
nxJson.workspaceLayout = {
appsDir: 'e2e',
libsDir: 'packages',
};
writeJsonFile(nxJsonPath, nxJson);
removeSync(path.join(workspaceName, 'apps'));
removeSync(path.join(workspaceName, 'libs'));
}
function commitChanges(workspaceName) {
execSync('git add .', {
cwd: workspaceName,
stdio: 'ignore',
});
execSync('git commit --amend --no-edit', {
cwd: workspaceName,
stdio: 'ignore',
});
}
function determineWorkspaceName(parsedArgs: any): Promise<string> {
const workspaceName: string = parsedArgs._[2];
if (workspaceName) {
return Promise.resolve(workspaceName);
}
return enquirer
.prompt([
{
name: 'WorkspaceName',
message: `Workspace name (e.g., org name) `,
type: 'input',
},
])
.then((a: { WorkspaceName: string }) => {
if (!a.WorkspaceName) {
output.error({
title: 'Invalid workspace name',
bodyLines: [`Workspace name cannot be empty`],
});
process.exit(1);
}
return a.WorkspaceName;
});
}
function determinePluginName(parsedArgs) {
if (parsedArgs.pluginName) {
return Promise.resolve(parsedArgs.pluginName);
}
return enquirer
.prompt([
{
name: 'PluginName',
message: `Plugin name `,
type: 'input',
},
])
.then((a: { PluginName: string }) => {
if (!a.PluginName) {
output.error({
title: 'Invalid name',
bodyLines: [`Name cannot be empty`],
});
process.exit(1);
}
return a.PluginName;
});
}
function showHelp() {
console.log(`
Usage: <name> [options]
Create a new Nx workspace
Args:
name workspace name (e.g., org name)
Options:
pluginName the name of the plugin to be created
`);
}
if (parsedArgs.help) {
showHelp();
process.exit(0);
}
const packageManager: PackageManager =
parsedArgs.packageManager || detectInvokedPackageManager();
determineWorkspaceName(parsedArgs).then((workspaceName) => {
return determinePluginName(parsedArgs).then((pluginName) => {
const tmpDir = createSandbox(packageManager);
createWorkspace(tmpDir, packageManager, parsedArgs, workspaceName);
updateWorkspace(workspaceName);
createNxPlugin(workspaceName, pluginName, packageManager, parsedArgs);
commitChanges(workspaceName);
showNxWarning(workspaceName);
});
});