/
create-preset.ts
62 lines (54 loc) · 1.68 KB
/
create-preset.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
import { CreateWorkspaceOptions } from './create-workspace-options';
import { output } from './utils/output';
import {
getPackageManagerCommand,
getPackageManagerVersion,
PackageManager,
} from './utils/package-manager';
import { spawnAndWait } from './utils/child-process-utils';
import { unparse } from './utils/unparse';
export async function createPreset<T extends CreateWorkspaceOptions>(
preset: string,
parsedArgs: T,
packageManager: PackageManager,
directory: string
): Promise<void> {
const { skipGit, ci, commit, nxCloud, ...restArgs } = parsedArgs;
const args = unparse({
...restArgs,
}).join(' ');
const pmc = getPackageManagerCommand(packageManager);
const workingDir = process.cwd().replace(/\\/g, '/');
let nxWorkspaceRoot = `"${workingDir}"`;
// If path contains spaces there is a problem in Windows for npm@6.
// In this case we have to escape the wrapping quotes.
if (
process.platform === 'win32' &&
/\s/.test(nxWorkspaceRoot) &&
packageManager === 'npm'
) {
const pmVersion = +getPackageManagerVersion(packageManager).split('.')[0];
if (pmVersion < 7) {
nxWorkspaceRoot = `\\"${nxWorkspaceRoot.slice(1, -1)}\\"`;
}
}
const command = `g ${preset}:preset --quiet ${args}`;
try {
const [exec, ...args] = pmc.exec.split(' ');
args.push(
'nx',
`--nxWorkspaceRoot=${nxWorkspaceRoot}`,
...command.split(' ')
);
await spawnAndWait(exec, args, directory);
output.log({
title: `Successfully applied preset: ${preset}.`,
});
} catch (e) {
output.error({
title: `Failed to apply preset: ${preset}`,
bodyLines: ['See above'],
});
process.exit(1);
}
}