Skip to content

Commit

Permalink
feat: create validate method
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Nov 1, 2023
1 parent 84097a8 commit 1f8fbf3
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 17 deletions.
58 changes: 49 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as yargs from "yargs";
import { build } from "./commands/build";
import { watch } from "./commands/watch";
import { validate } from "./commands/validate";

yargs
.scriptName("github-actions-wac")
Expand All @@ -18,4 +19,10 @@ yargs
{},
watch
)
.command(
"validate",
`Ensures "*.wac.ts" files are in sync with generated YAML files.`,
{},
validate
)
.help().argv;
9 changes: 1 addition & 8 deletions src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@ import * as fs from "fs";
import * as path from "path";
import jsYaml from "js-yaml";
import debug from "debug";
import { getWorkflowsPaths, clearImportCache } from "./utils";
import { getWorkflowsPaths, clearImportCache, TOP_YAML_WORKFLOW_COMMENT } from "./utils";
import * as tsNode from "ts-node";

const log = debug("ghawac");

const relativePath = p => path.relative(process.cwd(), p);

const TOP_YAML_WORKFLOW_COMMENT = [
"# This file was automatically generated by github-actions-wac.",
"# DO NOT MODIFY IT BY HAND. Instead, modify the source *.wac.ts file(s)",
'# and run "github-actions-wac build" (or "ghawac build") to regenerate this file.',
'# For more information, run "github-actions-wac --help".'
].join("\n");

let tsNodeRegistered = false;
const registerTsNode = (options = {}) => {
if (tsNodeRegistered) {
Expand Down
7 changes: 7 additions & 0 deletions src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export const clearImportCache = () => {
delete require.cache[key];
});
};

export const TOP_YAML_WORKFLOW_COMMENT = [
"# This file was automatically generated by github-actions-wac.",
"# DO NOT MODIFY IT BY HAND. Instead, modify the source *.wac.ts file(s)",
'# and run "github-actions-wac build" (or "ghawac build") to regenerate this file.',
'# For more information, run "github-actions-wac --help".'
].join("\n");
70 changes: 70 additions & 0 deletions src/commands/validate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as fs from "fs";
import * as path from "path";
import jsYaml from "js-yaml";
import debug from "debug";
import { getWorkflowsPaths, TOP_YAML_WORKFLOW_COMMENT } from "./utils";
import * as tsNode from "ts-node";

const log = debug("ghawac");

const relativePath = p => path.relative(process.cwd(), p);

let tsNodeRegistered = false;
const registerTsNode = (options = {}) => {
if (tsNodeRegistered) {
return;
}

tsNode.register({ ...options });
tsNodeRegistered = true;
};

export const validate = async () => {
registerTsNode();

const workflowFilesPaths = getWorkflowsPaths();
log(
"Detected following workflow files:\n",
workflowFilesPaths.map(item => `-> ${relativePath(item)}`).join("\n")
);

const invalidFiles = [];
for (let i = 0; i < workflowFilesPaths.length; i++) {
const tsWorkflowPath = workflowFilesPaths[i];
const exportedWorkflows = await import(tsWorkflowPath);
for (const name in exportedWorkflows) {
const yamlWorkflowPath = path.join(".github", "workflows", `${name}.yml`);

const yaml = [
TOP_YAML_WORKFLOW_COMMENT,
jsYaml.dump(exportedWorkflows[name], { noRefs: true })
].join("\n");

const existingYaml = fs.readFileSync(yamlWorkflowPath, "utf8");

if (yaml !== existingYaml) {
invalidFiles.push({
name,
tsWorkflowPath,
yamlWorkflowPath
});
}
}
}

if (invalidFiles.length === 0) {
console.log("All files are valid.");
return;
}

console.log("The following files are not valid:");
for (let i = 0; i < invalidFiles.length; i++) {
const { name, tsWorkflowPath, yamlWorkflowPath } = invalidFiles[i];
console.log(`‣ ${relativePath(tsWorkflowPath)} -> ${relativePath(yamlWorkflowPath)}`);
console.log(` ${name} is not in sync with ${relativePath(yamlWorkflowPath)}`);
}

console.log("\nPlease run `ghawac build` to fix the above issues.");

process.exit(1);
};

0 comments on commit 1f8fbf3

Please sign in to comment.