sdtk-code-kit 0.3.0 → 0.3.1
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.
- package/README.md +204 -204
- package/assets/manifest/toolkit-bundle.manifest.json +147 -147
- package/assets/manifest/toolkit-bundle.sha256.txt +5 -5
- package/bin/sdtk-code.js +14 -14
- package/package.json +44 -44
- package/src/commands/agent-team.js +425 -174
- package/src/commands/build.js +53 -53
- package/src/commands/doctor.js +51 -51
- package/src/commands/generate.js +11 -11
- package/src/commands/guardrails.js +215 -215
- package/src/commands/help.js +264 -257
- package/src/commands/init.js +98 -98
- package/src/commands/plan.js +53 -53
- package/src/commands/readiness.js +77 -77
- package/src/commands/resume.js +61 -61
- package/src/commands/runtime.js +224 -224
- package/src/commands/ship.js +59 -59
- package/src/commands/sleep.js +132 -132
- package/src/commands/start.js +89 -89
- package/src/commands/status.js +77 -77
- package/src/commands/update.js +11 -11
- package/src/commands/verify.js +71 -71
- package/src/index.js +112 -112
- package/src/lib/agent-team-author.js +521 -0
- package/src/lib/agent-team-evidence.js +302 -0
- package/src/lib/agent-team-execute.js +892 -0
- package/src/lib/agent-team-export.js +411 -409
- package/src/lib/agent-team-plan.js +514 -514
- package/src/lib/agent-team-readiness.js +161 -0
- package/src/lib/agent-team-report.js +165 -0
- package/src/lib/args.js +107 -107
- package/src/lib/errors.js +41 -41
- package/src/lib/model-provider.js +219 -0
- package/src/lib/powershell.js +65 -65
- package/src/lib/project-target-guard.js +53 -53
- package/src/lib/scope.js +72 -72
- package/src/lib/ship-readiness.js +281 -281
- package/src/lib/sleep-plan.js +175 -175
- package/src/lib/sleep-report.js +229 -229
- package/src/lib/toolkit-payload.js +72 -72
- package/src/lib/trust-events.js +252 -185
- package/src/lib/trust-file-scope.js +154 -154
- package/src/lib/trust-policy.js +212 -212
- package/src/lib/trust-redact.js +58 -35
- package/src/lib/trust-trace.js +179 -179
- package/src/lib/update.js +344 -344
- package/src/lib/workflow-artifact.js +503 -503
- package/src/lib/workflow-build.js +127 -127
- package/src/lib/workflow-doctor.js +83 -83
- package/src/lib/workflow-handoff.js +1029 -1029
- package/src/lib/workflow-intake.js +101 -101
- package/src/lib/workflow-plan.js +193 -193
- package/src/lib/workflow-review-packet.js +650 -650
- package/src/lib/workflow-ship.js +213 -213
- package/src/lib/workflow-state.js +438 -438
- package/src/lib/workflow-verify.js +393 -393
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { ValidationError } = require("./errors");
|
|
6
|
+
const { safeSegment } = require("./agent-team-export");
|
|
7
|
+
const { loadAgentTeamPlan } = require("./agent-team-plan");
|
|
8
|
+
const {
|
|
9
|
+
asArray,
|
|
10
|
+
assertPathUnderBase,
|
|
11
|
+
atomicWriteJson,
|
|
12
|
+
resolveEvidencePath,
|
|
13
|
+
} = require("./agent-team-evidence");
|
|
14
|
+
|
|
15
|
+
const READINESS_SCHEMA_VERSION = "sdtk.agent-team-readiness.v1";
|
|
16
|
+
const READINESS_NOTE =
|
|
17
|
+
"READY_TO_SLEEP indicates evidence is present and well-formed — it does NOT verify implementation correctness. Human review required before acting on this verdict.";
|
|
18
|
+
|
|
19
|
+
function readinessBaseDir(projectPath) {
|
|
20
|
+
return path.resolve(projectPath || ".", ".sdtk", "agent-team", "readiness");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function resolveReadinessPath(projectPath, featureKey) {
|
|
24
|
+
const safeFeatureKey = safeSegment(featureKey, "feature_key");
|
|
25
|
+
const base = readinessBaseDir(projectPath);
|
|
26
|
+
const dir = assertPathUnderBase(path.join(base, safeFeatureKey), base);
|
|
27
|
+
return assertPathUnderBase(path.join(dir, "readiness.json"), base);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function loadSleepPlanStopConditions(projectPath) {
|
|
31
|
+
const sleepPlanPath = path.resolve(projectPath || ".", ".sdtk", "trust", "sleep-plan.json");
|
|
32
|
+
let sleepPlan;
|
|
33
|
+
try {
|
|
34
|
+
sleepPlan = JSON.parse(fs.readFileSync(sleepPlanPath, "utf8"));
|
|
35
|
+
} catch (_error) {
|
|
36
|
+
throw new ValidationError(
|
|
37
|
+
`NEEDS_SLEEP_PLAN: no sleep plan at ${sleepPlanPath}. Run \`sdtk-code sleep plan\` first.`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const ids = asArray(sleepPlan.stop_conditions)
|
|
41
|
+
.map((condition) => {
|
|
42
|
+
if (condition && typeof condition === "object") return condition.id;
|
|
43
|
+
return condition;
|
|
44
|
+
})
|
|
45
|
+
.filter((id) => typeof id === "string" && id.length > 0);
|
|
46
|
+
if (ids.length === 0) {
|
|
47
|
+
throw new ValidationError(`NEEDS_SLEEP_PLAN: sleep plan at ${sleepPlanPath} has no stop_conditions.`);
|
|
48
|
+
}
|
|
49
|
+
return ids;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function loadEvidenceFile(projectPath, featureKey, role) {
|
|
53
|
+
const evidencePath = resolveEvidencePath(projectPath, featureKey, role);
|
|
54
|
+
if (!fs.existsSync(evidencePath)) {
|
|
55
|
+
return { evidencePath, evidence: null };
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
return { evidencePath, evidence: JSON.parse(fs.readFileSync(evidencePath, "utf8")) };
|
|
59
|
+
} catch (_error) {
|
|
60
|
+
return { evidencePath, evidence: null, invalid: true };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function scoreRole(projectPath, plan, roleSpec, activeStopConditions) {
|
|
65
|
+
const role = roleSpec && roleSpec.role;
|
|
66
|
+
const { evidencePath, evidence, invalid } = loadEvidenceFile(projectPath, plan.feature_key, role);
|
|
67
|
+
if (!evidence) {
|
|
68
|
+
return {
|
|
69
|
+
role,
|
|
70
|
+
verdict: "NOT_READY",
|
|
71
|
+
reason: invalid
|
|
72
|
+
? `evidence file invalid — ${evidencePath} could not be parsed`
|
|
73
|
+
: `evidence file missing — no ${String(role).toUpperCase()}.evidence.json submitted`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const stopConditionHit = evidence.stop_condition_hit === undefined ? null : evidence.stop_condition_hit;
|
|
78
|
+
if (stopConditionHit !== null) {
|
|
79
|
+
if (!activeStopConditions.includes(stopConditionHit)) {
|
|
80
|
+
return {
|
|
81
|
+
role,
|
|
82
|
+
verdict: "NOT_READY",
|
|
83
|
+
reason: `unrecognized stop_condition_hit=${stopConditionHit} is not active in sleep-plan.json`,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
role,
|
|
88
|
+
verdict: "BLOCKED_BY_STOP_CONDITION",
|
|
89
|
+
reason: `stop_condition_hit=${stopConditionHit}`,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const evidenceObject = evidence.evidence && typeof evidence.evidence === "object" ? evidence.evidence : {};
|
|
94
|
+
const missing = asArray(roleSpec.evidence_required).filter((key) => !(key in evidenceObject));
|
|
95
|
+
if (missing.length > 0) {
|
|
96
|
+
return {
|
|
97
|
+
role,
|
|
98
|
+
verdict: "NOT_READY",
|
|
99
|
+
reason: `required evidence missing: ${missing.join(", ")}`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return { role, verdict: "READY", reason: null };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function overallReason(roles) {
|
|
107
|
+
const notReady = roles.filter((role) => role.verdict !== "READY");
|
|
108
|
+
if (notReady.length === 0) {
|
|
109
|
+
return "All required role evidence is present and well-formed.";
|
|
110
|
+
}
|
|
111
|
+
if (notReady.length === 1) {
|
|
112
|
+
return `${notReady[0].role} is ${notReady[0].verdict}: ${notReady[0].reason}`;
|
|
113
|
+
}
|
|
114
|
+
return `${notReady.length} roles are not ready.`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function computeReadiness(projectPath, featureKey) {
|
|
118
|
+
const safeFeatureKey = safeSegment(featureKey, "feature_key");
|
|
119
|
+
const plan = loadAgentTeamPlan(projectPath, safeFeatureKey);
|
|
120
|
+
const activeStopConditions = loadSleepPlanStopConditions(projectPath);
|
|
121
|
+
const roles = asArray(plan.roles).map((roleSpec) => scoreRole(projectPath, plan, roleSpec, activeStopConditions));
|
|
122
|
+
const rolesNotReady = roles.filter((role) => role.verdict !== "READY");
|
|
123
|
+
const overallVerdict = rolesNotReady.length === 0 ? "READY_TO_SLEEP" : "NOT_READY";
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
schema_version: READINESS_SCHEMA_VERSION,
|
|
127
|
+
feature_key: plan.feature_key,
|
|
128
|
+
run_id: plan.run_id,
|
|
129
|
+
evaluated_at: new Date().toISOString(),
|
|
130
|
+
roles,
|
|
131
|
+
overall_verdict: overallVerdict,
|
|
132
|
+
overall_reason: overallReason(roles),
|
|
133
|
+
readiness_note: READINESS_NOTE,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function writeReadiness(projectPath, featureKey, readinessJson) {
|
|
138
|
+
const outputPath = resolveReadinessPath(projectPath, featureKey);
|
|
139
|
+
return atomicWriteJson(outputPath, readinessJson, readinessBaseDir(projectPath));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function loadReadiness(projectPath, featureKey) {
|
|
143
|
+
const outputPath = resolveReadinessPath(projectPath, featureKey);
|
|
144
|
+
try {
|
|
145
|
+
return JSON.parse(fs.readFileSync(outputPath, "utf8"));
|
|
146
|
+
} catch (_error) {
|
|
147
|
+
throw new ValidationError(`NEEDS_READINESS: no readiness.json at ${outputPath}. Run \`sdtk-code agent-team readiness --feature-key ${featureKey}\` first.`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
module.exports = {
|
|
152
|
+
READINESS_SCHEMA_VERSION,
|
|
153
|
+
READINESS_NOTE,
|
|
154
|
+
readinessBaseDir,
|
|
155
|
+
resolveReadinessPath,
|
|
156
|
+
loadSleepPlanStopConditions,
|
|
157
|
+
scoreRole,
|
|
158
|
+
computeReadiness,
|
|
159
|
+
writeReadiness,
|
|
160
|
+
loadReadiness,
|
|
161
|
+
};
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { safeSegment } = require("./agent-team-export");
|
|
6
|
+
const { loadAgentTeamPlan } = require("./agent-team-plan");
|
|
7
|
+
const {
|
|
8
|
+
asArray,
|
|
9
|
+
assertPathUnderBase,
|
|
10
|
+
atomicWriteText,
|
|
11
|
+
resolveEvidencePath,
|
|
12
|
+
} = require("./agent-team-evidence");
|
|
13
|
+
const { READINESS_NOTE, loadReadiness } = require("./agent-team-readiness");
|
|
14
|
+
|
|
15
|
+
function reportBaseDir(projectPath) {
|
|
16
|
+
return path.resolve(projectPath || ".", ".sdtk", "agent-team", "reports");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function resolveReportPath(projectPath, featureKey) {
|
|
20
|
+
const safeFeatureKey = safeSegment(featureKey, "feature_key");
|
|
21
|
+
const base = reportBaseDir(projectPath);
|
|
22
|
+
const dir = assertPathUnderBase(path.join(base, safeFeatureKey), base);
|
|
23
|
+
return assertPathUnderBase(path.join(dir, "morning_report.md"), base);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function loadEvidenceMap(projectPath, plan) {
|
|
27
|
+
const evidenceMap = {};
|
|
28
|
+
for (const roleSpec of asArray(plan && plan.roles)) {
|
|
29
|
+
const role = roleSpec.role;
|
|
30
|
+
const evidencePath = resolveEvidencePath(projectPath, plan.feature_key, role);
|
|
31
|
+
if (!fs.existsSync(evidencePath)) {
|
|
32
|
+
evidenceMap[role] = null;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
evidenceMap[role] = JSON.parse(fs.readFileSync(evidencePath, "utf8"));
|
|
37
|
+
} catch (_error) {
|
|
38
|
+
evidenceMap[role] = { _invalid: true };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return evidenceMap;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function bulletList(items, fallback) {
|
|
45
|
+
if (!Array.isArray(items) || items.length === 0) {
|
|
46
|
+
return `- ${fallback}`;
|
|
47
|
+
}
|
|
48
|
+
return items.map((item) => `- ${String(item)}`).join("\n");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function roleStatus(readinessJson, role) {
|
|
52
|
+
return asArray(readinessJson && readinessJson.roles).find((entry) => entry.role === role) || {
|
|
53
|
+
role,
|
|
54
|
+
verdict: "NOT_READY",
|
|
55
|
+
reason: "role missing from readiness.json",
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function renderEvidenceDetail(roleSpec, evidence) {
|
|
60
|
+
const role = roleSpec.role;
|
|
61
|
+
if (!evidence) {
|
|
62
|
+
return [
|
|
63
|
+
`### ${role}`,
|
|
64
|
+
"",
|
|
65
|
+
"- Evidence: not submitted",
|
|
66
|
+
"",
|
|
67
|
+
].join("\n");
|
|
68
|
+
}
|
|
69
|
+
if (evidence._invalid) {
|
|
70
|
+
return [
|
|
71
|
+
`### ${role}`,
|
|
72
|
+
"",
|
|
73
|
+
"- Evidence: invalid JSON",
|
|
74
|
+
"",
|
|
75
|
+
].join("\n");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const lines = [
|
|
79
|
+
`### ${role}`,
|
|
80
|
+
"",
|
|
81
|
+
`- Summary: ${evidence.summary || "(none)"}`,
|
|
82
|
+
"- Commands run:",
|
|
83
|
+
bulletList(evidence.commands_run, "none"),
|
|
84
|
+
`- Verification evidence: ${evidence.verification_evidence || "(none)"}`,
|
|
85
|
+
"- Blockers:",
|
|
86
|
+
bulletList(evidence.blockers, "none"),
|
|
87
|
+
"- Files touched:",
|
|
88
|
+
bulletList(evidence.files_touched, "none declared"),
|
|
89
|
+
`- Stop condition hit: ${evidence.stop_condition_hit === null || evidence.stop_condition_hit === undefined ? "none" : evidence.stop_condition_hit}`,
|
|
90
|
+
];
|
|
91
|
+
|
|
92
|
+
if (evidence.commit_range) {
|
|
93
|
+
lines.push("- Commit range:");
|
|
94
|
+
lines.push(`- before: ${evidence.commit_range.before || "(none)"}`);
|
|
95
|
+
lines.push(`- after: ${evidence.commit_range.after || "(none)"}`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
lines.push("");
|
|
99
|
+
return lines.join("\n");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function renderMorningReport(plan, evidenceMap, readinessJson) {
|
|
103
|
+
const rows = asArray(plan && plan.roles).map((roleSpec) => {
|
|
104
|
+
const status = roleStatus(readinessJson, roleSpec.role);
|
|
105
|
+
return `| ${roleSpec.role} | ${status.verdict} | ${status.reason || ""} |`;
|
|
106
|
+
});
|
|
107
|
+
const details = asArray(plan && plan.roles).map((roleSpec) => renderEvidenceDetail(roleSpec, evidenceMap[roleSpec.role]));
|
|
108
|
+
|
|
109
|
+
return [
|
|
110
|
+
`# Agent Team Morning Report — ${plan.feature_key} (${plan.run_id})`,
|
|
111
|
+
"",
|
|
112
|
+
`Generated: ${new Date().toISOString()}`,
|
|
113
|
+
"",
|
|
114
|
+
"## Overall Verdict",
|
|
115
|
+
"",
|
|
116
|
+
"```text",
|
|
117
|
+
readinessJson.overall_verdict,
|
|
118
|
+
"```",
|
|
119
|
+
"",
|
|
120
|
+
`Reason: ${readinessJson.overall_reason || "(none)"}`,
|
|
121
|
+
"",
|
|
122
|
+
"## Readiness Note",
|
|
123
|
+
"",
|
|
124
|
+
readinessJson.readiness_note || READINESS_NOTE,
|
|
125
|
+
"",
|
|
126
|
+
"## Role Evidence Status",
|
|
127
|
+
"",
|
|
128
|
+
"| Role | Verdict | Reason |",
|
|
129
|
+
"| --- | --- | --- |",
|
|
130
|
+
...(rows.length > 0 ? rows : ["| (none) | NOT_READY | no roles in plan |"]),
|
|
131
|
+
"",
|
|
132
|
+
"## Evidence Details",
|
|
133
|
+
"",
|
|
134
|
+
...details,
|
|
135
|
+
"## Next steps",
|
|
136
|
+
"",
|
|
137
|
+
"- Review evidence files before treating READY_TO_SLEEP as final.",
|
|
138
|
+
"- Check code, tests, and product behavior manually before shipping.",
|
|
139
|
+
"- Resolve any NOT_READY or BLOCKED_BY_STOP_CONDITION role before relying on this report.",
|
|
140
|
+
"- This report records submitted evidence only; it does not execute or verify implementation correctness.",
|
|
141
|
+
"",
|
|
142
|
+
].join("\n");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function buildMorningReport(projectPath, featureKey) {
|
|
146
|
+
const safeFeatureKey = safeSegment(featureKey, "feature_key");
|
|
147
|
+
const plan = loadAgentTeamPlan(projectPath, safeFeatureKey);
|
|
148
|
+
const readinessJson = loadReadiness(projectPath, safeFeatureKey);
|
|
149
|
+
const evidenceMap = loadEvidenceMap(projectPath, plan);
|
|
150
|
+
return { plan, readinessJson, evidenceMap, markdown: renderMorningReport(plan, evidenceMap, readinessJson) };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function writeReport(projectPath, featureKey, markdown) {
|
|
154
|
+
const outputPath = resolveReportPath(projectPath, featureKey);
|
|
155
|
+
return atomicWriteText(outputPath, markdown, reportBaseDir(projectPath));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
module.exports = {
|
|
159
|
+
reportBaseDir,
|
|
160
|
+
resolveReportPath,
|
|
161
|
+
loadEvidenceMap,
|
|
162
|
+
renderMorningReport,
|
|
163
|
+
buildMorningReport,
|
|
164
|
+
writeReport,
|
|
165
|
+
};
|
package/src/lib/args.js
CHANGED
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { ValidationError } = require("./errors");
|
|
4
|
-
|
|
5
|
-
function parseFlags(argv, defs) {
|
|
6
|
-
const flags = {};
|
|
7
|
-
const positional = [];
|
|
8
|
-
const aliasMap = {};
|
|
9
|
-
|
|
10
|
-
for (const [name, def] of Object.entries(defs)) {
|
|
11
|
-
if (def.alias) {
|
|
12
|
-
aliasMap[def.alias] = name;
|
|
13
|
-
}
|
|
14
|
-
if (def.type === "boolean") {
|
|
15
|
-
flags[name] = false;
|
|
16
|
-
} else if (def.multiple) {
|
|
17
|
-
flags[name] = [];
|
|
18
|
-
} else {
|
|
19
|
-
flags[name] = undefined;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
let i = 0;
|
|
24
|
-
while (i < argv.length) {
|
|
25
|
-
const arg = argv[i];
|
|
26
|
-
|
|
27
|
-
if (!arg.startsWith("-")) {
|
|
28
|
-
positional.push(arg);
|
|
29
|
-
i++;
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let key;
|
|
34
|
-
let inlineValue;
|
|
35
|
-
if (arg.includes("=")) {
|
|
36
|
-
const eqIdx = arg.indexOf("=");
|
|
37
|
-
key = arg.slice(0, eqIdx);
|
|
38
|
-
inlineValue = arg.slice(eqIdx + 1);
|
|
39
|
-
} else {
|
|
40
|
-
key = arg;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const stripped = key.replace(/^-{1,2}/, "");
|
|
44
|
-
const resolved = aliasMap[stripped] || stripped;
|
|
45
|
-
if (!(resolved in defs)) {
|
|
46
|
-
throw new ValidationError(`Unknown flag: ${key}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const def = defs[resolved];
|
|
50
|
-
if (def.type === "boolean") {
|
|
51
|
-
flags[resolved] = true;
|
|
52
|
-
i++;
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let value = inlineValue;
|
|
57
|
-
if (value === undefined) {
|
|
58
|
-
i++;
|
|
59
|
-
if (i >= argv.length) {
|
|
60
|
-
throw new ValidationError(`Flag ${key} requires a value.`);
|
|
61
|
-
}
|
|
62
|
-
value = argv[i];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (def.multiple) {
|
|
66
|
-
flags[resolved].push(value);
|
|
67
|
-
} else {
|
|
68
|
-
flags[resolved] = value;
|
|
69
|
-
}
|
|
70
|
-
i++;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return { flags, positional };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function requireFlag(flags, name, label) {
|
|
77
|
-
const value = flags[name];
|
|
78
|
-
if (value === undefined || value === null || value === "") {
|
|
79
|
-
throw new ValidationError(`Missing required flag: --${label || name}`);
|
|
80
|
-
}
|
|
81
|
-
return value;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function validateChoice(value, choices, label) {
|
|
85
|
-
if (!choices.includes(value)) {
|
|
86
|
-
throw new ValidationError(
|
|
87
|
-
`Invalid value for --${label}: "${value}". Must be one of: ${choices.join(", ")}`
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
return value;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function validatePattern(value, regex, label, hint) {
|
|
94
|
-
if (!regex.test(value)) {
|
|
95
|
-
throw new ValidationError(
|
|
96
|
-
`Invalid value for --${label}: "${value}". ${hint || `Must match ${regex}`}`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
return value;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
module.exports = {
|
|
103
|
-
parseFlags,
|
|
104
|
-
requireFlag,
|
|
105
|
-
validateChoice,
|
|
106
|
-
validatePattern,
|
|
107
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { ValidationError } = require("./errors");
|
|
4
|
+
|
|
5
|
+
function parseFlags(argv, defs) {
|
|
6
|
+
const flags = {};
|
|
7
|
+
const positional = [];
|
|
8
|
+
const aliasMap = {};
|
|
9
|
+
|
|
10
|
+
for (const [name, def] of Object.entries(defs)) {
|
|
11
|
+
if (def.alias) {
|
|
12
|
+
aliasMap[def.alias] = name;
|
|
13
|
+
}
|
|
14
|
+
if (def.type === "boolean") {
|
|
15
|
+
flags[name] = false;
|
|
16
|
+
} else if (def.multiple) {
|
|
17
|
+
flags[name] = [];
|
|
18
|
+
} else {
|
|
19
|
+
flags[name] = undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let i = 0;
|
|
24
|
+
while (i < argv.length) {
|
|
25
|
+
const arg = argv[i];
|
|
26
|
+
|
|
27
|
+
if (!arg.startsWith("-")) {
|
|
28
|
+
positional.push(arg);
|
|
29
|
+
i++;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let key;
|
|
34
|
+
let inlineValue;
|
|
35
|
+
if (arg.includes("=")) {
|
|
36
|
+
const eqIdx = arg.indexOf("=");
|
|
37
|
+
key = arg.slice(0, eqIdx);
|
|
38
|
+
inlineValue = arg.slice(eqIdx + 1);
|
|
39
|
+
} else {
|
|
40
|
+
key = arg;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const stripped = key.replace(/^-{1,2}/, "");
|
|
44
|
+
const resolved = aliasMap[stripped] || stripped;
|
|
45
|
+
if (!(resolved in defs)) {
|
|
46
|
+
throw new ValidationError(`Unknown flag: ${key}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const def = defs[resolved];
|
|
50
|
+
if (def.type === "boolean") {
|
|
51
|
+
flags[resolved] = true;
|
|
52
|
+
i++;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let value = inlineValue;
|
|
57
|
+
if (value === undefined) {
|
|
58
|
+
i++;
|
|
59
|
+
if (i >= argv.length) {
|
|
60
|
+
throw new ValidationError(`Flag ${key} requires a value.`);
|
|
61
|
+
}
|
|
62
|
+
value = argv[i];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (def.multiple) {
|
|
66
|
+
flags[resolved].push(value);
|
|
67
|
+
} else {
|
|
68
|
+
flags[resolved] = value;
|
|
69
|
+
}
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return { flags, positional };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function requireFlag(flags, name, label) {
|
|
77
|
+
const value = flags[name];
|
|
78
|
+
if (value === undefined || value === null || value === "") {
|
|
79
|
+
throw new ValidationError(`Missing required flag: --${label || name}`);
|
|
80
|
+
}
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function validateChoice(value, choices, label) {
|
|
85
|
+
if (!choices.includes(value)) {
|
|
86
|
+
throw new ValidationError(
|
|
87
|
+
`Invalid value for --${label}: "${value}". Must be one of: ${choices.join(", ")}`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function validatePattern(value, regex, label, hint) {
|
|
94
|
+
if (!regex.test(value)) {
|
|
95
|
+
throw new ValidationError(
|
|
96
|
+
`Invalid value for --${label}: "${value}". ${hint || `Must match ${regex}`}`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
module.exports = {
|
|
103
|
+
parseFlags,
|
|
104
|
+
requireFlag,
|
|
105
|
+
validateChoice,
|
|
106
|
+
validatePattern,
|
|
107
|
+
};
|
package/src/lib/errors.js
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
class CliError extends Error {
|
|
4
|
-
constructor(message, exitCode = 4) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.name = this.constructor.name;
|
|
7
|
-
this.exitCode = exitCode;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
class ValidationError extends CliError {
|
|
12
|
-
constructor(message) {
|
|
13
|
-
super(message, 1);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class DependencyError extends CliError {
|
|
18
|
-
constructor(message) {
|
|
19
|
-
super(message, 2);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
class IntegrityError extends CliError {
|
|
24
|
-
constructor(message) {
|
|
25
|
-
super(message, 3);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
class InternalError extends CliError {
|
|
30
|
-
constructor(message) {
|
|
31
|
-
super(message, 4);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
module.exports = {
|
|
36
|
-
CliError,
|
|
37
|
-
ValidationError,
|
|
38
|
-
DependencyError,
|
|
39
|
-
IntegrityError,
|
|
40
|
-
InternalError,
|
|
41
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
class CliError extends Error {
|
|
4
|
+
constructor(message, exitCode = 4) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = this.constructor.name;
|
|
7
|
+
this.exitCode = exitCode;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class ValidationError extends CliError {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message, 1);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class DependencyError extends CliError {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message, 2);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class IntegrityError extends CliError {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message, 3);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class InternalError extends CliError {
|
|
30
|
+
constructor(message) {
|
|
31
|
+
super(message, 4);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
CliError,
|
|
37
|
+
ValidationError,
|
|
38
|
+
DependencyError,
|
|
39
|
+
IntegrityError,
|
|
40
|
+
InternalError,
|
|
41
|
+
};
|