@danielstyle/featflow 0.0.0-stage → 0.6.4

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 (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +249 -2
  3. package/README.zh-CN.md +250 -0
  4. package/bin/featflow.mjs +116 -0
  5. package/package.json +63 -4
  6. package/skills/featflow/SKILL.md +127 -0
  7. package/skills/featflow-issues/SKILL.md +104 -0
  8. package/skills/featflow-regression/SKILL.md +63 -0
  9. package/src/agent-docs.mjs +143 -0
  10. package/src/changed-files.mjs +204 -0
  11. package/src/commands/cleanup.mjs +221 -0
  12. package/src/commands/codegraph.mjs +108 -0
  13. package/src/commands/doctor.mjs +303 -0
  14. package/src/commands/finish.mjs +249 -0
  15. package/src/commands/hook-cmd.mjs +78 -0
  16. package/src/commands/hooks-cmd.mjs +247 -0
  17. package/src/commands/init.mjs +477 -0
  18. package/src/commands/issue.mjs +434 -0
  19. package/src/commands/new.mjs +311 -0
  20. package/src/commands/plan-check.mjs +75 -0
  21. package/src/commands/policy-cmd.mjs +55 -0
  22. package/src/commands/pr-cleanup.mjs +194 -0
  23. package/src/commands/regression.mjs +206 -0
  24. package/src/commands/setup.mjs +343 -0
  25. package/src/commands/size-check.mjs +137 -0
  26. package/src/commands/skills.mjs +181 -0
  27. package/src/commands/status.mjs +100 -0
  28. package/src/commands/sync.mjs +421 -0
  29. package/src/commands/verify.mjs +206 -0
  30. package/src/commands/worktree.mjs +205 -0
  31. package/src/config.mjs +295 -0
  32. package/src/detect.mjs +314 -0
  33. package/src/git.mjs +331 -0
  34. package/src/hook-adapters.mjs +218 -0
  35. package/src/hooks-core.mjs +420 -0
  36. package/src/index.mjs +14 -0
  37. package/src/init-args.mjs +294 -0
  38. package/src/init-hooks.mjs +312 -0
  39. package/src/init-prompts.mjs +191 -0
  40. package/src/issues-helpers.mjs +463 -0
  41. package/src/issues.mjs +521 -0
  42. package/src/plan.mjs +202 -0
  43. package/src/policy.mjs +143 -0
  44. package/src/regression.mjs +338 -0
  45. package/src/size-check.mjs +203 -0
  46. package/src/skills-manager.mjs +348 -0
  47. package/src/tui-select.mjs +378 -0
  48. package/templates/adapters/antigravity/hooks.json +42 -0
  49. package/templates/adapters/claude-code/settings.json +46 -0
  50. package/templates/adapters/codex/config.toml +19 -0
  51. package/templates/adapters/cursor/hooks.json +27 -0
  52. package/templates/agents/codex/codebase-locator.toml +28 -0
  53. package/templates/agents/codex/implementation-scout.toml +28 -0
  54. package/templates/agents/codex/regression-guard-planner.toml +20 -0
  55. package/templates/features/README.md +56 -0
  56. package/templates/features/_program_template.md +79 -0
  57. package/templates/features/_template.md +120 -0
  58. package/templates/features/current-feature.md +18 -0
  59. package/templates/githooks/post-merge +10 -0
  60. package/templates/githooks/pre-commit +14 -0
  61. package/templates/githooks/pre-push +13 -0
  62. package/templates/regression/regression-ledger.yml +23 -0
  63. package/templates/snippets/AGENTS-workflow.md +21 -0
@@ -0,0 +1,204 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { existsSync } from "node:fs";
3
+ import path from "node:path";
4
+
5
+ import { gitConfigGet, runStatus } from "./git.mjs";
6
+
7
+ export function normalizeRelPath(filePath) {
8
+ return String(filePath ?? "")
9
+ .replaceAll("\\", "/")
10
+ .replace(/^\.\//, "")
11
+ .trim();
12
+ }
13
+
14
+ export function uniqueNormalizedPaths(paths) {
15
+ return [...new Set((paths ?? []).map(normalizeRelPath).filter(Boolean))].sort();
16
+ }
17
+
18
+ export function existingFiles(repoRoot, paths) {
19
+ return uniqueNormalizedPaths(paths).filter((rel) => existsSync(path.resolve(repoRoot, rel)));
20
+ }
21
+
22
+ export function globToRegExp(pattern) {
23
+ const normalized = normalizeRelPath(pattern);
24
+ if (!normalized) {
25
+ return /^$/;
26
+ }
27
+
28
+ let regexStr = "^";
29
+ for (let i = 0; i < normalized.length; i += 1) {
30
+ const ch = normalized[i];
31
+ if (ch === "*") {
32
+ if (normalized[i + 1] === "*") {
33
+ if (normalized[i + 2] === "/") {
34
+ regexStr += "(?:.*/)?";
35
+ i += 2;
36
+ } else {
37
+ regexStr += ".*";
38
+ i += 1;
39
+ }
40
+ } else {
41
+ regexStr += "[^/]*";
42
+ }
43
+ continue;
44
+ }
45
+ if (ch === "?") {
46
+ regexStr += "[^/]";
47
+ continue;
48
+ }
49
+ regexStr += ch.replace(/[.+^${}()|[\]\\]/g, "\\$&");
50
+ }
51
+ regexStr += "$";
52
+ return new RegExp(regexStr);
53
+ }
54
+
55
+ export function matchesPattern(relPath, pattern) {
56
+ const normPath = normalizeRelPath(relPath);
57
+ const normPattern = normalizeRelPath(pattern);
58
+ if (!normPattern) {
59
+ return false;
60
+ }
61
+ if (!/[*?]/.test(normPattern)) {
62
+ if (normPattern.endsWith("/")) {
63
+ return normPath.startsWith(normPattern);
64
+ }
65
+ return normPath === normPattern || normPath.startsWith(`${normPattern}/`);
66
+ }
67
+ return globToRegExp(normPattern).test(normPath);
68
+ }
69
+
70
+ export function matchesAnyPattern(relPath, patterns) {
71
+ return (patterns ?? []).some((pattern) => matchesPattern(relPath, pattern));
72
+ }
73
+
74
+ function gitRefExists(repoRoot, ref) {
75
+ return runStatus("git", ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`], repoRoot) === 0;
76
+ }
77
+
78
+ function gitCurrentBranch(repoRoot) {
79
+ const res = spawnSync("git", ["symbolic-ref", "--quiet", "--short", "HEAD"], {
80
+ cwd: repoRoot,
81
+ encoding: "utf8",
82
+ });
83
+ return res.status === 0 ? (res.stdout ?? "").trim() : "";
84
+ }
85
+
86
+ function gitDiffNameOnly(repoRoot, args) {
87
+ const res = spawnSync("git", ["diff", "--name-only", "-z", ...args], {
88
+ cwd: repoRoot,
89
+ encoding: "utf8",
90
+ });
91
+ if (res.status !== 0) {
92
+ return [];
93
+ }
94
+ return (res.stdout ?? "").split("\0").filter(Boolean);
95
+ }
96
+
97
+ function gitStatusFiles(repoRoot) {
98
+ const res = spawnSync("git", ["status", "--porcelain=v1", "-z", "--untracked-files=all"], {
99
+ cwd: repoRoot,
100
+ encoding: "utf8",
101
+ });
102
+ if (res.status !== 0) {
103
+ return [];
104
+ }
105
+ const entries = (res.stdout ?? "").split("\0").filter(Boolean);
106
+ const files = [];
107
+ for (let index = 0; index < entries.length; index += 1) {
108
+ const entry = entries[index];
109
+ const status = entry.slice(0, 2);
110
+ files.push(entry.slice(3));
111
+ if (status.includes("R") || status.includes("C")) {
112
+ index += 1;
113
+ }
114
+ }
115
+ return files;
116
+ }
117
+
118
+ export function resolveChangedBase(repoRoot, config = null, options = {}) {
119
+ if (options.base !== undefined && options.base !== "") {
120
+ return { base: options.base, source: "--base" };
121
+ }
122
+ if (process.env.FEATFLOW_CHANGED_BASE) {
123
+ return { base: process.env.FEATFLOW_CHANGED_BASE, source: "FEATFLOW_CHANGED_BASE" };
124
+ }
125
+ if (process.env.CHANGED_CHECKS_BASE) {
126
+ return { base: process.env.CHANGED_CHECKS_BASE, source: "CHANGED_CHECKS_BASE" };
127
+ }
128
+
129
+ const current = gitCurrentBranch(repoRoot);
130
+ const remote = config?.remote || "origin";
131
+
132
+ if (config?.mode === "developer-branch") {
133
+ const devKey = config.developerBranchConfigKey || "featflow.developerBranch";
134
+ const devBranch = gitConfigGet(devKey, repoRoot);
135
+ if (devBranch) {
136
+ if (current === devBranch) {
137
+ return { base: "HEAD", source: `developer branch ${devBranch} working tree` };
138
+ }
139
+ if (gitRefExists(repoRoot, devBranch)) {
140
+ return { base: devBranch, source: `configured developer branch ${devBranch}` };
141
+ }
142
+ if (gitRefExists(repoRoot, `${remote}/${devBranch}`)) {
143
+ return { base: `${remote}/${devBranch}`, source: `remote developer branch ${remote}/${devBranch}` };
144
+ }
145
+ }
146
+ }
147
+
148
+ const baseBranch = config?.baseBranch || "main";
149
+ if (current === baseBranch) {
150
+ return { base: "HEAD", source: `base branch ${baseBranch} working tree` };
151
+ }
152
+ if (gitRefExists(repoRoot, baseBranch)) {
153
+ return { base: baseBranch, source: `base branch ${baseBranch}` };
154
+ }
155
+ if (gitRefExists(repoRoot, `${remote}/${baseBranch}`)) {
156
+ return { base: `${remote}/${baseBranch}`, source: `remote base branch ${remote}/${baseBranch}` };
157
+ }
158
+ return { base: "HEAD", source: "fallback HEAD working tree" };
159
+ }
160
+
161
+ export function changedFileSelection(repoRoot, config = null, options = {}) {
162
+ if (options.files?.length > 0) {
163
+ return {
164
+ base: null,
165
+ head: null,
166
+ source: "explicit files",
167
+ files: uniqueNormalizedPaths(options.files),
168
+ };
169
+ }
170
+
171
+ if (process.env.FEATFLOW_TEST_CHANGED_FILES) {
172
+ const testFiles = process.env.FEATFLOW_TEST_CHANGED_FILES.split(/\r?\n|,/)
173
+ .map((entry) => entry.trim())
174
+ .filter(Boolean);
175
+ return {
176
+ base: null,
177
+ head: null,
178
+ source: "FEATFLOW_TEST_CHANGED_FILES",
179
+ files: uniqueNormalizedPaths(testFiles),
180
+ };
181
+ }
182
+
183
+ const files = [];
184
+ const { base, source } = resolveChangedBase(repoRoot, config, options);
185
+ const head = options.head || process.env.FEATFLOW_CHANGED_HEAD || process.env.CHANGED_CHECKS_HEAD || "HEAD";
186
+
187
+ if (base && base !== "HEAD" && gitRefExists(repoRoot, base) && gitRefExists(repoRoot, head)) {
188
+ files.push(...gitDiffNameOnly(repoRoot, [`${base}...${head}`]));
189
+ }
190
+ files.push(...gitDiffNameOnly(repoRoot, ["--cached"]));
191
+ files.push(...gitDiffNameOnly(repoRoot, []));
192
+ files.push(...gitStatusFiles(repoRoot));
193
+
194
+ return {
195
+ base,
196
+ head,
197
+ source,
198
+ files: uniqueNormalizedPaths(files),
199
+ };
200
+ }
201
+
202
+ export function changedFiles(repoRoot, config = null, options = {}) {
203
+ return changedFileSelection(repoRoot, config, options).files;
204
+ }
@@ -0,0 +1,221 @@
1
+ import { loadConfig } from "../config.mjs";
2
+ import {
3
+ currentBranch,
4
+ fail,
5
+ findWorktreeForBranch,
6
+ gitConfigGet,
7
+ gitWorktrees,
8
+ isAncestor,
9
+ isInsidePath,
10
+ isTaskBranch,
11
+ localBranches,
12
+ localBranchExists,
13
+ runWrite,
14
+ statusShort,
15
+ } from "../git.mjs";
16
+ import { setupInstructions } from "../policy.mjs";
17
+
18
+ const usage = `
19
+ Usage:
20
+ featflow cleanup [options]
21
+
22
+ Removes local task branches and their clean worktrees once the branch tip is
23
+ already merged into the target base branch (developer branch in developer-branch
24
+ mode, or baseBranch in trunk mode). Never removes protected branches, unmerged
25
+ branches, dirty worktrees, or another developer's task branches.
26
+
27
+ Options:
28
+ --developer <branch> Override configured developer branch.
29
+ --dry-run Print mutating commands without running them.
30
+ --post-merge Run as a best-effort post-merge hook; only acts on the target branch.
31
+ -h, --help Show this help.
32
+ `.trim();
33
+
34
+ function parseArgs(argv) {
35
+ const options = {
36
+ developer: "",
37
+ dryRun: false,
38
+ postMerge: false,
39
+ };
40
+
41
+ for (let i = 0; i < argv.length; i += 1) {
42
+ const arg = argv[i];
43
+ if (arg === "-h" || arg === "--help") {
44
+ console.log(usage);
45
+ process.exit(0);
46
+ }
47
+ if (arg === "--dry-run") {
48
+ options.dryRun = true;
49
+ continue;
50
+ }
51
+ if (arg === "--post-merge") {
52
+ options.postMerge = true;
53
+ continue;
54
+ }
55
+ if (arg === "--developer") {
56
+ options.developer = argv[++i] ?? "";
57
+ continue;
58
+ }
59
+ if (arg.startsWith("--developer=")) {
60
+ options.developer = arg.slice("--developer=".length);
61
+ continue;
62
+ }
63
+ fail(`Unknown option: ${arg}`, usage);
64
+ }
65
+
66
+ return options;
67
+ }
68
+
69
+ export function cleanupMergedTaskBranches({
70
+ cwd,
71
+ config,
72
+ targetBranch,
73
+ developerBranch = "",
74
+ dryRun = false,
75
+ excludeBranches = [],
76
+ quiet = false,
77
+ }) {
78
+ const current = currentBranch(cwd);
79
+ const excluded = new Set(excludeBranches);
80
+ const protectedSet = new Set(config.protectedBranches);
81
+ const result = {
82
+ errors: [],
83
+ removedBranches: [],
84
+ removedWorktrees: [],
85
+ skipped: [],
86
+ };
87
+
88
+ for (const taskBranch of localBranches(cwd)) {
89
+ if (
90
+ taskBranch === targetBranch ||
91
+ taskBranch === current ||
92
+ excluded.has(taskBranch) ||
93
+ protectedSet.has(taskBranch) ||
94
+ !isTaskBranch(taskBranch, config, developerBranch)
95
+ ) {
96
+ continue;
97
+ }
98
+
99
+ if (!isAncestor(taskBranch, targetBranch, cwd)) {
100
+ continue;
101
+ }
102
+
103
+ const worktree = findWorktreeForBranch(gitWorktrees(cwd), taskBranch);
104
+ if (worktree) {
105
+ if (isInsidePath(process.cwd(), worktree.path)) {
106
+ result.skipped.push({ branch: taskBranch, reason: "current-worktree", detail: worktree.path });
107
+ continue;
108
+ }
109
+
110
+ const status = statusShort(worktree.path);
111
+ if (status) {
112
+ result.skipped.push({
113
+ branch: taskBranch,
114
+ reason: "dirty-worktree",
115
+ detail: `${worktree.path}\n${status}`,
116
+ });
117
+ continue;
118
+ }
119
+
120
+ try {
121
+ runWrite("git", ["worktree", "remove", worktree.path], cwd, { dryRun });
122
+ result.removedWorktrees.push({ branch: taskBranch, path: worktree.path });
123
+ } catch (error) {
124
+ result.errors.push({
125
+ action: "worktree-remove",
126
+ branch: taskBranch,
127
+ message: error instanceof Error ? error.message : String(error),
128
+ });
129
+ continue;
130
+ }
131
+ }
132
+
133
+ if (!localBranchExists(taskBranch, cwd)) {
134
+ continue;
135
+ }
136
+
137
+ try {
138
+ runWrite("git", ["branch", "-d", taskBranch], cwd, { dryRun });
139
+ result.removedBranches.push(taskBranch);
140
+ } catch (error) {
141
+ result.errors.push({
142
+ action: "branch-delete",
143
+ branch: taskBranch,
144
+ message: error instanceof Error ? error.message : String(error),
145
+ });
146
+ }
147
+ }
148
+
149
+ if (!quiet) {
150
+ printCleanupSummary(result, { dryRun });
151
+ }
152
+
153
+ return result;
154
+ }
155
+
156
+ function printCleanupSummary(result, { dryRun = false } = {}) {
157
+ const prefix = dryRun ? "Would clean" : "Cleaned";
158
+ for (const item of result.removedWorktrees) {
159
+ console.log(`${prefix} worktree for ${item.branch}: ${item.path}`);
160
+ }
161
+ for (const branch of result.removedBranches) {
162
+ console.log(`${prefix} merged task branch: ${branch}`);
163
+ }
164
+ for (const item of result.skipped) {
165
+ if (item.reason === "dirty-worktree") {
166
+ console.error(`Skipped ${item.branch}: worktree is dirty.\n${item.detail}`);
167
+ } else if (item.reason === "current-worktree") {
168
+ console.error(`Skipped ${item.branch}: current process is inside ${item.detail}`);
169
+ } else {
170
+ console.error(`Skipped ${item.branch}: ${item.reason}`);
171
+ }
172
+ }
173
+ for (const item of result.errors) {
174
+ console.error(`Failed to ${item.action} for ${item.branch}: ${item.message}`);
175
+ }
176
+
177
+ if (
178
+ result.removedBranches.length === 0 &&
179
+ result.removedWorktrees.length === 0 &&
180
+ result.skipped.length === 0 &&
181
+ result.errors.length === 0
182
+ ) {
183
+ console.log("No merged local task branches to clean.");
184
+ }
185
+ }
186
+
187
+ export async function runCleanup(argv) {
188
+ const options = parseArgs(argv);
189
+ const { config, repoRoot } = loadConfig(process.cwd());
190
+
191
+ let targetBranch = config.baseBranch;
192
+ let developerBranch = "";
193
+
194
+ if (config.mode === "developer-branch") {
195
+ developerBranch = options.developer || gitConfigGet(config.developerBranchConfigKey, repoRoot);
196
+ if (options.postMerge) {
197
+ if (!developerBranch || currentBranch(repoRoot) !== developerBranch) {
198
+ return;
199
+ }
200
+ } else if (!developerBranch) {
201
+ fail(setupInstructions(config));
202
+ }
203
+ targetBranch = developerBranch;
204
+ } else if (options.postMerge) {
205
+ if (currentBranch(repoRoot) !== config.baseBranch) {
206
+ return;
207
+ }
208
+ }
209
+
210
+ const result = cleanupMergedTaskBranches({
211
+ cwd: repoRoot,
212
+ config,
213
+ targetBranch,
214
+ developerBranch,
215
+ dryRun: options.dryRun,
216
+ });
217
+
218
+ if (!options.postMerge && result.errors.length > 0) {
219
+ process.exit(1);
220
+ }
221
+ }
@@ -0,0 +1,108 @@
1
+ import { existsSync } from "node:fs";
2
+ import path from "node:path";
3
+
4
+ import { commandExists, fail, findRepoRoot, runWrite } from "../git.mjs";
5
+
6
+ const usage = `
7
+ Usage:
8
+ featflow codegraph [bootstrap|status] [path] [options]
9
+
10
+ Initializes a local CodeGraph index when .codegraph/codegraph.db is missing,
11
+ or syncs the existing index when present. Skips cleanly in CI or when the
12
+ optional \`codegraph\` CLI is not installed.
13
+
14
+ Options:
15
+ --dry-run Print command without executing it
16
+ --json Output machine-readable JSON (for status)
17
+ -h, --help Show this help
18
+
19
+ Environment:
20
+ FEATFLOW_SKIP_CODEGRAPH=1 Skip CodeGraph bootstrap
21
+ FEATFLOW_CODEGRAPH_IN_CI=1 Allow bootstrap in CI
22
+ `.trim();
23
+
24
+ function parseArgs(argv) {
25
+ const options = {
26
+ subcommand: "bootstrap",
27
+ targetPath: "",
28
+ dryRun: false,
29
+ json: false,
30
+ };
31
+
32
+ for (const arg of argv) {
33
+ if (arg === "-h" || arg === "--help") {
34
+ console.log(usage);
35
+ process.exit(0);
36
+ }
37
+ if (arg === "--dry-run") {
38
+ options.dryRun = true;
39
+ continue;
40
+ }
41
+ if (arg === "--json") {
42
+ options.json = true;
43
+ continue;
44
+ }
45
+ if (arg.startsWith("-")) {
46
+ fail(`Unknown option for codegraph: ${arg}`, usage);
47
+ }
48
+ if ((arg === "bootstrap" || arg === "status") && !options.targetPath && options.subcommand === "bootstrap") {
49
+ options.subcommand = arg;
50
+ continue;
51
+ }
52
+ if (options.targetPath) {
53
+ fail(`Unexpected extra argument: ${arg}`, usage);
54
+ }
55
+ options.targetPath = arg;
56
+ }
57
+
58
+ return options;
59
+ }
60
+
61
+ export function inspectCodegraphState(repoRoot) {
62
+ const installed = commandExists("codegraph");
63
+ const indexPath = path.join(repoRoot, ".codegraph", "codegraph.db");
64
+ const indexed = existsSync(indexPath);
65
+ return { installed, indexed, indexPath };
66
+ }
67
+
68
+ export async function runCodegraph(argv) {
69
+ const options = parseArgs(argv);
70
+ const requestedPath = path.resolve(options.targetPath || ".");
71
+ const repoRoot = findRepoRoot(requestedPath);
72
+ if (!repoRoot) {
73
+ fail(`Not inside a Git repository: ${requestedPath}`);
74
+ }
75
+
76
+ if (options.subcommand === "status") {
77
+ const state = inspectCodegraphState(repoRoot);
78
+ if (options.json) {
79
+ console.log(JSON.stringify({ repoRoot, ...state }, null, 2));
80
+ return;
81
+ }
82
+ console.log(`CodeGraph CLI: ${state.installed ? "installed" : "not installed"}`);
83
+ console.log(`Local index: ${state.indexed ? `present (${state.indexPath})` : "not initialized"}`);
84
+ return;
85
+ }
86
+
87
+ const skipFlag = process.env.FEATFLOW_SKIP_CODEGRAPH || process.env.CELHIVE_SKIP_CODEGRAPH;
88
+ if (skipFlag === "1" || skipFlag === "true") {
89
+ console.log("Skipping CodeGraph bootstrap because FEATFLOW_SKIP_CODEGRAPH is set.");
90
+ return;
91
+ }
92
+
93
+ const allowInCi = process.env.FEATFLOW_CODEGRAPH_IN_CI === "1" || process.env.CELHIVE_CODEGRAPH_BOOTSTRAP_IN_CI === "1";
94
+ if (process.env.CI && !allowInCi) {
95
+ console.log("Skipping CodeGraph bootstrap in CI.");
96
+ return;
97
+ }
98
+
99
+ const state = inspectCodegraphState(repoRoot);
100
+ if (!state.installed) {
101
+ console.log("CodeGraph CLI is not installed; skipping local index bootstrap.");
102
+ return;
103
+ }
104
+
105
+ const action = state.indexed ? "sync" : "init";
106
+ console.log(`CodeGraph ${action === "init" ? "initializing" : "syncing"} index for ${repoRoot}`);
107
+ runWrite("codegraph", [action, repoRoot], repoRoot, { dryRun: options.dryRun });
108
+ }