sdtk-code-kit 0.1.1 → 0.1.2

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,337 @@
1
+ "use strict";
2
+
3
+ const path = require("path");
4
+ const { spawn } = require("child_process");
5
+ const { parseFlags, validateChoice } = require("./args");
6
+ const { CliError, DependencyError, ValidationError } = require("./errors");
7
+ const { VALID_SCOPES, defaultScope, isProjectScopeSupported } = require("./scope");
8
+
9
+ const PACKAGE_NAME = "sdtk-code-kit";
10
+ const CLI_NAME = "sdtk-code";
11
+ const CLI_BIN = path.resolve(__dirname, "..", "..", "bin", "sdtk-code.js");
12
+ const NPM_BIN = process.platform === "win32" ? "npm.cmd" : "npm";
13
+ const NPM_DISPLAY = "npm";
14
+ const NPM_VIEW_ARGS = ["view", PACKAGE_NAME, "version"];
15
+ const VALID_RUNTIMES = ["codex", "claude"];
16
+ const VERSION_PATTERN = /^\d+\.\d+\.\d+$/;
17
+ const FLAG_DEFS = {
18
+ version: { type: "string" },
19
+ runtime: { type: "string" },
20
+ scope: { type: "string" },
21
+ "project-path": { type: "string" },
22
+ "check-only": { type: "boolean" },
23
+ "skip-project-files": { type: "boolean" },
24
+ "skip-runtime-assets": { type: "boolean" },
25
+ verbose: { type: "boolean" },
26
+ };
27
+ const pkg = require("../../package.json");
28
+
29
+ let commandExecutor = defaultCommandExecutor;
30
+
31
+ function defaultCommandExecutor(command, args, options = {}) {
32
+ return new Promise((resolve, reject) => {
33
+ const child = spawn(command, args, {
34
+ cwd: options.cwd || process.cwd(),
35
+ env: options.env || process.env,
36
+ shell: options.shell || false,
37
+ windowsHide: true,
38
+ });
39
+ let stdout = "";
40
+ let stderr = "";
41
+
42
+ child.stdout.on("data", (chunk) => {
43
+ const text = chunk.toString();
44
+ stdout += text;
45
+ if (options.verbose) {
46
+ process.stdout.write(text);
47
+ }
48
+ });
49
+ child.stderr.on("data", (chunk) => {
50
+ const text = chunk.toString();
51
+ stderr += text;
52
+ if (options.verbose) {
53
+ process.stderr.write(text);
54
+ }
55
+ });
56
+ child.on("error", (error) => {
57
+ if (error && error.code === "ENOENT") {
58
+ reject(new DependencyError(`Required command not found in PATH: ${command}`));
59
+ return;
60
+ }
61
+ reject(error);
62
+ });
63
+ child.on("close", (exitCode) => {
64
+ resolve({
65
+ exitCode: typeof exitCode === "number" ? exitCode : 1,
66
+ stdout,
67
+ stderr,
68
+ });
69
+ });
70
+ });
71
+ }
72
+
73
+ function setCommandExecutorForTests(executor) {
74
+ commandExecutor = executor || defaultCommandExecutor;
75
+ }
76
+
77
+ function resetCommandExecutorForTests() {
78
+ commandExecutor = defaultCommandExecutor;
79
+ }
80
+
81
+ function quote(value) {
82
+ const text = String(value);
83
+ return /[\s"]/u.test(text) ? JSON.stringify(text) : text;
84
+ }
85
+
86
+ function formatCommand(command, args) {
87
+ return [command, ...args].map((value) => quote(value)).join(" ");
88
+ }
89
+
90
+ function validateVersion(targetVersion) {
91
+ if (targetVersion !== "latest" && !VERSION_PATTERN.test(targetVersion)) {
92
+ throw new ValidationError(
93
+ `Invalid value for --version: "${targetVersion}". Must be "latest" or x.y.z.`
94
+ );
95
+ }
96
+ }
97
+
98
+ function extractResolvedVersion(stdout) {
99
+ const lines = String(stdout || "")
100
+ .split(/\r?\n/u)
101
+ .map((line) => line.trim())
102
+ .filter(Boolean);
103
+ const candidate = (lines[lines.length - 1] || "").replace(/^['"]|['"]$/gu, "");
104
+ if (!VERSION_PATTERN.test(candidate)) {
105
+ throw new CliError(
106
+ `npm registry lookup returned an invalid version for ${PACKAGE_NAME}: "${candidate || "<empty>"}"`
107
+ );
108
+ }
109
+ return candidate;
110
+ }
111
+
112
+ async function resolveTargetVersion(options) {
113
+ if (options.requestedVersion !== "latest") {
114
+ return options.requestedVersion;
115
+ }
116
+
117
+ let result;
118
+ try {
119
+ result = await commandExecutor(NPM_BIN, NPM_VIEW_ARGS, {
120
+ verbose: options.verbose,
121
+ shell: process.platform === "win32",
122
+ });
123
+ } catch (error) {
124
+ throw new CliError(
125
+ `npm registry lookup failed for ${PACKAGE_NAME} while resolving --version latest.\n${error.message}`,
126
+ error.exitCode || 4
127
+ );
128
+ }
129
+
130
+ if (result.exitCode !== 0) {
131
+ const detail = (result.stderr || result.stdout || "").trim();
132
+ throw new CliError(
133
+ `npm registry lookup failed for ${PACKAGE_NAME} while resolving --version latest (exit code ${result.exitCode}).${detail ? `\n${detail}` : ""}`
134
+ );
135
+ }
136
+
137
+ try {
138
+ return extractResolvedVersion(result.stdout);
139
+ } catch (error) {
140
+ throw new CliError(
141
+ `npm registry lookup failed for ${PACKAGE_NAME} while resolving --version latest.\n${error.message}`,
142
+ error.exitCode || 4
143
+ );
144
+ }
145
+ }
146
+
147
+ function buildPlan(options) {
148
+ const npmArgs = ["install", "-g", `${PACKAGE_NAME}@${options.targetVersion}`];
149
+ const plan = {
150
+ installedVersion: pkg.version,
151
+ requestedVersion: options.requestedVersion,
152
+ targetVersion: options.targetVersion,
153
+ updateNeeded: pkg.version !== options.targetVersion,
154
+ npmArgs,
155
+ npmCommand: formatCommand(NPM_DISPLAY, npmArgs),
156
+ runtime: options.runtime,
157
+ scope: options.scope,
158
+ projectPath: options.projectPath,
159
+ checkOnly: options.checkOnly,
160
+ projectRefreshArgs: null,
161
+ runtimeRefreshArgs: null,
162
+ projectRefreshCommand: "skipped (no --runtime provided)",
163
+ runtimeRefreshCommand: "skipped (no --runtime provided)",
164
+ };
165
+
166
+ if (!options.runtime) {
167
+ return plan;
168
+ }
169
+
170
+ if (options.skipProjectFiles) {
171
+ plan.projectRefreshCommand = "skipped (--skip-project-files)";
172
+ } else {
173
+ const initArgs = [
174
+ "init",
175
+ "--runtime",
176
+ options.runtime,
177
+ "--runtime-scope",
178
+ options.scope,
179
+ "--project-path",
180
+ options.projectPath,
181
+ "--force",
182
+ "--skip-runtime-assets",
183
+ ];
184
+ if (options.verbose) {
185
+ initArgs.push("--verbose");
186
+ }
187
+ plan.projectRefreshArgs = initArgs;
188
+ plan.projectRefreshCommand = formatCommand(CLI_NAME, initArgs);
189
+ }
190
+
191
+ if (options.skipRuntimeAssets) {
192
+ plan.runtimeRefreshCommand = "skipped (--skip-runtime-assets)";
193
+ } else {
194
+ const runtimeArgs = ["runtime", "install", "--runtime", options.runtime, "--scope", options.scope, "--force"];
195
+ if (options.scope === "project") {
196
+ runtimeArgs.push("--project-path", options.projectPath);
197
+ }
198
+ if (options.verbose) {
199
+ runtimeArgs.push("--verbose");
200
+ }
201
+ plan.runtimeRefreshArgs = runtimeArgs;
202
+ plan.runtimeRefreshCommand = formatCommand(CLI_NAME, runtimeArgs);
203
+ }
204
+
205
+ return plan;
206
+ }
207
+
208
+ function parseUpdateOptions(args) {
209
+ const { flags, positional } = parseFlags(args, FLAG_DEFS);
210
+
211
+ if (positional.length > 0) {
212
+ throw new ValidationError(`Unexpected arguments: ${positional.join(" ")}`);
213
+ }
214
+
215
+ const requestedVersion = flags.version || "latest";
216
+ validateVersion(requestedVersion);
217
+
218
+ const runtime = flags.runtime;
219
+ if (runtime) {
220
+ validateChoice(runtime, VALID_RUNTIMES, "runtime");
221
+ }
222
+ if (flags.scope && !runtime) {
223
+ throw new ValidationError("--scope requires --runtime.");
224
+ }
225
+
226
+ const scope = runtime ? (flags.scope || defaultScope(runtime)) : undefined;
227
+ if (scope) {
228
+ validateChoice(scope, VALID_SCOPES, "scope");
229
+ if (scope === "project" && !isProjectScopeSupported(runtime)) {
230
+ throw new ValidationError(
231
+ `${runtime} does not support project-local runtime assets. Use --scope user instead.`
232
+ );
233
+ }
234
+ }
235
+
236
+ return {
237
+ requestedVersion,
238
+ runtime,
239
+ scope,
240
+ projectPath: path.resolve(flags["project-path"] || process.cwd()),
241
+ checkOnly: flags["check-only"],
242
+ skipProjectFiles: flags["skip-project-files"],
243
+ skipRuntimeAssets: flags["skip-runtime-assets"],
244
+ verbose: flags.verbose,
245
+ };
246
+ }
247
+
248
+ function printPlan(plan) {
249
+ console.log("SDTK-CODE update plan");
250
+ console.log(` Installed package version: ${plan.installedVersion}`);
251
+ console.log(` Requested package version: ${plan.requestedVersion}`);
252
+ console.log(` Target package version: ${plan.targetVersion}`);
253
+ console.log(
254
+ ` Package update needed: ${plan.updateNeeded ? "yes" : `no (already installed: ${plan.installedVersion})`}`
255
+ );
256
+ console.log(` Package refresh command: ${plan.npmCommand}`);
257
+ if (plan.runtime) {
258
+ console.log(` Runtime: ${plan.runtime}`);
259
+ console.log(` Scope: ${plan.scope}`);
260
+ console.log(` Project path: ${plan.projectPath}`);
261
+ }
262
+ console.log(` Project file refresh: ${plan.projectRefreshCommand}`);
263
+ console.log(` Runtime asset refresh: ${plan.runtimeRefreshCommand}`);
264
+ console.log(
265
+ ` Mode: ${plan.checkOnly ? "check-only (no changes applied)" : "apply"}`
266
+ );
267
+ }
268
+
269
+ async function runCommand(label, command, args, options) {
270
+ const result = await commandExecutor(command, args, options);
271
+ if (result.exitCode !== 0) {
272
+ const detail = (result.stderr || result.stdout || "").trim();
273
+ throw new CliError(
274
+ `${label} failed (exit code ${result.exitCode}).${detail ? `\n${detail}` : ""}`
275
+ );
276
+ }
277
+ return result;
278
+ }
279
+
280
+ async function applyPlan(plan, options) {
281
+ console.log("");
282
+ console.log("Applying SDTK-CODE update...");
283
+ if (plan.updateNeeded) {
284
+ console.log(` npm refresh: ${plan.npmCommand}`);
285
+ await runCommand("npm package refresh", NPM_BIN, plan.npmArgs, {
286
+ verbose: options.verbose,
287
+ shell: process.platform === "win32",
288
+ });
289
+ } else {
290
+ console.log(` npm refresh: skipped (installed package already matches ${plan.targetVersion})`);
291
+ }
292
+
293
+ if (plan.projectRefreshArgs) {
294
+ console.log(` project refresh: ${plan.projectRefreshCommand}`);
295
+ await runCommand("project file refresh", process.execPath, [CLI_BIN, ...plan.projectRefreshArgs], {
296
+ verbose: options.verbose,
297
+ });
298
+ } else {
299
+ console.log(` project refresh: ${plan.projectRefreshCommand}`);
300
+ }
301
+
302
+ if (plan.runtimeRefreshArgs) {
303
+ console.log(` runtime refresh: ${plan.runtimeRefreshCommand}`);
304
+ await runCommand("runtime asset refresh", process.execPath, [CLI_BIN, ...plan.runtimeRefreshArgs], {
305
+ verbose: options.verbose,
306
+ });
307
+ } else {
308
+ console.log(` runtime refresh: ${plan.runtimeRefreshCommand}`);
309
+ }
310
+
311
+ console.log("");
312
+ console.log("SDTK-CODE update completed successfully.");
313
+ }
314
+
315
+ async function executeUpdate(args) {
316
+ const options = parseUpdateOptions(args);
317
+ const targetVersion = await resolveTargetVersion(options);
318
+ const plan = buildPlan({ ...options, targetVersion });
319
+ printPlan(plan);
320
+
321
+ if (options.checkOnly) {
322
+ return 0;
323
+ }
324
+
325
+ await applyPlan(plan, options);
326
+ return 0;
327
+ }
328
+
329
+ module.exports = {
330
+ buildPlan,
331
+ executeUpdate,
332
+ formatCommand,
333
+ parseUpdateOptions,
334
+ resetCommandExecutorForTests,
335
+ resolveTargetVersion,
336
+ setCommandExecutorForTests,
337
+ };
@@ -210,6 +210,11 @@ function renderVerifyBlock(data) {
210
210
  return [
211
211
  "<!-- SDTK-CODE:VERIFY:START -->",
212
212
  "## 6. Evidence And Review Notes",
213
+ "### Review Packet",
214
+ data.reviewPacketPath
215
+ ? `- Review packet: \`${data.reviewPacketPath}\``
216
+ : "- Review packet not generated yet.",
217
+ "",
213
218
  "### Evidence Summary",
214
219
  renderEvidenceList(data.evidenceEntries),
215
220
  "",
@@ -459,6 +464,7 @@ function createOrUpdateWorkflowArtifact(options) {
459
464
  shipFollowUps: options.shipFollowUps || [],
460
465
  shipNotes: options.shipNotes || [],
461
466
  shipDebugNotes: options.shipDebugNotes || [],
467
+ reviewPacketPath: options.reviewPacketPath || null,
462
468
  intakeSource: options.intakeSource || "legacy_compatibility",
463
469
  handoffStatus: options.handoffStatus || "missing",
464
470
  compatibilityWarnings: options.compatibilityWarnings || [],