-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
connect-to-nx-cloud.ts
79 lines (73 loc) · 2.2 KB
/
connect-to-nx-cloud.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
import { readNxJson } from '../project-graph/file-utils';
import { output } from '../utils/output';
import { getPackageManagerCommand } from '../utils/package-manager';
import { execSync } from 'child_process';
export async function connectToNxCloudUsingScan(scan: boolean): Promise<void> {
if (!scan) return;
const nxJson = readNxJson();
const defaultRunnerIsUsed = Object.values(nxJson.tasksRunnerOptions).find(
(r) =>
r.runner == '@nrwl/workspace/tasks-runners/default' ||
r.runner == 'nx/tasks-runners/default'
);
if (!defaultRunnerIsUsed) return;
output.log({
title: '--scan requires the workspace to be connected to Nx Cloud.',
});
const res = await connectToNxCloudPrompt();
if (res) {
const pmc = getPackageManagerCommand();
execSync(`${pmc.addDev} @nrwl/nx-cloud@latest`);
execSync(`${pmc.exec} nx g @nrwl/nx-cloud:init`, {
stdio: [0, 1, 2],
});
} else {
output.log({ title: 'Executing the command without --scan' });
}
}
export async function connectToNxCloudCommand(
promptOverride?: string
): Promise<void> {
const nxJson = readNxJson();
const nxCloudUsed = Object.values(nxJson.tasksRunnerOptions).find(
(r) => r.runner == '@nrwl/nx-cloud'
);
if (nxCloudUsed) {
output.log({
title: 'This workspace is already connected to Nx Cloud.',
});
return;
}
const res = await connectToNxCloudPrompt();
if (!res) return;
const pmc = getPackageManagerCommand();
execSync(`${pmc.addDev} @nrwl/nx-cloud@latest`);
execSync(`${pmc.exec} nx g @nrwl/nx-cloud:init`, {
stdio: [0, 1, 2],
});
}
async function connectToNxCloudPrompt(prompt?: string) {
return await (
await import('enquirer')
)
.prompt([
{
name: 'NxCloud',
message:
prompt ??
`Connect to Nx Cloud? (It's free and doesn't require registration.)`,
type: 'select',
choices: [
{
name: 'Yes',
hint: 'Faster builds, run details, GitHub integration. Learn more at https://nx.app',
},
{
name: 'No',
},
],
initial: 'Yes' as any,
},
])
.then((a: { NxCloud: 'Yes' | 'No' }) => a.NxCloud === 'Yes');
}