Skip to content

Commit

Permalink
Refactor how Gat compiles templates
Browse files Browse the repository at this point in the history
A new way how Gat handles the template generation.
  • Loading branch information
fcsonline committed Nov 3, 2023
1 parent 689c1fc commit cb6c993
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 66 deletions.
3 changes: 1 addition & 2 deletions .github/templates/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const setupNodeStep: UseStep = {
uses: "actions/setup-node@v3",
};

new Workflow("Build")
export default new Workflow("Build")
.on("push", { branches: ["main"] })
.on("pull_request")
.addJob("build", {
Expand All @@ -31,4 +31,3 @@ new Workflow("Build")
{ name: "Check format problems", run: "npm run format:check" },
],
})
.compile();
3 changes: 3 additions & 0 deletions .github/templates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Build from './build'

Build.compile('build.yml')
1 change: 0 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ jobs:
run: npm run lint
- name: Check format problems
run: npm run format:check

2 changes: 1 addition & 1 deletion src/__snapshots__/workflow.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
// Vitest Snapshot v1

exports[`Workflow > allows a job matrix 1`] = `
"# Workflow automatically generated by gat
Expand Down
62 changes: 3 additions & 59 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,27 @@ import path from "path";
import { exec } from "child_process";
import { Command } from "commander";
import { promisify } from "util";
import debounce from "lodash/debounce";

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

const parseFile = async (templateFile: string) => {
// NOTE: can we improve this using ts-node or typescript programatically?
const { stdout } = await execPromise(
`npx ts-node ${
process.env["GAT_BUILD_FLAGS"] ?? "--swc -T"
} ${templateFile}`
);

await writeFilePromise(
path.join(
process.cwd(),
".github",
"workflows",
templateFile.split("/").at(-1)!.replace(".ts", ".yml")
),
stdout
);
};

const cli = new Command();

cli
.version("1.0.0")
.version("2.0.0")
.description("Write your GitHub Actions workflows using TypeScript");

cli
.command("build")
.description("Transpile all Gat templates into GitHub Actions workflows.")
.argument("[file]", "(Optional) A Gat template file")
.action(async (file) => {
.action(async () => {
if (!fs.existsSync(path.join(folder, "..", "workflows"))) {
fs.mkdirSync(path.join(folder, "..", "workflows"));
}

if (file !== undefined) {
await parseFile(file);
} else {
await Promise.all(
fs.readdirSync(folder).map(async (templateFile) => {
if (!templateFile.match(/^shared$/)) {
await parseFile(`${path.join(folder, templateFile)}`);
}
})
);
}
await execPromise(`npx ts-node ${process.env["GAT_BUILD_FLAGS"] ?? "--swc -T"} ${path.join(folder, 'index.ts')}`);

process.exit(0);
});

cli
.command("watch")
.description(
"Watch file changes in your Gat templates folder and transpile them automatically."
)
.action(async () => {
const parseWatchedFile = debounce(async (fileName) => {
const start = process.hrtime.bigint();
process.stdout.write(
`😸 Detected change on file ${fileName}. Transpiling... `
);
await parseFile(path.join(folder, fileName.toString()));
console.log(
`Done in ${(
Number(process.hrtime.bigint() - start) / 1_000_000
).toFixed(2)}ms`
);
}, 1000);
console.log(`😼 Watching file changes on ${folder}...`);
fs.watch(folder).on("change", (_eventName, fileName) => {
parseWatchedFile(fileName);
});
});

cli.parse(process.argv);
19 changes: 16 additions & 3 deletions src/workflow.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { dump } from "js-yaml";
import kebabCase from "lodash/kebabCase";
import fs from "fs";
import path from "path";
import { promisify } from "util";

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

const writeFilePromise = promisify(fs.writeFile);

const DEFAULT_RUNNERS = ["ubuntu-22.04"];

interface DefaultOptions {
Expand Down Expand Up @@ -73,7 +78,7 @@ export class Workflow<
return isSelfHosted ? ["self-hosted", runnerName] : runnerName;
}

compile() {
compile(filepath?: string) {
const result = {
name: this.name,
on: Object.fromEntries(
Expand Down Expand Up @@ -187,8 +192,16 @@ export class Workflow<
}
)}`;

console.log(compiled);
if (!filepath) return compiled

return compiled;
return writeFilePromise(
path.join(
process.cwd(),
".github",
"workflows",
filepath
),
compiled
);
}
}

0 comments on commit cb6c993

Please sign in to comment.