Skip to content

Commit f3b2c45

Browse files
authored
Merge pull request #4 from factorialco/feature/refactor-compile
Refactor how Gat compiles templates
2 parents 689c1fc + a4475ed commit f3b2c45

File tree

9 files changed

+42
-78
lines changed

9 files changed

+42
-78
lines changed

.github/templates/build.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const setupNodeStep: UseStep = {
88
uses: "actions/setup-node@v3",
99
};
1010

11-
new Workflow("Build")
11+
export default new Workflow("Build")
1212
.on("push", { branches: ["main"] })
1313
.on("pull_request")
1414
.addJob("build", {
@@ -30,5 +30,4 @@ new Workflow("Build")
3030
{ name: "Check lint problems", run: "npm run lint" },
3131
{ name: "Check format problems", run: "npm run format:check" },
3232
],
33-
})
34-
.compile();
33+
});

.github/templates/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Build from "./build";
2+
3+
Build.compile("build.yml");

.github/workflows/build.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ jobs:
2828
run: npm run lint
2929
- name: Check format problems
3030
run: npm run format:check
31-

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@factorialco/gat",
3-
"version": "1.5.0",
3+
"version": "2.0.0",
44
"description": "Write your GitHub Actions workflows using TypeScript",
55
"bin": {
66
"gat": "dist/cli.js"

src/__snapshots__/workflow.spec.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
1+
// Vitest Snapshot v1
22

33
exports[`Workflow > allows a job matrix 1`] = `
44
"# Workflow automatically generated by gat

src/cli.ts

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,83 +4,32 @@ import path from "path";
44
import { exec } from "child_process";
55
import { Command } from "commander";
66
import { promisify } from "util";
7-
import debounce from "lodash/debounce";
87

98
const execPromise = promisify(exec);
10-
const writeFilePromise = promisify(fs.writeFile);
119
const folder = path.join(process.cwd(), ".github", "templates");
1210

13-
const parseFile = async (templateFile: string) => {
14-
// NOTE: can we improve this using ts-node or typescript programatically?
15-
const { stdout } = await execPromise(
16-
`npx ts-node ${
17-
process.env["GAT_BUILD_FLAGS"] ?? "--swc -T"
18-
} ${templateFile}`
19-
);
20-
21-
await writeFilePromise(
22-
path.join(
23-
process.cwd(),
24-
".github",
25-
"workflows",
26-
templateFile.split("/").at(-1)!.replace(".ts", ".yml")
27-
),
28-
stdout
29-
);
30-
};
31-
3211
const cli = new Command();
3312

3413
cli
35-
.version("1.0.0")
14+
.version("2.0.0")
3615
.description("Write your GitHub Actions workflows using TypeScript");
3716

3817
cli
3918
.command("build")
4019
.description("Transpile all Gat templates into GitHub Actions workflows.")
41-
.argument("[file]", "(Optional) A Gat template file")
42-
.action(async (file) => {
20+
.action(async () => {
4321
if (!fs.existsSync(path.join(folder, "..", "workflows"))) {
4422
fs.mkdirSync(path.join(folder, "..", "workflows"));
4523
}
4624

47-
if (file !== undefined) {
48-
await parseFile(file);
49-
} else {
50-
await Promise.all(
51-
fs.readdirSync(folder).map(async (templateFile) => {
52-
if (!templateFile.match(/^shared$/)) {
53-
await parseFile(`${path.join(folder, templateFile)}`);
54-
}
55-
})
56-
);
57-
}
25+
await execPromise(
26+
`npx ts-node ${process.env["GAT_BUILD_FLAGS"] ?? "--swc -T"} ${path.join(
27+
folder,
28+
"index.ts"
29+
)}`
30+
);
5831

5932
process.exit(0);
6033
});
6134

62-
cli
63-
.command("watch")
64-
.description(
65-
"Watch file changes in your Gat templates folder and transpile them automatically."
66-
)
67-
.action(async () => {
68-
const parseWatchedFile = debounce(async (fileName) => {
69-
const start = process.hrtime.bigint();
70-
process.stdout.write(
71-
`😸 Detected change on file ${fileName}. Transpiling... `
72-
);
73-
await parseFile(path.join(folder, fileName.toString()));
74-
console.log(
75-
`Done in ${(
76-
Number(process.hrtime.bigint() - start) / 1_000_000
77-
).toFixed(2)}ms`
78-
);
79-
}, 1000);
80-
console.log(`😼 Watching file changes on ${folder}...`);
81-
fs.watch(folder).on("change", (_eventName, fileName) => {
82-
parseWatchedFile(fileName);
83-
});
84-
});
85-
8635
cli.parse(process.argv);

src/event.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type EventName =
66
| "workflow_dispatch"
77
| "schedule"
88
| "pull_request_target"
9-
| "repository_dispatch"
9+
| "repository_dispatch";
1010

1111
export type EventOptions<T extends EventName> = T extends "push"
1212
? PushEventOptions
@@ -60,7 +60,7 @@ interface WorkflowDispatchEventOptions {
6060
type ScheduleEventOptions = Array<{ cron: string }>;
6161

6262
interface RepositoryDispatchEventOptions {
63-
types: string[]
63+
types: string[];
6464
}
6565

6666
export interface Event {

src/workflow.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { dump } from "js-yaml";
22
import kebabCase from "lodash/kebabCase";
3+
import fs from "fs";
4+
import path from "path";
5+
import { promisify } from "util";
36

47
import { ConcurrencyGroup, Job, JobOptions, StringWithNoSpaces } from "./job";
58
import type { Event, EventName, EventOptions } from "./event";
69
import { BaseStep, Step } from "./step";
710

11+
const writeFilePromise = promisify(fs.writeFile);
12+
813
const DEFAULT_RUNNERS = ["ubuntu-22.04"];
914

1015
interface DefaultOptions {
@@ -73,7 +78,7 @@ export class Workflow<
7378
return isSelfHosted ? ["self-hosted", runnerName] : runnerName;
7479
}
7580

76-
compile() {
81+
compile(filepath?: string) {
7782
const result = {
7883
name: this.name,
7984
on: Object.fromEntries(
@@ -136,12 +141,18 @@ export class Workflow<
136141
strategy: matrix
137142
? {
138143
"fail-fast": false,
139-
matrix: typeof matrix === 'string' ? matrix : {
140-
...Object.fromEntries(
141-
matrix.elements.map(({ id, options }) => [id, options])
142-
),
143-
include: matrix.extra,
144-
},
144+
matrix:
145+
typeof matrix === "string"
146+
? matrix
147+
: {
148+
...Object.fromEntries(
149+
matrix.elements.map(({ id, options }) => [
150+
id,
151+
options,
152+
])
153+
),
154+
include: matrix.extra,
155+
},
145156
}
146157
: undefined,
147158
env,
@@ -187,8 +198,11 @@ export class Workflow<
187198
}
188199
)}`;
189200

190-
console.log(compiled);
201+
if (!filepath) return compiled;
191202

192-
return compiled;
203+
return writeFilePromise(
204+
path.join(process.cwd(), ".github", "workflows", filepath),
205+
compiled
206+
);
193207
}
194208
}

0 commit comments

Comments
 (0)