diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 37c76ac..b9c88e8 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,11 +4,13 @@
-
+
+
+
-
+
-
+
@@ -34,9 +36,9 @@
-
+ {
+ "associatedIndex": 6
+}
@@ -48,6 +50,7 @@
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
"git-widget-placeholder": "noRefs",
+ "last_opened_file_path": "/Users/adrian/dev/github-actions-wac/src/commands",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@@ -111,7 +114,8 @@
-
+
+
1656690553053
@@ -246,7 +250,39 @@
1657085218773
-
+
+
+ 1698760006797
+
+
+
+ 1698760006797
+
+
+
+ 1698760026275
+
+
+
+ 1698760026275
+
+
+
+ 1698760043626
+
+
+
+ 1698760043626
+
+
+
+ 1698760173213
+
+
+
+ 1698760173213
+
+
@@ -273,7 +309,11 @@
-
+
+
+
+
+
diff --git a/src/bin.ts b/src/bin.ts
index 31d3475..5d86c75 100644
--- a/src/bin.ts
+++ b/src/bin.ts
@@ -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")
@@ -18,4 +19,10 @@ yargs
{},
watch
)
+ .command(
+ "validate",
+ `Ensures "*.wac.ts" files are in sync with generated YAML files.`,
+ {},
+ validate
+ )
.help().argv;
diff --git a/src/commands/build.ts b/src/commands/build.ts
index 6e6eb34..4f83b09 100644
--- a/src/commands/build.ts
+++ b/src/commands/build.ts
@@ -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) {
diff --git a/src/commands/utils.ts b/src/commands/utils.ts
index 28aab63..4b6e003 100644
--- a/src/commands/utils.ts
+++ b/src/commands/utils.ts
@@ -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");
diff --git a/src/commands/validate.ts b/src/commands/validate.ts
new file mode 100644
index 0000000..4ebe6a4
--- /dev/null
+++ b/src/commands/validate.ts
@@ -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);
+};