-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
141 lines (133 loc) · 3.99 KB
/
index.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
import CircleCI from "@circleci/circleci-config-sdk";
import fs from "fs";
import glob from "glob";
import path from "path";
/**
* Job names you can pass to createConfig().
* These already have jobs created for them.
*/
export const JobNames = {
E2eTest: "e2e-test",
JsLint: "js-lint",
JsTest: "js-test",
PhpLint: "php-lint",
PhpTest: "php-test",
Vsix: "vsix",
Zip: "zip",
};
const nodeExecutor = new CircleCI.executors.DockerExecutor(
"cimg/node:lts",
"large"
);
const preCreatedJobs = [
new CircleCI.Job(JobNames.JsLint, nodeExecutor, [
new CircleCI.commands.Checkout(),
new CircleCI.commands.Run({
command: "npm ci && npm run lint",
}),
]),
new CircleCI.Job(JobNames.JsTest, nodeExecutor, [
new CircleCI.commands.Checkout(),
new CircleCI.commands.Run({ command: "npm ci && npm test" }),
]),
new CircleCI.Job(
JobNames.PhpLint,
new CircleCI.executors.DockerExecutor("cimg/php:8.0", "large"),
[
new CircleCI.commands.Checkout(),
new CircleCI.commands.Run({
command: "composer i && composer lint",
}),
]
),
new CircleCI.Job(
JobNames.PhpTest,
new CircleCI.executors.DockerExecutor("cimg/php:8.1", "large"),
[
new CircleCI.commands.Checkout(),
new CircleCI.commands.Run({ command: "composer i && composer test" }),
]
),
new CircleCI.Job(
JobNames.E2eTest,
new CircleCI.executors.MachineExecutor("large", "ubuntu-2004:202111-02"),
[
new CircleCI.commands.Checkout(),
new CircleCI.commands.Run({ command: "npm ci" }),
new CircleCI.commands.Run({
name: "Running e2e tests",
command: "npm run wp-env start && npm run test:e2e",
}),
new CircleCI.commands.StoreArtifacts({ path: "artifacts" }),
]
),
new CircleCI.Job(
JobNames.Vsix,
new CircleCI.executors.DockerExecutor("cimg/node:16.8.0-browsers", "large"),
[
new CircleCI.commands.Checkout(),
new CircleCI.commands.Run({ command: "npm ci && npm run vsix" }),
new CircleCI.commands.Run({
command: `mkdir /tmp/artifacts
mv ${
JSON.parse(
fs.existsSync("../../package.json")
? fs.readFileSync("../../package.json")?.toString()
: "{}"
).name
}*.vsix /tmp/artifacts`,
}),
new CircleCI.commands.StoreArtifacts({ path: "/tmp/artifacts" }),
]
),
new CircleCI.Job(
JobNames.Zip,
new CircleCI.executors.DockerExecutor("cimg/php:8.0"),
[
new CircleCI.commands.Checkout(),
new CircleCI.commands.Run({ command: "composer zip" }),
new CircleCI.commands.StoreArtifacts({
path: `${path.basename(
glob.sync("../../*.php")?.[0] ?? "",
".php"
)}.zip`,
}),
]
),
];
/** Creates PHP test jobs, given the passed PHP versions. */
export function createPhpTestJobs(...phpVersions: string[]): CircleCI.Job[] {
return phpVersions.map((phpVersion) => {
return new CircleCI.Job(
`php-test-${phpVersion.replace(".", "-")}`,
new CircleCI.executors.DockerExecutor(`cimg/php:${phpVersion}`),
[
new CircleCI.commands.Checkout(),
new CircleCI.commands.Run({ command: "composer i && composer test" }),
]
);
});
}
/** Not needed for the public API, simply use createConfig(). */
export function getJobs(...jobs: (string | CircleCI.Job)[]): CircleCI.Job[] {
return jobs.map((job) => {
return typeof job === "string"
? preCreatedJobs.find((preCreatedJob) => {
return job === preCreatedJob.name;
})
: job;
});
}
/** Creates and writes the config, given the passed jobs. */
export function createConfig(...jobs: (string | CircleCI.Job)[]) {
const config = new CircleCI.Config();
const workflow = new CircleCI.Workflow("test-lint");
config.addWorkflow(workflow);
getJobs(...jobs).forEach((job) => {
if (job) {
config.addJob(job);
workflow.addJob(job);
}
});
fs.writeFile("./dynamicConfig.yml", config.stringify(), () => {});
}