@moglenny/content-workbench-agent 0.1.0 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/bin.mjs +71 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moglenny/content-workbench-agent",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Agent installer for Content Workbench CLI, Skills, and MCP configuration",
5
5
  "type": "module",
6
6
  "bin": {
package/src/bin.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { spawn } from "node:child_process";
4
4
  import { realpathSync } from "node:fs";
5
- import { mkdir, stat } from "node:fs/promises";
5
+ import { mkdir, rm, stat } from "node:fs/promises";
6
6
  import os from "node:os";
7
7
  import path from "node:path";
8
8
  import { fileURLToPath } from "node:url";
@@ -10,6 +10,16 @@ import { fileURLToPath } from "node:url";
10
10
  const scriptPath = fileURLToPath(import.meta.url);
11
11
  const defaultRepoUrl = "https://github.com/Himog0921/topic-dashboard.git";
12
12
  const defaultBranch = "main";
13
+ const cloneRetryAttempts = 3;
14
+ const sparseCheckoutPaths = [
15
+ ".cw-agent",
16
+ ".claude/skills",
17
+ "bin",
18
+ "packages/cli",
19
+ "packages/mcp-server",
20
+ "scripts",
21
+ "public/releases/cw",
22
+ ];
13
23
 
14
24
  function expandHome(filePath) {
15
25
  if (filePath === "~") return os.homedir();
@@ -108,7 +118,22 @@ export function buildInstallPlan(options, repoExists) {
108
118
  : [
109
119
  {
110
120
  command: "git",
111
- args: ["clone", "--depth", "1", "--branch", options.branch, options.repoUrl, options.repoDir],
121
+ args: [
122
+ "clone",
123
+ "--depth",
124
+ "1",
125
+ "--filter=blob:none",
126
+ "--sparse",
127
+ "--single-branch",
128
+ "--branch",
129
+ options.branch,
130
+ options.repoUrl,
131
+ options.repoDir,
132
+ ],
133
+ },
134
+ {
135
+ command: "git",
136
+ args: ["-C", options.repoDir, "sparse-checkout", "set", "--cone", ...sparseCheckoutPaths],
112
137
  },
113
138
  ];
114
139
 
@@ -157,6 +182,41 @@ async function run(command, args) {
157
182
  });
158
183
  }
159
184
 
185
+ function gitVerb(step) {
186
+ if (step.command !== "git") return null;
187
+ if (step.args[0] === "-C") return step.args[2] || null;
188
+ return step.args[0] || null;
189
+ }
190
+
191
+ function shouldRetryStep(step) {
192
+ return ["clone", "fetch", "pull"].includes(gitVerb(step));
193
+ }
194
+
195
+ function isCloneStep(step) {
196
+ return step.command === "git" && step.args[0] === "clone";
197
+ }
198
+
199
+ function sleep(ms) {
200
+ return new Promise((resolve) => setTimeout(resolve, ms));
201
+ }
202
+
203
+ async function runStepWithRetry(step, options) {
204
+ const maxAttempts = shouldRetryStep(step) ? cloneRetryAttempts : 1;
205
+ for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
206
+ try {
207
+ await options.runner(step.command, step.args);
208
+ return;
209
+ } catch (error) {
210
+ if (attempt >= maxAttempts) throw error;
211
+ if (isCloneStep(step)) {
212
+ await options.removeDir(options.repoDir, { recursive: true, force: true });
213
+ }
214
+ options.stdout.write(`Retrying interrupted ${step.command} ${gitVerb(step)} (${attempt + 1}/${maxAttempts})...\n`);
215
+ await options.sleep(attempt * 1000);
216
+ }
217
+ }
218
+ }
219
+
160
220
  function printPlan(plan, stdout) {
161
221
  stdout.write(`${plan.dryRun ? "Content Workbench Agent install dry run" : "Content Workbench Agent install"}\n`);
162
222
  stdout.write(`Managed repository: ${plan.repoDir}\n`);
@@ -183,8 +243,16 @@ export async function runAgentInstaller(argv = process.argv.slice(2), io = proce
183
243
  if (options.dryRun) return plan;
184
244
 
185
245
  const runner = io.run || run;
246
+ const removeDir = io.removeDir || rm;
247
+ const wait = io.sleep || sleep;
186
248
  for (const step of plan.steps) {
187
- await runner(step.command, step.args);
249
+ await runStepWithRetry(step, {
250
+ runner,
251
+ removeDir,
252
+ repoDir: options.repoDir,
253
+ sleep: wait,
254
+ stdout: io.stdout,
255
+ });
188
256
  }
189
257
 
190
258
  io.stdout.write("Content Workbench Agent install complete\n");