delivrie-cli 0.3.0 → 0.4.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/dist/cli.js CHANGED
@@ -641,6 +641,8 @@ export async function run(argv, io = {}) {
641
641
  return r;
642
642
  };
643
643
  const index = (await (await get(`${base}/index.json`)).json());
644
+ // Older indexes listed file names only (SKILL.md); newer ones list every file with its hash.
645
+ const filesOf = (s) => s.files.map((f) => typeof f === "string" ? { path: f, sha256: s.sha256, url: f === "SKILL.md" ? s.url : `${base}/${s.name}/${f}` } : f);
644
646
  const manifestPath = path.join(dir, ".delivrie.json");
645
647
  const manifest = (() => { try {
646
648
  return JSON.parse(fs.readFileSync(manifestPath, "utf8"));
@@ -649,33 +651,62 @@ export async function run(argv, io = {}) {
649
651
  return { skills: {} };
650
652
  } })();
651
653
  manifest.skills ||= {};
654
+ const entry = (n) => { const e = manifest.skills[n]; return !e ? null : typeof e === "string" ? { sha256: e, files: ["SKILL.md"] } : e; };
652
655
  const sha = (b) => createHash("sha256").update(b).digest("hex");
653
- const fileOf = (n) => path.join(dir, n, "SKILL.md");
656
+ const safe = (n, rel) => {
657
+ const p = path.resolve(dir, n, rel);
658
+ if (!p.startsWith(path.resolve(dir, n) + path.sep))
659
+ throw new UsageError(`Refusing to write outside ${dir}: ${rel}`);
660
+ return p;
661
+ };
662
+ const combinedOf = (files) => sha(files.map((f) => `${f.path}:${f.sha256}\n`).join(""));
663
+ // The hash of what's on disk for these files (missing files count as changed).
664
+ const localHash = (n, files, legacy) => {
665
+ if (legacy)
666
+ return fs.existsSync(safe(n, "SKILL.md")) ? sha(fs.readFileSync(safe(n, "SKILL.md"))) : "";
667
+ return combinedOf(files.map((f) => ({ path: f, sha256: fs.existsSync(safe(n, f)) ? sha(fs.readFileSync(safe(n, f))) : "missing" })));
668
+ };
654
669
  // Each installed skill: missing, current, outdated (Delivrie published a newer one), edited (changed locally).
655
- const state = (n, remote) => {
656
- if (!fs.existsSync(fileOf(n)))
670
+ const state = (s) => {
671
+ if (!fs.existsSync(safe(s.name, "SKILL.md")))
657
672
  return "missing";
658
- const local = sha(fs.readFileSync(fileOf(n)));
659
- if (manifest.skills[n] && local !== manifest.skills[n])
673
+ const e = entry(s.name), legacy = !!e && typeof manifest.skills[s.name] === "string";
674
+ if (e && localHash(s.name, e.files, legacy) !== e.sha256)
660
675
  return "edited";
661
- return local === remote ? "current" : "outdated";
676
+ const remote = filesOf(s);
677
+ return localHash(s.name, remote.map((f) => f.path), false) === combinedOf(remote) ? "current" : "outdated";
662
678
  };
663
679
  const install = async (names) => {
664
680
  const done = [], kept = [];
665
681
  for (const s of index.skills.filter((x) => names.includes(x.name))) {
666
- const st = state(s.name, s.sha256);
682
+ const st = state(s), files = filesOf(s);
667
683
  if (st === "current") {
668
- manifest.skills[s.name] = s.sha256;
684
+ manifest.skills[s.name] = { sha256: combinedOf(files), files: files.map((f) => f.path) };
669
685
  continue;
670
686
  }
671
687
  if (st === "edited" && !flags.force) {
672
688
  kept.push(s.name);
673
689
  continue;
674
690
  }
675
- const body = Buffer.from(await (await get(s.url)).arrayBuffer());
676
- fs.mkdirSync(path.dirname(fileOf(s.name)), { recursive: true });
677
- fs.writeFileSync(fileOf(s.name), body);
678
- manifest.skills[s.name] = sha(body);
691
+ // Download every file first (8 at a time), so a failed download leaves the installed copy untouched.
692
+ const bodies = new Array(files.length);
693
+ let next = 0;
694
+ await Promise.all(Array.from({ length: Math.min(8, files.length) }, async () => {
695
+ while (next < files.length) {
696
+ const i = next++;
697
+ bodies[i] = [files[i], Buffer.from(await (await get(files[i].url)).arrayBuffer())];
698
+ }
699
+ }));
700
+ // Remove files an older version had that this one doesn't.
701
+ for (const old of entry(s.name)?.files || [])
702
+ if (!files.some((f) => f.path === old))
703
+ fs.rmSync(safe(s.name, old), { force: true });
704
+ for (const [f, body] of bodies) {
705
+ const p = safe(s.name, f.path);
706
+ fs.mkdirSync(path.dirname(p), { recursive: true });
707
+ fs.writeFileSync(p, body);
708
+ }
709
+ manifest.skills[s.name] = { sha256: combinedOf(bodies.map(([f, b]) => ({ path: f.path, sha256: sha(b) }))), files: files.map((f) => f.path) };
679
710
  done.push(s.name);
680
711
  }
681
712
  manifest.version = index.version;
@@ -713,7 +744,7 @@ export async function run(argv, io = {}) {
713
744
  return 0;
714
745
  }
715
746
  if (action === "status") {
716
- const rows = index.skills.map((s) => ({ name: s.name, status: state(s.name, s.sha256), tracked: !!manifest.skills[s.name] }))
747
+ const rows = index.skills.map((s) => ({ name: s.name, status: state(s), tracked: !!manifest.skills[s.name] }))
717
748
  .filter((r) => r.status !== "missing" || Object.keys(manifest.skills).length);
718
749
  if (flags.json)
719
750
  return json({ dir, latest: index.version, installed: manifest.version || null, skills: rows }), 0;
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "0.3.0";
1
+ export const VERSION = "0.4.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "delivrie-cli",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "The Delivrie command-line tool: send email, check deliveries, and manage domains, API keys, webhooks and templates from your terminal.",
5
5
  "keywords": [
6
6
  "delivrie",