@agentage/cli 0.79.0 → 0.80.0

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/dist/commands/daemon/installer.d.ts +2 -0
  2. package/dist/commands/daemon/installer.js +12 -0
  3. package/dist/commands/daemon/repo-installer.d.ts +13 -0
  4. package/dist/commands/daemon/repo-installer.js +93 -0
  5. package/dist/commands/daemon/repo-pins.d.ts +6 -0
  6. package/dist/commands/daemon/repo-pins.js +22 -0
  7. package/dist/commands/extension/available.d.ts +20 -0
  8. package/dist/commands/extension/available.js +44 -0
  9. package/dist/commands/extension/install-once.js +2 -1
  10. package/dist/commands/extension/install-repo.d.ts +7 -0
  11. package/dist/commands/extension/install-repo.js +30 -0
  12. package/dist/commands/extension/install-spec.js +12 -4
  13. package/dist/commands/extension/lifecycle-list.d.ts +3 -1
  14. package/dist/commands/extension/lifecycle-list.js +37 -11
  15. package/dist/commands/extension/lifecycle-repo.d.ts +16 -0
  16. package/dist/commands/extension/lifecycle-repo.js +66 -0
  17. package/dist/commands/extension/lifecycle.d.ts +2 -0
  18. package/dist/commands/extension/lifecycle.js +2 -1
  19. package/dist/commands/extension/list.d.ts +2 -0
  20. package/dist/commands/extension/list.js +3 -1
  21. package/dist/commands/extension/market-commands.d.ts +10 -0
  22. package/dist/commands/extension/market-commands.js +24 -0
  23. package/dist/commands/extension/market.d.ts +39 -0
  24. package/dist/commands/extension/market.js +89 -0
  25. package/dist/commands/extension/table.d.ts +1 -0
  26. package/dist/commands/extension/table.js +8 -0
  27. package/dist/core/installer/npm.d.ts +1 -0
  28. package/dist/core/installer/npm.js +1 -1
  29. package/dist/core/installer/resolve.js +3 -1
  30. package/dist/core/installer/tree.d.ts +1 -0
  31. package/dist/core/installer/tree.js +5 -4
  32. package/dist/core/loader/installed.d.ts +5 -0
  33. package/dist/core/loader/installed.js +6 -0
  34. package/dist/lib/json-codes.d.ts +2 -0
  35. package/dist/lib/json-codes.js +41 -0
  36. package/dist/lib/json-output.d.ts +2 -2
  37. package/dist/lib/json-output.js +2 -38
  38. package/dist/lib/market/repo-build.d.ts +7 -0
  39. package/dist/lib/market/repo-build.js +67 -0
  40. package/dist/lib/market/repo-cache.d.ts +25 -0
  41. package/dist/lib/market/repo-cache.js +96 -0
  42. package/dist/lib/market/repo-entries.d.ts +14 -0
  43. package/dist/lib/market/repo-entries.js +46 -0
  44. package/dist/lib/market/repo-git.d.ts +13 -0
  45. package/dist/lib/market/repo-git.js +76 -0
  46. package/dist/lib/market/repo-links.d.ts +4 -0
  47. package/dist/lib/market/repo-links.js +35 -0
  48. package/dist/lib/market/repo-manifest.d.ts +14 -0
  49. package/dist/lib/market/repo-manifest.js +73 -0
  50. package/dist/lib/market/repo-markets-file.d.ts +12 -0
  51. package/dist/lib/market/repo-markets-file.js +39 -0
  52. package/dist/lib/market/repo-resolve.d.ts +15 -0
  53. package/dist/lib/market/repo-resolve.js +55 -0
  54. package/dist/lib/market/repo-text.d.ts +2 -0
  55. package/dist/lib/market/repo-text.js +7 -0
  56. package/dist/lib/market/repo-version.d.ts +13 -0
  57. package/dist/lib/market/repo-version.js +35 -0
  58. package/dist/lib/npm/npm-cli.d.ts +1 -0
  59. package/dist/lib/npm/npm-cli.js +1 -1
  60. package/dist/program-deps.js +24 -1
  61. package/dist/program.d.ts +2 -0
  62. package/dist/program.js +6 -0
  63. package/package.json +1 -1
@@ -1,8 +1,10 @@
1
1
  import type { Installer } from '@agentage/apphost';
2
2
  import { type Npm, type NpmManifest } from '../../core/installer/npm.js';
3
3
  import { type Market } from '../../lib/net/market.js';
4
+ import { type RepoInstallDeps } from './repo-installer.js';
4
5
  export interface InstallerDeps {
5
6
  npm: () => Promise<Npm>;
7
+ repo?: RepoInstallDeps;
6
8
  manifest: () => Promise<NpmManifest>;
7
9
  market: () => Market;
8
10
  }
@@ -11,6 +11,8 @@ import { CliError, messageOf } from '../../lib/json-output.js';
11
11
  import {} from '../../lib/net/market.js';
12
12
  import { npmFallbackLine, noSiteMarket } from '../../lib/net/market-error.js';
13
13
  import { siteFqdn } from '../../lib/net/origins.js';
14
+ import { parseRepoVersion } from '../../lib/market/repo-version.js';
15
+ import { installRepo } from './repo-installer.js';
14
16
  // The kit sends an error with a string code to the client as that envelope (404, 502, else 400).
15
17
  export const installError = (code, status, message) => Object.assign(new Error(message), { code, status });
16
18
  // The market's own reason, in the daemon's case (market_failed is MARKET_FAILED).
@@ -95,6 +97,13 @@ const fetched = async (id, found, deps) => {
95
97
  throw installError('INSTALL_FAILED', 502, messageOf(err));
96
98
  });
97
99
  };
100
+ const fromRepo = async (id, version, ref, deps) => {
101
+ if (!deps.repo)
102
+ throw installError('VALIDATION_ERROR', 400, `${id}: no repo market installer`);
103
+ const dir = await installRepo(id, ref, version, { ...deps.repo, fail: installError });
104
+ checkId(dir, id);
105
+ return { id, version, dir };
106
+ };
98
107
  // A version on disk needs no network; else the market, then npm into one folder per version.
99
108
  export const cliInstaller = (deps) => async ({ id, version }) => {
100
109
  if (version !== undefined && !isVersionSpec(version))
@@ -102,6 +111,9 @@ export const cliInstaller = (deps) => async ({ id, version }) => {
102
111
  const have = version ? installedDir(id, version) : null;
103
112
  if (version && have)
104
113
  return { id, version, dir: have };
114
+ const repo = version ? parseRepoVersion(version) : null;
115
+ if (repo && version)
116
+ return fromRepo(id, version, repo, deps);
105
117
  const registry = registryLookup(deps.manifest, editableProfile().registries);
106
118
  const found = await exact(await inputOf(id, version, deps.market()), registry);
107
119
  const dir = packageDir(id, found.version) ?? (await fetched(id, found, deps));
@@ -0,0 +1,13 @@
1
+ import { type NpmStep } from '../../lib/market/repo-build.js';
2
+ import { type Git } from '../../lib/market/repo-git.js';
3
+ import { type RepoVersion } from '../../lib/market/repo-version.js';
4
+ export interface RepoInstallDeps {
5
+ git: Git;
6
+ step: NpmStep;
7
+ }
8
+ type InstallError = (code: string, status: 400 | 404 | 502, message: string) => Error;
9
+ type InstallDeps = RepoInstallDeps & {
10
+ fail: InstallError;
11
+ };
12
+ export declare const installRepo: (id: string, ref: RepoVersion, full: string, deps: InstallDeps) => Promise<string>;
13
+ export {};
@@ -0,0 +1,93 @@
1
+ import { mkdirSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { packageDir, packagesDir } from '../../core/installer/package-dir.js';
4
+ import { clearStaging, commitTree, discardTree, manifestText, stageTree, treePath, withInstallLock, } from '../../core/installer/tree.js';
5
+ import { messageOf } from '../../lib/json-output.js';
6
+ import { runBuild } from '../../lib/market/repo-build.js';
7
+ import { commitOf, exportFolder } from '../../lib/market/repo-cache.js';
8
+ import { offersAt } from '../../lib/market/repo-entries.js';
9
+ import {} from '../../lib/market/repo-git.js';
10
+ import { escapingLink } from '../../lib/market/repo-links.js';
11
+ import { marketNamed } from '../../lib/market/repo-markets-file.js';
12
+ import { repoVersion } from '../../lib/market/repo-version.js';
13
+ import { pinCommit } from './repo-pins.js';
14
+ const noEscape = (dir) => {
15
+ const link = escapingLink(dir);
16
+ if (link !== null)
17
+ throw new Error(`${link.shown} is a link that points outside the extension folder`);
18
+ };
19
+ // The tree's own package.json names the one package, as an npm tree does: packageDir reads it.
20
+ const stagedTree = (name, version) => {
21
+ clearStaging(packagesDir());
22
+ const manifest = manifestText({ [name]: version });
23
+ return stageTree(packagesDir(), { manifest, npmrc: '', lock: null });
24
+ };
25
+ // The folder at the commit, built in a staged tree; a failure leaves no tree behind.
26
+ const buildTree = async (offer, at, deps) => {
27
+ const name = offer.name ?? offer.id;
28
+ const staging = stagedTree(name, at.version);
29
+ const dir = join(staging, 'node_modules', name);
30
+ try {
31
+ mkdirSync(dir, { recursive: true });
32
+ await exportFolder(deps.git, at.repo, { commit: at.commit, path: offer.path }, dir);
33
+ noEscape(dir);
34
+ await runBuild(dir, deps.step);
35
+ noEscape(dir);
36
+ }
37
+ catch (err) {
38
+ discardTree(staging);
39
+ throw err;
40
+ }
41
+ return { staging, name };
42
+ };
43
+ const offerAt = async (id, at, git, fail) => {
44
+ const offers = await offersAt(git, { name: at.market, repo: at.repo, commit: at.commit });
45
+ const offer = offers.find((o) => o.id === id);
46
+ if (!offer)
47
+ throw fail('NOT_FOUND', 404, `${id}: the market ${at.market} has no ${id} at ${at.commit}`);
48
+ if (offer.problem)
49
+ throw fail('VALIDATION_ERROR', 400, `${at.market}/${id}: ${offer.problem}`);
50
+ const full = repoVersion(at.version, at.market, at.commit);
51
+ if (offer.version !== full)
52
+ throw fail('VALIDATION_ERROR', 400, `${id}@${full}: the package at that commit is ${offer.version}`);
53
+ return offer;
54
+ };
55
+ // The market by name and the commit in its clone: the cli fetched it, so no network here.
56
+ const cloneOf = async (id, ref, deps) => {
57
+ const market = marketNamed(ref.market);
58
+ if (!market)
59
+ throw deps.fail('MARKET_GONE', 404, `${id}: the market ${ref.market} is not on this machine`);
60
+ if (!(await commitOf(deps.git, market.repo, ref.commit)))
61
+ throw deps.fail('NOT_FOUND', 404, `${id}: commit ${ref.commit} is no longer in the clone of ${ref.market}; agentage extension update ${id} installs its newest ref`);
62
+ return { ...ref, repo: market.repo };
63
+ };
64
+ // A commit with no pin can go at the next gc, and a lost tree could not be built again.
65
+ const pinned = async (id, at, deps) => {
66
+ if (!(await pinCommit(deps.git, at, id)))
67
+ throw deps.fail('INSTALL_FAILED', 502, `${id}: cannot keep commit ${at.commit} in the clone of ${at.market}; nothing installed`);
68
+ };
69
+ // Pinned before the commit of the tree, so a failed pin leaves no tree a retry would reuse.
70
+ const builtTree = async (id, full, at, deps) => {
71
+ const built = await buildTree(at.offer, at, deps).catch((err) => {
72
+ throw deps.fail('BUILD_FAILED', 400, `${id}: ${messageOf(err)}`);
73
+ });
74
+ await pinned(id, at, deps).catch((err) => {
75
+ discardTree(built.staging);
76
+ throw err;
77
+ });
78
+ commitTree(packagesDir(), built.staging, `${id}@${full}`);
79
+ return join(treePath(packagesDir(), `${id}@${full}`), 'node_modules', built.name);
80
+ };
81
+ // `<version>+<market>.<commit>` is built from the clone, never from npm; pin and prune hold the lock.
82
+ export const installRepo = async (id, ref, full, deps) => {
83
+ const at = await cloneOf(id, ref, deps);
84
+ const offer = await offerAt(id, at, deps.git, deps.fail);
85
+ mkdirSync(packagesDir(), { recursive: true, mode: 0o700 });
86
+ return withInstallLock(packagesDir(), async () => {
87
+ const have = packageDir(id, full);
88
+ if (!have)
89
+ return builtTree(id, full, { ...at, offer }, deps);
90
+ await pinned(id, at, deps);
91
+ return have;
92
+ });
93
+ };
@@ -0,0 +1,6 @@
1
+ import { type Git } from '../../lib/market/repo-git.js';
2
+ export declare const pinCommit: (git: Git, at: {
3
+ repo: string;
4
+ market: string;
5
+ commit: string;
6
+ }, id: string) => Promise<boolean>;
@@ -0,0 +1,22 @@
1
+ import { existsSync, readdirSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { packagesDir } from '../../core/installer/package-dir.js';
4
+ import { installedVersions } from '../../core/loader/installed.js';
5
+ import { keepCommit, withCloneLock } from '../../lib/market/repo-cache.js';
6
+ import {} from '../../lib/market/repo-git.js';
7
+ import { parseRepoVersion } from '../../lib/market/repo-version.js';
8
+ // Each tree on disk as `<id>@<version>`: the daemon writes a row only after its tree is there.
9
+ const treesOnDisk = () => {
10
+ const trees = join(packagesDir(), 'trees');
11
+ return (existsSync(trees) ? readdirSync(trees) : []).flatMap((name) => {
12
+ const at = name.indexOf('@');
13
+ return at > 0 ? [{ id: name.slice(0, at), version: name.slice(at + 1) }] : [];
14
+ });
15
+ };
16
+ // `<id>/<commit>` of each row or tree from this market: the commits it still needs.
17
+ const usedPins = (market) => new Set([...installedVersions(), ...treesOnDisk()].flatMap(({ id, version }) => {
18
+ const at = parseRepoVersion(version);
19
+ return at?.market === market ? [`${id}/${at.commit}`] : [];
20
+ }));
21
+ // Pins the commit and drops the pins no row and no tree names; false when the pin failed.
22
+ export const pinCommit = (git, at, id) => withCloneLock(at.repo, () => keepCommit(git, at.repo, usedPins(at.market), { id, commit: at.commit }));
@@ -0,0 +1,20 @@
1
+ import { WithMeta } from '../../lib/json-output.js';
2
+ import { type RepoDeps } from '../../lib/market/repo-resolve.js';
3
+ import { type MarketListing } from '../../lib/net/market.js';
4
+ export interface Skipped {
5
+ id: string;
6
+ reason: string;
7
+ }
8
+ export interface AvailableRow {
9
+ id: string;
10
+ version: string | null;
11
+ market: string;
12
+ commit?: string;
13
+ description?: string;
14
+ invalid?: string;
15
+ }
16
+ export declare const repoRows: (deps: RepoDeps) => Promise<{
17
+ rows: AvailableRow[];
18
+ skipped: Skipped[];
19
+ }>;
20
+ export declare const printAll: (site: MarketListing, repo: Awaited<ReturnType<typeof repoRows>>) => WithMeta;
@@ -0,0 +1,44 @@
1
+ import { messageOf, WithMeta } from '../../lib/json-output.js';
2
+ import {} from '../../lib/market/repo-entries.js';
3
+ import { SITE_MARKET } from '../../lib/market/repo-manifest.js';
4
+ import { marketOffers } from '../../lib/market/repo-resolve.js';
5
+ import { parseRepoVersion } from '../../lib/market/repo-version.js';
6
+ import {} from '../../lib/net/market.js';
7
+ import { table } from './table.js';
8
+ const rowOf = (offer) => {
9
+ const parsed = offer.version ? parseRepoVersion(offer.version) : null;
10
+ return {
11
+ id: offer.id,
12
+ version: parsed?.version ?? null,
13
+ market: offer.market,
14
+ ...(parsed && { commit: parsed.commit }),
15
+ ...(offer.description !== undefined && { description: offer.description }),
16
+ ...(offer.problem !== undefined && { invalid: offer.problem }),
17
+ };
18
+ };
19
+ // Every repo market at its newest ref; one that fails is skipped, named by the market.
20
+ export const repoRows = async (deps) => {
21
+ const rows = [];
22
+ const skipped = [];
23
+ for (const market of deps.markets())
24
+ await marketOffers(deps, market).then((offers) => rows.push(...offers.map(rowOf)), (err) => skipped.push({ id: `market ${market.name}`, reason: messageOf(err) }));
25
+ return { rows, skipped };
26
+ };
27
+ const noteOf = (row) => row.invalid ? `invalid: ${row.invalid}` : (row.description ?? '');
28
+ // The site market's rows and the repo markets' rows in one table (exit 0 when some are silent).
29
+ export const printAll = (site, repo) => {
30
+ const down = site.down;
31
+ if (down)
32
+ console.error(down.message);
33
+ const offered = down ? [] : site.offered.map((o) => ({ ...o, market: SITE_MARKET }));
34
+ const rows = [...offered, ...repo.rows];
35
+ const cells = rows.map((r) => [r.id, r.version ?? '-', r.market, noteOf(r)]);
36
+ const lines = table([['ID', 'VERSION', 'MARKET', ''], ...cells]);
37
+ for (const line of rows.length > 0 ? lines : ['the markets list nothing'])
38
+ console.log(line);
39
+ const skipped = [...site.skipped, ...repo.skipped];
40
+ if (skipped.length > 0)
41
+ console.error(`skipped ${skipped.map((s) => `${s.id} (${s.reason})`).join(', ')}`);
42
+ const meta = { skipped, ...(down && { down: { code: down.code, message: down.message } }) };
43
+ return new WithMeta(rows, meta);
44
+ };
@@ -2,6 +2,7 @@ import { realpathSync } from 'node:fs';
2
2
  import { existingOf } from '../../lib/daemon/daemon-answer.js';
3
3
  import {} from '../../lib/daemon/host-client.js';
4
4
  import { CliError, DaemonError } from '../../lib/json-output.js';
5
+ import { installVersion } from '../../core/loader/installed.js';
5
6
  // The add is the check: of two cli runs, the second gets CONFLICT naming the installed entry.
6
7
  const added = (host, body) => host.add(body).catch((err) => {
7
8
  const existing = err instanceof DaemonError ? existingOf(err) : null;
@@ -27,7 +28,7 @@ export const installId = async (host, spec) => {
27
28
  const { id, source } = got.row;
28
29
  if (source !== 'market')
29
30
  throw elsewhere(`${id} runs from the folder ${source}`, id);
30
- if (spec.version && spec.version !== got.row.version)
31
+ if (spec.version && spec.version !== (installVersion(id) ?? got.row.version))
31
32
  return { row: await host.change(id, { version: spec.version }), already: false };
32
33
  return got;
33
34
  };
@@ -0,0 +1,7 @@
1
+ import { type RepoDeps } from '../../lib/market/repo-resolve.js';
2
+ import { type IdBody } from './install-once.js';
3
+ export interface RepoInstallDeps {
4
+ repo: RepoDeps;
5
+ siteKnows: (id: string) => Promise<boolean>;
6
+ }
7
+ export declare const installSpec: (name: string, deps?: RepoInstallDeps) => Promise<IdBody>;
@@ -0,0 +1,30 @@
1
+ import { installVersion } from '../../core/loader/installed.js';
2
+ import { CliError } from '../../lib/json-output.js';
3
+ import { SITE_MARKET } from '../../lib/market/repo-manifest.js';
4
+ import { qualifiedOf, resolveBare, resolveQualified, } from '../../lib/market/repo-resolve.js';
5
+ import { specOf } from './install-spec.js';
6
+ import {} from './install-once.js';
7
+ // No repo market configured: a qualified name finds none.
8
+ const none = {
9
+ git: async () => ({ code: 1, stdout: '', stderr: '' }),
10
+ markets: () => [],
11
+ };
12
+ const pinned = (name, version) => new CliError('invalid_argument', `${name}: a repo market installs its newest ref, so drop @${version}`);
13
+ // `acme/hello` is that market's; a bare id the site market lacks may come from one repo market.
14
+ export const installSpec = async (name, deps) => {
15
+ const qualified = qualifiedOf(name);
16
+ const spec = specOf(qualified?.name ?? name);
17
+ if (qualified && qualified.market !== SITE_MARKET) {
18
+ if (spec.version)
19
+ throw pinned(name, spec.version);
20
+ const version = await resolveQualified(deps?.repo ?? none, qualified.market, spec.id);
21
+ return version ? { id: spec.id, version } : spec;
22
+ }
23
+ if (qualified || spec.version || !deps)
24
+ return spec;
25
+ // An installed id answers from installation.json: no network, as with no repo market.
26
+ if (installVersion(spec.id) !== null)
27
+ return spec;
28
+ const version = await resolveBare(deps.repo, spec.id, () => deps.siteKnows(spec.id));
29
+ return version ? { id: spec.id, version } : spec;
30
+ };
@@ -1,5 +1,6 @@
1
1
  import { isVersionSpec } from '../../core/installer/version-spec.js';
2
2
  import { kitIdProblem } from '../../core/kit-id.js';
3
+ import { parseRepoVersion } from '../../lib/market/repo-version.js';
3
4
  import { CliError } from '../../lib/json-output.js';
4
5
  const ID = /^[a-z][a-z0-9-]*$/;
5
6
  const PACKAGE_PREFIX = /^(@agentage\/extension-|agentage-extension-)/;
@@ -14,6 +15,16 @@ const refusedName = (name) => {
14
15
  return `${name}: only @agentage/extension-<id> packages install, by id; for another package, install its folder (./path)`;
15
16
  return null;
16
17
  };
18
+ const checked = (name, id, version) => {
19
+ if (!ID.test(id) || version === '')
20
+ throw new CliError('invalid_argument', `${name}: not an id, id@version or a folder (./path)`);
21
+ if (version !== undefined && !isVersionSpec(version))
22
+ throw new CliError('invalid_argument', `${name}: after @ only a version, a range or a dist-tag`);
23
+ // The cli builds `<version>+<market>.<commit>` from the clone; a typed one could name any version.
24
+ const typed = version === undefined ? null : parseRepoVersion(version);
25
+ if (typed)
26
+ throw new CliError('invalid_argument', `${name}: install ${typed.market}/${id} instead`);
27
+ };
17
28
  // `probe`, `probe@0.6.2`, or a package name of the id.
18
29
  export const specOf = (name) => {
19
30
  const refused = refusedName(name);
@@ -22,10 +33,7 @@ export const specOf = (name) => {
22
33
  const at = name.indexOf('@', 1);
23
34
  const id = (at < 0 ? name : name.slice(0, at)).replace(PACKAGE_PREFIX, '');
24
35
  const version = at < 0 ? undefined : name.slice(at + 1);
25
- if (!ID.test(id) || version === '')
26
- throw new CliError('invalid_argument', `${name}: not an id, id@version or a folder (./path)`);
27
- if (version !== undefined && !isVersionSpec(version))
28
- throw new CliError('invalid_argument', `${name}: after @ only a version, a range or a dist-tag`);
36
+ checked(name, id, version);
29
37
  const owned = kitIdProblem(id);
30
38
  if (owned)
31
39
  throw new CliError('invalid_argument', `${name}: ${owned}`);
@@ -1,7 +1,9 @@
1
1
  import { type HostRow } from '../../lib/daemon/host-client.js';
2
2
  import { WithMeta } from '../../lib/json-output.js';
3
3
  import { type Market, type MarketListing } from '../../lib/net/market.js';
4
+ import { type RepoDeps } from '../../lib/market/repo-resolve.js';
4
5
  import { type LifecycleDeps } from './lifecycle.js';
6
+ export { table } from './table.js';
5
7
  export interface ListOptions {
6
8
  available?: boolean;
7
9
  json?: boolean;
@@ -15,8 +17,8 @@ export interface ListDeps extends LifecycleDeps {
15
17
  available: () => Promise<MarketListing>;
16
18
  market: () => Market;
17
19
  npmNewest: (id: string) => Promise<string>;
20
+ repo?: RepoDeps;
18
21
  }
19
- export declare const table: (rows: string[][]) => string[];
20
22
  export declare const runList: (opts: ListOptions, deps: ListDeps) => Promise<WithMeta | HostRow[]>;
21
23
  export declare const runUpdate: (opts: ExtensionUpdateOptions, deps: ListDeps) => Promise<{
22
24
  updated: boolean;
@@ -4,24 +4,30 @@ import { CliError, messageOf, WithMeta } from '../../lib/json-output.js';
4
4
  import {} from '../../lib/net/market.js';
5
5
  import { fromNpmLine, noSiteMarket } from '../../lib/net/market-error.js';
6
6
  import { siteFqdn } from '../../lib/net/origins.js';
7
+ import {} from '../../lib/market/repo-resolve.js';
8
+ import { printAll, repoRows } from './available.js';
7
9
  import { pruneUnused } from './lifecycle.js';
10
+ import { repoMoves, repoOf, shortVersion, withMarket } from './lifecycle-repo.js';
11
+ import { table } from './table.js';
12
+ export { table } from './table.js';
8
13
  // The daemon's rows as they are; a folder entry says `folder` and keeps its path.
9
14
  const shown = (row) => row.source === 'market' ? row : { ...row, source: 'folder', path: row.source };
10
- export const table = (rows) => {
11
- const widths = rows[0]?.map((_, i) => Math.max(...rows.map((r) => (r[i] ?? '').length))) ?? [];
12
- return rows.map((r) => r
13
- .map((cell, i) => cell.padEnd(widths[i] ?? 0))
14
- .join(' ')
15
- .trimEnd());
16
- };
17
15
  const printRows = (rows) => {
18
- const cells = rows.map((r) => [r.id, r.version, r.source, r.state, r.error ?? '']);
16
+ const cells = rows.map((r) => [
17
+ r.id,
18
+ r.version,
19
+ String(r['market'] ?? r.source),
20
+ r.state,
21
+ r.error ?? '',
22
+ ]);
19
23
  const lines = table([['ID', 'VERSION', 'SOURCE', 'STATE', ''], ...cells]);
20
24
  for (const line of rows.length > 0 ? lines : ['no extensions'])
21
25
  console.log(line);
22
26
  };
23
27
  // A silent market is one stderr line and an empty list (exit 0), never a failed command.
24
28
  const available = async (deps) => {
29
+ if (deps.repo && deps.repo.markets().length > 0)
30
+ return printAll(await deps.available(), await repoRows(deps.repo));
25
31
  const { offered, skipped, down } = await deps.available();
26
32
  if (down) {
27
33
  console.error(down.message);
@@ -43,7 +49,7 @@ const goneHints = (rows) => {
43
49
  export const runList = async (opts, deps) => {
44
50
  if (opts.available)
45
51
  return available(deps);
46
- const rows = (await (await deps.host()).list()).map(shown);
52
+ const rows = (await (await deps.host()).list()).map(shown).map(withMarket);
47
53
  printRows(rows);
48
54
  goneHints(rows);
49
55
  return rows;
@@ -80,6 +86,23 @@ const movesOf = async (rows, deps) => {
80
86
  console.error(fromNpmLine(siteFqdn(), 'newest versions'));
81
87
  return { moves, failed };
82
88
  };
89
+ const siteMoves = (rows, deps) => movesOf(rows, deps).catch((err) => ({
90
+ moves: [],
91
+ failed: [messageOf(err)],
92
+ ...(err instanceof CliError && { outage: err }),
93
+ }));
94
+ // With repo rows, a site outage is one failed line at the end and the repo markets still update.
95
+ const allMoves = async (rows, deps) => {
96
+ const repo = deps.repo ? rows.filter((r) => repoOf(r) !== null) : [];
97
+ const siteRows = rows.filter((r) => !repo.includes(r));
98
+ if (repo.length === 0 || !deps.repo)
99
+ return movesOf(siteRows, deps);
100
+ const site = await siteMoves(siteRows, deps);
101
+ const more = await repoMoves(repo, deps.repo);
102
+ const failed = [...site.failed, ...more.failed];
103
+ const outage = more.failed.length === 0 ? site.outage : undefined;
104
+ return { moves: [...site.moves, ...more.moves], failed, outage };
105
+ };
83
106
  const marketRows = async (opts, rows) => {
84
107
  const chosen = rows.filter((r) => r.source === 'market' && (!opts.id || r.id === opts.id));
85
108
  if (opts.id && chosen.length === 0)
@@ -90,14 +113,17 @@ const marketRows = async (opts, rows) => {
90
113
  // On a site with no market, an id npm cannot resolve fails the run at the end (exit 1).
91
114
  export const runUpdate = async (opts, deps) => {
92
115
  const host = await deps.host();
93
- const { moves, failed } = await movesOf(await marketRows(opts, await host.list()), deps);
116
+ const { moves, failed, outage } = await allMoves(await marketRows(opts, await host.list()), deps);
94
117
  for (const { id, from, to } of moves) {
95
118
  if (!opts.check)
96
119
  await host.change(id, { version: to }).then(() => pruneUnused(id));
97
- console.log(`${id} ${from} to ${to}${opts.check ? ' (available)' : ''}`);
120
+ const line = `${id} ${shortVersion(from)} to ${shortVersion(to)}`;
121
+ console.log(`${line}${opts.check ? ' (available)' : ''}`);
98
122
  }
99
123
  if (moves.length === 0 && failed.length === 0)
100
124
  console.log('every extension is at its newest version');
125
+ if (outage)
126
+ throw outage;
101
127
  if (failed.length > 0)
102
128
  throw new CliError('extension_failed', `no newest version found:\n ${failed.join('\n ')}`);
103
129
  return moves.map((move) => ({ ...move, updated: !opts.check }));
@@ -0,0 +1,16 @@
1
+ import { type HostRow } from '../../lib/daemon/host-client.js';
2
+ import { type RepoDeps } from '../../lib/market/repo-resolve.js';
3
+ import { type RepoVersion } from '../../lib/market/repo-version.js';
4
+ export interface Move {
5
+ id: string;
6
+ from: string;
7
+ to: string;
8
+ }
9
+ export interface Moves {
10
+ moves: Move[];
11
+ failed: string[];
12
+ }
13
+ export declare const repoOf: (row: HostRow) => RepoVersion | null;
14
+ export declare const withMarket: (row: HostRow) => HostRow;
15
+ export declare const shortVersion: (version: string) => string;
16
+ export declare const repoMoves: (rows: HostRow[], deps: RepoDeps) => Promise<Moves>;
@@ -0,0 +1,66 @@
1
+ import { installVersion } from '../../core/loader/installed.js';
2
+ import { newestFirst } from '../../core/semver-range.js';
3
+ import {} from '../../lib/daemon/host-client.js';
4
+ import { messageOf } from '../../lib/json-output.js';
5
+ import {} from '../../lib/market/repo-entries.js';
6
+ import { marketOffers } from '../../lib/market/repo-resolve.js';
7
+ import { parseRepoVersion } from '../../lib/market/repo-version.js';
8
+ // A repo-market row: its installation.json version names the market and the commit.
9
+ export const repoOf = (row) => row.source === 'market' ? parseRepoVersion(installVersion(row.id) ?? '') : null;
10
+ // The row with its market and commit; the daemon's own fields stay as they are.
11
+ export const withMarket = (row) => {
12
+ const repo = repoOf(row);
13
+ return repo ? { ...row, market: repo.market, commit: repo.commit } : row;
14
+ };
15
+ // `1.0.0+acme.<commit>` as a person reads it.
16
+ export const shortVersion = (version) => {
17
+ const repo = parseRepoVersion(version);
18
+ return repo ? `${repo.version} (${repo.market} ${repo.commit.slice(0, 7)})` : version;
19
+ };
20
+ // Each market fetched once per run; null for a market no longer on this machine.
21
+ const offersOnce = (deps) => {
22
+ const seen = new Map();
23
+ return (name) => {
24
+ const market = deps.markets().find((m) => m.name === name);
25
+ if (!market)
26
+ return null;
27
+ const got = seen.get(name) ?? marketOffers(deps, market);
28
+ seen.set(name, got);
29
+ return got;
30
+ };
31
+ };
32
+ const plain = (version) => parseRepoVersion(version)?.version ?? version;
33
+ // Forward only: a newer version, or the same version at another commit; never back to an older one.
34
+ const choose = (offer, from, into) => {
35
+ const to = offer.version;
36
+ if (offer.problem || !to)
37
+ into.failed.push(`${offer.id}: ${offer.problem}`);
38
+ else if (newestFirst(plain(from), plain(to)) < 0)
39
+ console.error(`${offer.id}: the newest ref of ${offer.market} is ${plain(to)}, older than the installed ${plain(from)}; it stays`);
40
+ else if (to !== from)
41
+ into.moves.push({ id: offer.id, from, to });
42
+ };
43
+ const moveOf = async (row, offers, into) => {
44
+ const repo = repoOf(row);
45
+ const from = installVersion(row.id) ?? row.version;
46
+ const listed = repo && offers(repo.market);
47
+ if (!repo || !listed) {
48
+ console.error(`${row.id}: the market ${repo?.market} is gone; ${shortVersion(from)} stays`);
49
+ return;
50
+ }
51
+ const offer = (await listed).find((o) => o.id === row.id);
52
+ if (!offer)
53
+ console.error(`${row.id}: not in the market ${repo.market} any more; it stays`);
54
+ else
55
+ choose(offer, from, into);
56
+ };
57
+ // The newest ref of each row's market; a market that fails names the row, the others go on.
58
+ export const repoMoves = async (rows, deps) => {
59
+ const into = { moves: [], failed: [] };
60
+ const offers = offersOnce(deps);
61
+ for (const row of rows)
62
+ await moveOf(row, offers, into).catch((err) => {
63
+ into.failed.push(`${row.id}: ${messageOf(err)}`);
64
+ });
65
+ return into;
66
+ };
@@ -1,4 +1,5 @@
1
1
  import { type HostClient, type HostSummary } from '../../lib/daemon/host-client.js';
2
+ import { type RepoInstallDeps } from './install-repo.js';
2
3
  export interface LifecycleDeps {
3
4
  host: () => Promise<HostClient>;
4
5
  }
@@ -24,6 +25,7 @@ export interface InstallDeps extends LifecycleDeps {
24
25
  fromNpm?: (id: string, what: string) => Promise<string | null>;
25
26
  hasMarket?: (id: string) => Promise<boolean>;
26
27
  sealOff?: () => Promise<string | null>;
28
+ repo?: RepoInstallDeps;
27
29
  }
28
30
  export declare const runInstall: (opts: InstallOptions, deps: InstallDeps) => Promise<HostSummary>;
29
31
  export declare const runEnabled: (id: string, enabled: boolean, deps: LifecycleDeps) => Promise<HostSummary>;
@@ -10,6 +10,7 @@ import {} from '../../lib/daemon/host-client.js';
10
10
  import { installFolder, installId } from './install-once.js';
11
11
  import { CliError, isJsonRun } from '../../lib/json-output.js';
12
12
  import { specOf, unscoped, UNSCOPED } from './install-spec.js';
13
+ import { installSpec } from './install-repo.js';
13
14
  const LOCAL = /^\.{1,2}(?:[/\\]|$)/;
14
15
  // The real path; a folder another user can write runs code as you, so it is refused.
15
16
  const folderOf = (input) => {
@@ -41,7 +42,7 @@ export const report = (done, note = '') => {
41
42
  // The install string is checked before the daemon starts: a bad one starts nothing.
42
43
  export const runInstall = async (opts, deps) => {
43
44
  const folder = LOCAL.test(opts.name) || isAbsolute(opts.name) ? folderOf(opts.name) : null;
44
- const spec = folder ? null : specOf(opts.name);
45
+ const spec = folder ? null : await installSpec(opts.name, deps.repo);
45
46
  if (spec && opts.name.startsWith(UNSCOPED) && (await deps.hasMarket?.(spec.id)))
46
47
  throw unscoped(opts.name);
47
48
  const host = await deps.host();
@@ -4,6 +4,7 @@ import { type ExtensionReleaseOptions } from './release.js';
4
4
  import { type ExtensionCreateOptions } from './create.js';
5
5
  import { type ExtensionDoctorOptions } from './doctor.js';
6
6
  import { type LifecycleActions } from './lifecycle-commands.js';
7
+ import { type MarketActions } from './market-commands.js';
7
8
  import { type ExtensionsGather } from './rows.js';
8
9
  import { type ExtensionTypesOptions } from './types.js';
9
10
  import { type KitActions } from './up-commands.js';
@@ -14,5 +15,6 @@ export interface ExtensionActions extends KitActions, BuildActions, LifecycleAct
14
15
  create: (opts: ExtensionCreateOptions) => Promise<void>;
15
16
  release: (opts: ExtensionReleaseOptions) => Promise<void>;
16
17
  types: (opts: ExtensionTypesOptions) => Promise<void>;
18
+ market: MarketActions;
17
19
  }
18
20
  export declare const registerExtension: (program: Command, actions: ExtensionActions) => void;
@@ -4,6 +4,7 @@ import { addRelease } from './release.js';
4
4
  import { addCreate } from './create.js';
5
5
  import {} from './doctor.js';
6
6
  import { addLifecycle } from './lifecycle-commands.js';
7
+ import { addMarket } from './market-commands.js';
7
8
  import { printWarnings, rowLines } from './row-lines.js';
8
9
  import {} from './rows.js';
9
10
  import { addTypes } from './types.js';
@@ -40,7 +41,7 @@ const addDoctor = (group, doctor) => {
40
41
  .addHelpText('after', DOCTOR_HELP)
41
42
  .action(doctor);
42
43
  };
43
- // Help order: C1 (install to remove), then doctor, create, check, build, release, up, down, token.
44
+ // Help order: C1 (install to remove), market, then doctor, create, check, build, release, up, down, token.
44
45
  export const registerExtension = (program, actions) => {
45
46
  const group = program
46
47
  .command('extension')
@@ -48,6 +49,7 @@ export const registerExtension = (program, actions) => {
48
49
  .allowExcessArguments()
49
50
  .action(helpOrUnknown);
50
51
  addLifecycle(group, actions);
52
+ addMarket(group, actions.market);
51
53
  addDoctor(group, actions.doctor);
52
54
  addCreate(group, actions.create);
53
55
  addBuild(group, actions);
@@ -0,0 +1,10 @@
1
+ import { type Command } from 'commander';
2
+ import { type MarketAddOptions, type MarketListOptions, type MarketRemoveOptions } from './market.js';
3
+ type Action<T> = (opts: T) => Promise<void>;
4
+ export interface MarketActions {
5
+ add: Action<MarketAddOptions>;
6
+ list: Action<MarketListOptions>;
7
+ remove: Action<MarketRemoveOptions>;
8
+ }
9
+ export declare const addMarket: (group: Command, actions: MarketActions) => void;
10
+ export {};