@moritakaaz/markflow 0.0.0-stage → 0.2.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.
Files changed (41) hide show
  1. package/README.md +32 -2
  2. package/package.json +21 -4
  3. package/src/agents.js +17 -0
  4. package/src/discover.js +188 -0
  5. package/src/doctor.js +284 -0
  6. package/src/hash.js +7 -0
  7. package/src/index.js +61 -0
  8. package/src/init.js +228 -0
  9. package/src/minimal.js +22 -0
  10. package/src/templates.js +17 -0
  11. package/src/update.js +146 -0
  12. package/src/versions.js +6 -0
  13. package/templates/AGENTS.md +26 -0
  14. package/templates/markflow/.agents/MEMORY.md +17 -0
  15. package/templates/markflow/.agents/PROMPTS.md +21 -0
  16. package/templates/markflow/.agents/RULES.md +7 -0
  17. package/templates/markflow/.agents/mcp.json +3 -0
  18. package/templates/markflow/.agents/workflows/create-feature.md +14 -0
  19. package/templates/markflow/.agents/workflows/implement-spec.md +13 -0
  20. package/templates/markflow/.agents/workflows/onboarding.md +22 -0
  21. package/templates/markflow/.agents/workflows/pre-deploy-check.md +13 -0
  22. package/templates/markflow/.agents/workflows/review-spec.md +13 -0
  23. package/templates/markflow/.agents/workflows/sync-docs.md +13 -0
  24. package/templates/markflow/CHANGELOG.md +9 -0
  25. package/templates/markflow/README.md +30 -0
  26. package/templates/markflow/TASKS.md +13 -0
  27. package/templates/markflow/docs/API.md +24 -0
  28. package/templates/markflow/docs/ARCHITECTURE.md +29 -0
  29. package/templates/markflow/docs/CODING_STANDARDS.md +40 -0
  30. package/templates/markflow/docs/DATA_MODEL.md +23 -0
  31. package/templates/markflow/docs/DEPLOY.md +23 -0
  32. package/templates/markflow/docs/GLOSSARY.md +16 -0
  33. package/templates/markflow/docs/MCP.md +19 -0
  34. package/templates/markflow/docs/PRD.md +29 -0
  35. package/templates/markflow/docs/STACK.md +19 -0
  36. package/templates/markflow/docs/TESTING.md +27 -0
  37. package/templates/markflow/docs/adr/001-initial-stack.md +25 -0
  38. package/templates/markflow/docs/adr/_template.md +27 -0
  39. package/templates/markflow/specs/_template.md +33 -0
  40. package/templates/markflow/specs/index.md +15 -0
  41. package/test/cli.test.js +590 -0
package/README.md CHANGED
@@ -1,3 +1,33 @@
1
- # Temporary Holding Version
1
+ # CLI
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This folder holds the Markflow command line tool. Read it when you work on the init command.
4
+
5
+ ## Tests
6
+
7
+ Run tests with npm test from this folder. Tests use temp dirs only. They never touch a real repo. They cover discovery, init, doctor, update, the command parser, and agent adapters.
8
+
9
+ ## Usage
10
+
11
+ Install it with npx @moritakaaz/markflow init. Then run one of these commands. A command you do not know is rejected and creates no files.
12
+
13
+ - `markflow init` creates the docs layer in the current repo.
14
+ - `markflow init --minimal` creates only PRD, ARCHITECTURE, one spec, and TASKS, plus AGENTS.md and the markflow README. Doctor checks only those files in a minimal project. Run plain `markflow init` later to add the rest.
15
+ - `markflow init --agent claude` also creates CLAUDE.md. Use `--agent codex` for Codex, which reads AGENTS.md. Use `--agent cursor` for .cursor/rules/markflow.mdc. Use `--agent copilot` for .github/copilot-instructions.md.
16
+ - `markflow doctor` checks the docs layer for missing files, leftover placeholders, broken internal links, and specs missing from the index. It reports errors, warnings, and info. Only errors make it exit with 1.
17
+ - `markflow update` restores missing files and updates files the user did not change. It also protects AGENTS.md and agent files with the same hash check. ARCHITECTURE.md and STACK.md belong to the user after init, so update restores them when missing but never changes their content. Use `markflow update --preview` to see what would change without changing any file. The CLI, methodology, and template each have their own version. See src/versions.js for the methodology and template versions.
18
+ - `markflow --help` shows the commands.
19
+ - `markflow --version` shows the CLI version.
20
+
21
+ Init reads a repo, then copies the base templates into it. It is non-interactive by default. It does not init a framework. It does not install app dependencies.
22
+
23
+ Use --agent to pick the agent file. The default is generic and creates AGENTS.md only. Use --agent claude to also create CLAUDE.md. Use --agent cursor to also create .cursor/rules/markflow.mdc.
24
+
25
+ Init also writes markflow/.markflow.json. That file holds the cli, methodology, and template versions used at init time.
26
+
27
+ Doctor checks a repo for missing Markflow files. Run it with doctor. It reports missing files and placeholders that were not replaced. It exits with 1 when it finds issues.
28
+
29
+ Update restores missing files and updates files the user did not change. Run it with update. It never overwrites a file the user changed. It reports skipped files instead. It updates the version marker after it runs.
30
+
31
+ Init detects stacks from manifest files at the root and in packages and apps members. It scans nested members up to three levels deep. It reports each stack with its evidence file and confidence, and it marks a repo as a monorepo when workspace evidence exists or when it finds manifests in more than one member. It also records test and CI configs it finds at the root. Init writes what it found into markflow/docs/ARCHITECTURE.md and markflow/docs/STACK.md, and it stores the full discovery result in markflow/.markflow.json. It only fills docs that it just created. It never changes docs the user already edited.
32
+
33
+ See docs/PROJECT_DISCOVERY.md for how discovery works.
package/package.json CHANGED
@@ -1,6 +1,23 @@
1
1
  {
2
+ "bin": {
3
+ "markflow": "./src/index.js"
4
+ },
5
+ "description": "Create a Markflow docs layer in a repo.",
6
+ "engines": {
7
+ "node": ">=18"
8
+ },
9
+ "files": [
10
+ "src",
11
+ "test",
12
+ "templates",
13
+ "README.md"
14
+ ],
15
+ "license": "MIT",
2
16
  "name": "@moritakaaz/markflow",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
17
+ "scripts": {
18
+ "prepack": "rm -rf templates && cp -r ../../templates/base templates",
19
+ "test": "node --test"
20
+ },
21
+ "type": "module",
22
+ "version": "0.2.0"
23
+ }
package/src/agents.js ADDED
@@ -0,0 +1,17 @@
1
+ // Agent adapters map an agent name to its instruction files.
2
+ // AGENTS.md is created for every agent, so it is not listed here.
3
+ // Each entry lists the extra files the agent needs.
4
+ // A file entry can add frontmatter for formats that need it.
5
+ export const AGENTS = {
6
+ generic: [],
7
+ codex: [],
8
+ claude: [{ path: "CLAUDE.md" }],
9
+ cursor: [
10
+ {
11
+ path: ".cursor/rules/markflow.mdc",
12
+ frontmatter:
13
+ "---\ndescription: Markflow project instructions\nglobs:\nalwaysApply: true\n---\n",
14
+ },
15
+ ],
16
+ copilot: [{ path: ".github/copilot-instructions.md" }],
17
+ };
@@ -0,0 +1,188 @@
1
+ // Discovery reads manifest files in a repo.
2
+ // It checks many manifests, not only package.json.
3
+ // It reports every stack it finds, with its evidence.
4
+ // It scans nested members and reads workspace evidence.
5
+ import { readdir, readFile } from "node:fs/promises";
6
+ import { basename, join } from "node:path";
7
+
8
+ const MANIFESTS = {
9
+ "package.json": "JavaScript",
10
+ "composer.json": "PHP",
11
+ "go.mod": "Go",
12
+ "Cargo.toml": "Rust",
13
+ "pyproject.toml": "Python",
14
+ "requirements.txt": "Python",
15
+ "pom.xml": "Java",
16
+ "build.gradle": "Java",
17
+ "build.gradle.kts": "Kotlin",
18
+ "pubspec.yaml": "Dart",
19
+ "Gemfile": "Ruby",
20
+ };
21
+
22
+ const WORKSPACE_FILES = ["pnpm-workspace.yaml", "lerna.json"];
23
+
24
+ const CONFIG_PATTERNS = [
25
+ "vitest.config.js",
26
+ "vitest.config.ts",
27
+ "jest.config.js",
28
+ "jest.config.ts",
29
+ "pytest.ini",
30
+ "phpunit.xml",
31
+ ".gitlab-ci.yml",
32
+ ];
33
+
34
+ const SKIP_DIRS = new Set([
35
+ "node_modules",
36
+ ".git",
37
+ "dist",
38
+ "build",
39
+ "coverage",
40
+ "markflow",
41
+ ".markflow",
42
+ "vendor",
43
+ ".venv",
44
+ "venv",
45
+ "target",
46
+ ".next",
47
+ ]);
48
+
49
+ function stackFromEntry(name) {
50
+ if (name in MANIFESTS) {
51
+ return MANIFESTS[name];
52
+ }
53
+ if (name.endsWith(".csproj") || name.endsWith(".sln")) {
54
+ return "C#";
55
+ }
56
+ return null;
57
+ }
58
+
59
+ export async function discover(root) {
60
+ const projectName = basename(root);
61
+ const stacks = [];
62
+ const manifests = [];
63
+ const configs = [];
64
+ const workspaceEvidence = [];
65
+ const memberDirs = new Set();
66
+ const seen = new Set();
67
+ function addStack(name, evidence, confidence) {
68
+ manifests.push(evidence);
69
+ if (seen.has(name)) {
70
+ return;
71
+ }
72
+ seen.add(name);
73
+ stacks.push({ name, evidence, confidence });
74
+ }
75
+ async function scanDir(dir, rel, confidence, depth) {
76
+ let entries;
77
+ try {
78
+ entries = await readdir(dir, { withFileTypes: true });
79
+ } catch {
80
+ return;
81
+ }
82
+ for (const entry of entries) {
83
+ const childRel = rel ? rel + "/" + entry.name : entry.name;
84
+ if (entry.isDirectory()) {
85
+ if (SKIP_DIRS.has(entry.name)) {
86
+ continue;
87
+ }
88
+ if (
89
+ depth === 0 &&
90
+ (entry.name === "packages" || entry.name === "apps")
91
+ ) {
92
+ continue;
93
+ }
94
+ if (depth < 3) {
95
+ await scanDir(join(dir, entry.name), childRel, confidence, depth + 1);
96
+ }
97
+ continue;
98
+ }
99
+ const name = stackFromEntry(entry.name);
100
+ if (name) {
101
+ addStack(name, childRel, confidence);
102
+ if (rel) {
103
+ memberDirs.add(rel.split("/").slice(0, 2).join("/"));
104
+ }
105
+ }
106
+ }
107
+ }
108
+ await scanDir(root, "", "high", 0);
109
+ let rootEntries = [];
110
+ try {
111
+ rootEntries = await readdir(root, { withFileTypes: true });
112
+ } catch {
113
+ rootEntries = [];
114
+ }
115
+ for (const entry of rootEntries) {
116
+ if (!entry.isDirectory()) {
117
+ continue;
118
+ }
119
+ if (entry.name !== "packages" && entry.name !== "apps") {
120
+ continue;
121
+ }
122
+ let members = [];
123
+ try {
124
+ members = await readdir(join(root, entry.name), { withFileTypes: true });
125
+ } catch {
126
+ continue;
127
+ }
128
+ for (const member of members) {
129
+ if (!member.isDirectory() || SKIP_DIRS.has(member.name)) {
130
+ continue;
131
+ }
132
+ await scanDir(
133
+ join(root, entry.name, member.name),
134
+ entry.name + "/" + member.name,
135
+ "medium",
136
+ 0
137
+ );
138
+ }
139
+ }
140
+ for (const file of WORKSPACE_FILES) {
141
+ try {
142
+ await readFile(join(root, file), "utf8");
143
+ workspaceEvidence.push(file);
144
+ } catch {
145
+ // No workspace file with this name.
146
+ }
147
+ }
148
+ try {
149
+ const pkg = JSON.parse(await readFile(join(root, "package.json"), "utf8"));
150
+ if (pkg.workspaces) {
151
+ workspaceEvidence.push("package.json workspaces");
152
+ }
153
+ } catch {
154
+ // No readable package.json at the root.
155
+ }
156
+ for (const pattern of CONFIG_PATTERNS) {
157
+ try {
158
+ await readFile(join(root, pattern), "utf8");
159
+ configs.push(pattern);
160
+ } catch {
161
+ // No config file with this name.
162
+ }
163
+ }
164
+ try {
165
+ await readdir(join(root, ".github/workflows"));
166
+ configs.push(".github/workflows");
167
+ } catch {
168
+ // No workflows folder.
169
+ }
170
+ const hasFiles = rootEntries.some(
171
+ (e) => e.name !== ".git" && e.name !== "markflow"
172
+ );
173
+ const mode = stacks.length > 0 || hasFiles ? "brownfield" : "greenfield";
174
+ const monorepo = workspaceEvidence.length > 0 || memberDirs.size > 1;
175
+ return {
176
+ stack: stacks.length > 0 ? stacks[0].name : "TBD",
177
+ stacks,
178
+ manifests,
179
+ configs,
180
+ workspace: {
181
+ detected: workspaceEvidence.length > 0,
182
+ evidence: workspaceEvidence,
183
+ },
184
+ monorepo,
185
+ mode,
186
+ projectName,
187
+ };
188
+ }
package/src/doctor.js ADDED
@@ -0,0 +1,284 @@
1
+ // Doctor checks a repo for missing Markflow files.
2
+ // It reports errors, warnings, and info.
3
+ // Errors fail the check. Warnings and info do not.
4
+ import { readdir, readFile, stat } from "node:fs/promises";
5
+ import { basename, dirname, join, resolve } from "node:path";
6
+ import { findTemplatesDir } from "./templates.js";
7
+ import { MINIMAL_REQUIRED } from "./minimal.js";
8
+
9
+ const REQUIRED = [
10
+ "AGENTS.md",
11
+ "markflow/README.md",
12
+ "markflow/TASKS.md",
13
+ "markflow/CHANGELOG.md",
14
+ "markflow/docs/PRD.md",
15
+ "markflow/docs/ARCHITECTURE.md",
16
+ "markflow/docs/CODING_STANDARDS.md",
17
+ "markflow/docs/STACK.md",
18
+ "markflow/docs/TESTING.md",
19
+ "markflow/specs/index.md",
20
+ "markflow/.markflow.json",
21
+ ];
22
+
23
+ const AGENT_FILES = ["CLAUDE.md", ".cursor/rules/markflow.mdc"];
24
+
25
+ async function walk(dir, out) {
26
+ let entries;
27
+ try {
28
+ entries = await readdir(dir, { withFileTypes: true });
29
+ } catch {
30
+ return;
31
+ }
32
+ for (const entry of entries) {
33
+ const full = join(dir, entry.name);
34
+ if (entry.isDirectory()) {
35
+ await walk(full, out);
36
+ continue;
37
+ }
38
+ if (entry.name.endsWith(".md")) {
39
+ out.push(full);
40
+ }
41
+ }
42
+ }
43
+
44
+ // First sentence of each fill-in instruction in the templates.
45
+ // The full lines live inside HTML comments in the templates.
46
+ // A doc that still shows one of these as visible text was not filled in.
47
+ const INSTRUCTIONS = [
48
+ "Write one short paragraph.",
49
+ "List the main users.",
50
+ "List the core features.",
51
+ "List what this project will not do.",
52
+ "Write how you will know this project works.",
53
+ "Write what discovery found.",
54
+ "Write where code lives.",
55
+ "Write how a request or a job moves through the system.",
56
+ "Add one row per library.",
57
+ "Add one entry per term.",
58
+ "Write what you test at each level.",
59
+ "Write the exact command to run tests.",
60
+ "List the edge cases that often break.",
61
+ "Write your naming pattern here.",
62
+ "Write your function format here.",
63
+ "Write the order of parts in a file here.",
64
+ "List the main entities.",
65
+ "Write how entities link to each other.",
66
+ "Write the rules for data.",
67
+ "Write how schema changes are made and tested.",
68
+ "Write how auth works.",
69
+ "List each endpoint with method, path, request, and response.",
70
+ "Write the shared rules.",
71
+ "Write the steps from main branch to production.",
72
+ "List the env vars that are required.",
73
+ "Write how to roll back a bad release.",
74
+ "Add one entry per server.",
75
+ "Write how the master config in `.agents/mcp.json` is copied to your AI tool.",
76
+ "Write what this feature must do in one or two sentences.",
77
+ "Write who uses this feature.",
78
+ "List what must be true when this feature is done.",
79
+ "List what this feature will not do.",
80
+ "Write how you will test this feature.",
81
+ "Write the decision in one sentence.",
82
+ "Write why this decision was needed.",
83
+ "List the options you considered.",
84
+ "Write what you picked and why.",
85
+ "Write Draft or Approved.",
86
+ "Write the picked stack and the main reason.",
87
+ ];
88
+
89
+ // Remove HTML comments, so hidden instructions do not count as visible text.
90
+ function stripComments(text) {
91
+ return text.replace(/<!--[\s\S]*?-->/g, "");
92
+ }
93
+
94
+ // Lines of one ## section, or null when the section is missing.
95
+ function sectionLines(text, heading) {
96
+ const lines = text.split("\n");
97
+ const start = lines.findIndex((line) => line.trim() === "## " + heading);
98
+ if (start === -1) {
99
+ return null;
100
+ }
101
+ const out = [];
102
+ for (let i = start + 1; i < lines.length; i += 1) {
103
+ if (lines[i].startsWith("## ")) {
104
+ break;
105
+ }
106
+ out.push(lines[i]);
107
+ }
108
+ return out;
109
+ }
110
+
111
+ function findLinks(text) {
112
+ const links = [];
113
+ const re = /\[[^\]]*\]\(([^)\s]+)\)/g;
114
+ let match;
115
+ while ((match = re.exec(text)) !== null) {
116
+ links.push(match[1]);
117
+ }
118
+ return links;
119
+ }
120
+
121
+ export async function runDoctor(options) {
122
+ const root = (options && options.target) || process.cwd();
123
+ const errors = [];
124
+ const warnings = [];
125
+ let marker = null;
126
+ try {
127
+ marker = JSON.parse(
128
+ await readFile(join(root, "markflow/.markflow.json"), "utf8")
129
+ );
130
+ if (!marker.cli || !marker.template || !marker.methodology) {
131
+ console.log("Error Missing version in .markflow.json");
132
+ errors.push("markflow/.markflow.json");
133
+ }
134
+ } catch {
135
+ // Missing marker is already reported below.
136
+ }
137
+ const required =
138
+ marker && marker.minimal === true ? MINIMAL_REQUIRED : REQUIRED;
139
+ for (const file of required) {
140
+ try {
141
+ await stat(join(root, file));
142
+ console.log("OK " + file);
143
+ } catch {
144
+ console.log("Error Missing " + file);
145
+ errors.push(file);
146
+ }
147
+ }
148
+ if (marker && marker.files) {
149
+ for (const rel of Object.keys(marker.files)) {
150
+ try {
151
+ await stat(join(root, "markflow", rel));
152
+ } catch {
153
+ console.log("Warning Tracked file missing " + rel);
154
+ warnings.push(rel);
155
+ }
156
+ }
157
+ }
158
+ const mdFiles = [];
159
+ await walk(join(root, "markflow"), mdFiles);
160
+ mdFiles.push(join(root, "AGENTS.md"));
161
+ for (const file of AGENT_FILES) {
162
+ mdFiles.push(join(root, file));
163
+ }
164
+ let tbd = 0;
165
+ for (const file of mdFiles) {
166
+ let text;
167
+ try {
168
+ text = await readFile(file, "utf8");
169
+ } catch {
170
+ continue;
171
+ }
172
+ const rel = file.slice(root.length + 1);
173
+ if (text.includes("{{project_name}}")) {
174
+ console.log("Error Placeholder left in " + rel);
175
+ errors.push(rel);
176
+ }
177
+ const visible = stripComments(text);
178
+ for (const phrase of INSTRUCTIONS) {
179
+ if (visible.includes(phrase)) {
180
+ console.log("Warning Instruction text left in " + rel);
181
+ warnings.push(rel);
182
+ break;
183
+ }
184
+ }
185
+ tbd += text.split("TBD").length - 1;
186
+ for (const link of findLinks(text)) {
187
+ if (
188
+ link.startsWith("http") ||
189
+ link.startsWith("#") ||
190
+ link.startsWith("mailto:")
191
+ ) {
192
+ continue;
193
+ }
194
+ const target = link.split("#")[0];
195
+ if (!target) {
196
+ continue;
197
+ }
198
+ const dest = resolve(dirname(file), target);
199
+ try {
200
+ await stat(dest);
201
+ } catch {
202
+ console.log("Error Broken link in " + rel + " to " + target);
203
+ errors.push(rel);
204
+ }
205
+ }
206
+ }
207
+ try {
208
+ const templatesDir = await findTemplatesDir();
209
+ const projectName =
210
+ (marker && marker.project) || basename(root);
211
+ const prdTemplate = (
212
+ await readFile(join(templatesDir, "markflow/docs/PRD.md"), "utf8")
213
+ )
214
+ .split("{{project_name}}")
215
+ .join(projectName);
216
+ const prdText = await readFile(
217
+ join(root, "markflow/docs/PRD.md"),
218
+ "utf8"
219
+ );
220
+ if (prdText === prdTemplate) {
221
+ console.log("Warning PRD is still the template markflow/docs/PRD.md");
222
+ warnings.push("markflow/docs/PRD.md");
223
+ }
224
+ } catch {
225
+ // Template or PRD is missing, so skip this check.
226
+ }
227
+ let specTotal = 0;
228
+ let specDraft = 0;
229
+ try {
230
+ const specFiles = [];
231
+ await walk(join(root, "markflow/specs"), specFiles);
232
+ const indexText = await readFile(
233
+ join(root, "markflow/specs/index.md"),
234
+ "utf8"
235
+ );
236
+ for (const file of specFiles) {
237
+ const name = file.split("/").pop();
238
+ if (name === "index.md" || name === "_template.md") {
239
+ continue;
240
+ }
241
+ specTotal += 1;
242
+ const text = await readFile(file, "utf8");
243
+ const rel = file.slice(root.length + 1);
244
+ if (text.includes("Status") && text.includes("Draft")) {
245
+ specDraft += 1;
246
+ }
247
+ if (!indexText.includes(name)) {
248
+ console.log("Warning Spec not listed in index " + rel);
249
+ warnings.push(rel);
250
+ }
251
+ const acceptance = sectionLines(text, "Acceptance");
252
+ if (acceptance !== null) {
253
+ const cases = stripComments(acceptance.join("\n"))
254
+ .split("\n")
255
+ .filter((line) => line.trim() !== "");
256
+ if (cases.length === 0) {
257
+ console.log("Warning Empty acceptance in " + rel);
258
+ warnings.push(rel);
259
+ }
260
+ }
261
+ const status = sectionLines(text, "Status");
262
+ if (
263
+ status !== null &&
264
+ status.some((line) => line.trim() === "Approved") &&
265
+ text.includes("TBD")
266
+ ) {
267
+ console.log("Warning Approved spec still has TBD in " + rel);
268
+ warnings.push(rel);
269
+ }
270
+ }
271
+ } catch {
272
+ // Missing specs folder is already reported above.
273
+ }
274
+ console.log("Info " + tbd + " TBD left in docs.");
275
+ console.log("Info " + specTotal + " specs, " + specDraft + " draft.");
276
+ if (errors.length === 0 && warnings.length === 0) {
277
+ console.log("Doctor found no issues.");
278
+ } else {
279
+ console.log(
280
+ "Doctor found " + errors.length + " errors, " + warnings.length + " warnings."
281
+ );
282
+ }
283
+ return { ok: errors.length === 0, errors, warnings, issues: errors };
284
+ }
package/src/hash.js ADDED
@@ -0,0 +1,7 @@
1
+ // Hash helper for change detection.
2
+ // It returns a short hash of text.
3
+ import { createHash } from "node:crypto";
4
+
5
+ export function hashText(text) {
6
+ return createHash("sha256").update(text).digest("hex").slice(0, 12);
7
+ }
package/src/index.js ADDED
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env node
2
+ // Entry point for the markflow command.
3
+ import { readFile } from "node:fs/promises";
4
+ import { dirname, join } from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { runDoctor } from "./doctor.js";
7
+ import { runInit } from "./init.js";
8
+ import { runUpdate } from "./update.js";
9
+
10
+ const HELP = [
11
+ "Usage",
12
+ " markflow init [--agent generic|claude|codex|cursor|copilot] [--minimal]",
13
+ " markflow doctor",
14
+ " markflow update [--preview]",
15
+ " markflow --help",
16
+ " markflow --version",
17
+ ].join("\n");
18
+
19
+ function parseAgent(args) {
20
+ const idx = args.indexOf("--agent");
21
+ if (idx !== -1 && args[idx + 1]) {
22
+ return args[idx + 1];
23
+ }
24
+ const eq = args.find((a) => a.startsWith("--agent="));
25
+ if (eq) {
26
+ return eq.slice("--agent=".length);
27
+ }
28
+ return "generic";
29
+ }
30
+
31
+ async function printVersion() {
32
+ const srcDir = dirname(fileURLToPath(import.meta.url));
33
+ const pkg = JSON.parse(await readFile(join(srcDir, "../package.json"), "utf8"));
34
+ console.log(pkg.version);
35
+ }
36
+
37
+ const args = process.argv.slice(2);
38
+ if (args.length === 0) {
39
+ console.log(HELP);
40
+ process.exitCode = 1;
41
+ } else if (args[0] === "--help" || args[0] === "-h") {
42
+ console.log(HELP);
43
+ } else if (args[0] === "--version" || args[0] === "-v") {
44
+ await printVersion();
45
+ } else if (args[0] === "doctor") {
46
+ const result = await runDoctor();
47
+ process.exitCode = result.ok ? 0 : 1;
48
+ } else if (args[0] === "update") {
49
+ const result = await runUpdate({ preview: args.includes("--preview") });
50
+ process.exitCode = result.ok ? 0 : 1;
51
+ } else if (args[0] === "init") {
52
+ const result = await runInit({
53
+ agent: parseAgent(args.slice(1)),
54
+ minimal: args.includes("--minimal"),
55
+ });
56
+ process.exitCode = result.ok === false ? 1 : 0;
57
+ } else {
58
+ console.log("Unknown command " + args[0] + ".");
59
+ console.log(HELP);
60
+ process.exitCode = 1;
61
+ }