forge-workflow 1.4.9 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,303 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Forge Validate CLI
5
+ *
6
+ * Prerequisite validation for workflow stages.
7
+ * Helps ensure developers have required tools and files before proceeding.
8
+ *
9
+ * Usage:
10
+ * forge-validate status - Check project prerequisites
11
+ * forge-validate dev - Validate before /dev stage
12
+ * forge-validate ship - Validate before /ship stage
13
+ *
14
+ * Security: Uses execFileSync to prevent command injection.
15
+ */
16
+
17
+ const { execFileSync } = require("node:child_process");
18
+ const fs = require("node:fs");
19
+ const path = require("node:path");
20
+
21
+ // Validation results
22
+ let checks = [];
23
+
24
+ function check(label, condition, message) {
25
+ const passed = typeof condition === "function" ? condition() : condition;
26
+ checks.push({ label, passed, message: passed ? "✓" : `✗ ${message}` });
27
+ return passed;
28
+ }
29
+
30
+ function printResults() {
31
+ console.log("\nValidation Results:\n");
32
+ checks.forEach(({ label, passed, message }) => {
33
+ const status = passed ? "✓" : "✗";
34
+ const color = passed ? "\x1b[32m" : "\x1b[31m"; // green : red
35
+ console.log(` ${color}${status}\x1b[0m ${label}`);
36
+ if (!passed) {
37
+ console.log(` ${message}`);
38
+ }
39
+ });
40
+ console.log();
41
+
42
+ const allPassed = checks.every((c) => c.passed);
43
+ if (allPassed) {
44
+ console.log("✅ All checks passed!\n");
45
+ } else {
46
+ console.log("❌ Some checks failed. Please fix the issues above.\n");
47
+ }
48
+
49
+ return allPassed;
50
+ }
51
+
52
+ // Validation functions
53
+
54
+ function validateStatus() {
55
+ console.log("Checking project prerequisites...\n");
56
+
57
+ check(
58
+ "Git repository",
59
+ () => {
60
+ return fs.existsSync(".git") || fs.existsSync("../.git");
61
+ },
62
+ "Not a git repository. Run: git init",
63
+ );
64
+
65
+ check(
66
+ "package.json exists",
67
+ () => {
68
+ return fs.existsSync("package.json");
69
+ },
70
+ "No package.json found. Run: npm init",
71
+ );
72
+
73
+ check(
74
+ "Test framework configured",
75
+ () => {
76
+ try {
77
+ const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
78
+ return !!(pkg.scripts && pkg.scripts.test);
79
+ } catch {
80
+ return false;
81
+ }
82
+ },
83
+ 'No test script in package.json. Add "test" script.',
84
+ );
85
+
86
+ check(
87
+ "Node.js installed",
88
+ () => {
89
+ try {
90
+ execFileSync("node", ["--version"], { stdio: "pipe" });
91
+ return true;
92
+ } catch {
93
+ return false;
94
+ }
95
+ },
96
+ "Node.js not found. Install from nodejs.org",
97
+ );
98
+
99
+ return printResults();
100
+ }
101
+
102
+ function validateDev() {
103
+ console.log("Validating prerequisites for /dev stage...\n");
104
+
105
+ check(
106
+ "On feature branch",
107
+ () => {
108
+ try {
109
+ const branch = execFileSync(
110
+ "git",
111
+ ["rev-parse", "--abbrev-ref", "HEAD"],
112
+ {
113
+ encoding: "utf8",
114
+ },
115
+ ).trim();
116
+ return (
117
+ branch.startsWith("feat/") ||
118
+ branch.startsWith("fix/") ||
119
+ branch.startsWith("docs/")
120
+ );
121
+ } catch {
122
+ return false;
123
+ }
124
+ },
125
+ "Not on a feature branch. Create one: git checkout -b feat/your-feature",
126
+ );
127
+
128
+ check(
129
+ "Plan file exists",
130
+ () => {
131
+ try {
132
+ const plansDir = ".claude/plans";
133
+ if (!fs.existsSync(plansDir)) return false;
134
+ const plans = fs.readdirSync(plansDir).filter((f) => f.endsWith(".md"));
135
+ return plans.length > 0;
136
+ } catch {
137
+ return false;
138
+ }
139
+ },
140
+ "No plan file found in .claude/plans/. Run: /plan",
141
+ );
142
+
143
+ check(
144
+ "Research file exists",
145
+ () => {
146
+ try {
147
+ const researchDir = "docs/research";
148
+ if (!fs.existsSync(researchDir)) return false;
149
+ const research = fs
150
+ .readdirSync(researchDir)
151
+ .filter((f) => f.endsWith(".md"));
152
+ return research.length > 0;
153
+ } catch {
154
+ return false;
155
+ }
156
+ },
157
+ "No research file found in docs/research/. Run: /research",
158
+ );
159
+
160
+ check(
161
+ "Test directory exists",
162
+ () => {
163
+ return (
164
+ fs.existsSync("test") ||
165
+ fs.existsSync("tests") ||
166
+ fs.existsSync("__tests__")
167
+ );
168
+ },
169
+ "No test directory found. Create test/ directory",
170
+ );
171
+
172
+ return printResults();
173
+ }
174
+
175
+ function validateShip() {
176
+ console.log("Validating prerequisites for /ship stage...\n");
177
+
178
+ check(
179
+ "Tests exist",
180
+ () => {
181
+ const testDirs = ["test", "tests", "__tests__"];
182
+ return testDirs.some((dir) => {
183
+ if (!fs.existsSync(dir)) return false;
184
+ try {
185
+ const files = fs.readdirSync(dir, { recursive: true });
186
+ return files.some(
187
+ (f) => f.includes(".test.") || f.includes(".spec."),
188
+ );
189
+ } catch {
190
+ return false;
191
+ }
192
+ });
193
+ },
194
+ "No test files found. Write tests before shipping!",
195
+ );
196
+
197
+ check(
198
+ "Tests pass",
199
+ () => {
200
+ try {
201
+ execFileSync("npm", ["test"], { stdio: "pipe" });
202
+ return true;
203
+ } catch {
204
+ return false;
205
+ }
206
+ },
207
+ "Tests are failing. Fix them before shipping: npm test",
208
+ );
209
+
210
+ check(
211
+ "Documentation updated",
212
+ () => {
213
+ return fs.existsSync("README.md") || fs.existsSync("docs");
214
+ },
215
+ "No documentation found. Update README.md or docs/",
216
+ );
217
+
218
+ check(
219
+ "No uncommitted changes",
220
+ () => {
221
+ try {
222
+ const status = execFileSync("git", ["status", "--porcelain"], {
223
+ encoding: "utf8",
224
+ }).trim();
225
+ return status.length === 0;
226
+ } catch {
227
+ return false;
228
+ }
229
+ },
230
+ "Uncommitted changes found. Commit all changes before shipping.",
231
+ );
232
+
233
+ return printResults();
234
+ }
235
+
236
+ function showHelp() {
237
+ console.log(`
238
+ Forge Validate - Prerequisite validation for workflow stages
239
+
240
+ Usage:
241
+ forge-validate <command>
242
+
243
+ Commands:
244
+ status Check project prerequisites (git, npm, tests)
245
+ dev Validate before /dev stage (branch, plan, research)
246
+ ship Validate before /ship stage (tests pass, docs, clean)
247
+ help Show this help message
248
+
249
+ Examples:
250
+ forge-validate status
251
+ forge-validate dev
252
+ forge-validate ship
253
+ `);
254
+ }
255
+
256
+ // Main CLI
257
+ function main() {
258
+ const args = process.argv.slice(2);
259
+ const command = args[0];
260
+
261
+ if (
262
+ !command ||
263
+ command === "help" ||
264
+ command === "--help" ||
265
+ command === "-h"
266
+ ) {
267
+ showHelp();
268
+ process.exit(0);
269
+ }
270
+
271
+ let success;
272
+
273
+ switch (command) {
274
+ case "status":
275
+ success = validateStatus();
276
+ break;
277
+ case "dev":
278
+ success = validateDev();
279
+ break;
280
+ case "ship":
281
+ success = validateShip();
282
+ break;
283
+ default:
284
+ console.error(`\n❌ Unknown command: ${command}\n`);
285
+ console.error("Valid commands: status, dev, ship, help\n");
286
+ showHelp();
287
+ process.exit(1);
288
+ }
289
+
290
+ process.exit(success ? 0 : 1);
291
+ }
292
+
293
+ // Export for testing
294
+ module.exports = {
295
+ validateStatus,
296
+ validateDev,
297
+ validateShip,
298
+ };
299
+
300
+ // Run if called directly
301
+ if (require.main === module) {
302
+ main();
303
+ }