-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
ci-workflow.ts
78 lines (69 loc) · 2.12 KB
/
ci-workflow.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
import {
Tree,
names,
generateFiles,
getPackageManagerCommand,
readJson,
NxJsonConfiguration,
formatFiles,
writeJson,
} from '@nx/devkit';
import { deduceDefaultBase } from '../../utilities/default-base';
import { join } from 'path';
export interface Schema {
name: string;
ci: 'github' | 'azure' | 'circleci' | 'bitbucket-pipelines' | 'gitlab';
}
export async function ciWorkflowGenerator(host: Tree, schema: Schema) {
const ci = schema.ci;
const options = normalizeOptions(schema);
const nxJson: NxJsonConfiguration = readJson(host, 'nx.json');
const nxCloudUsed =
nxJson.nxCloudAccessToken ??
Object.values(nxJson.tasksRunnerOptions ?? {}).find(
(r) => r.runner == '@nrwl/nx-cloud' || r.runner == 'nx-cloud'
);
if (!nxCloudUsed) {
throw new Error('This workspace is not connected to Nx Cloud.');
}
if (ci === 'bitbucket-pipelines' && defaultBranchNeedsOriginPrefix(nxJson)) {
writeJson(host, 'nx.json', appendOriginPrefix(nxJson));
}
generateFiles(host, join(__dirname, 'files', ci), '', options);
await formatFiles(host);
}
interface Substitutes {
mainBranch: string;
workflowName: string;
workflowFileName: string;
packageManagerInstall: string;
packageManagerPrefix: string;
tmpl: '';
}
function normalizeOptions(options: Schema): Substitutes {
const { name: workflowName, fileName: workflowFileName } = names(
options.name
);
const { exec: packageManagerPrefix, ciInstall: packageManagerInstall } =
getPackageManagerCommand();
return {
workflowName,
workflowFileName,
packageManagerInstall,
packageManagerPrefix,
mainBranch: deduceDefaultBase(),
tmpl: '',
};
}
function defaultBranchNeedsOriginPrefix(nxJson: NxJsonConfiguration): boolean {
return !nxJson.affected?.defaultBase?.startsWith('origin/');
}
function appendOriginPrefix(nxJson: NxJsonConfiguration): NxJsonConfiguration {
if (nxJson?.affected?.defaultBase) {
nxJson.affected.defaultBase = `origin/${nxJson.affected.defaultBase}`;
}
if (nxJson.base || !nxJson.affected) {
nxJson.base = `origin/${nxJson.base ?? deduceDefaultBase()}`;
}
return nxJson;
}