@agentage/cli 0.80.1 → 0.81.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 +1 -1
- package/dist/commands/auth/setup.js +9 -13
- package/dist/commands/extension/create-markers.d.ts +2 -0
- package/dist/commands/extension/create-markers.js +56 -0
- package/dist/commands/extension/create-prune.d.ts +7 -0
- package/dist/commands/extension/create-prune.js +30 -0
- package/dist/commands/extension/create.d.ts +1 -0
- package/dist/commands/extension/create.js +19 -13
- package/dist/commands/extension/plan.js +2 -2
- package/dist/commands/extension/profile-plan.js +2 -2
- package/dist/commands/extension/setup-cloud.js +15 -53
- package/dist/commands/extension/setup-install.js +1 -1
- package/dist/commands/extension/sync.d.ts +0 -4
- package/dist/commands/extension/sync.js +0 -26
- package/dist/commands/extension/up-commands.js +1 -2
- package/dist/commands/extension/up.d.ts +1 -3
- package/dist/commands/extension/up.js +3 -10
- package/dist/core/installer/npm.d.ts +0 -1
- package/dist/core/installer/npm.js +0 -2
- package/dist/core/profile/cloud.d.ts +0 -17
- package/dist/core/profile/cloud.js +0 -40
- package/dist/core/profile/machine.d.ts +0 -1
- package/dist/core/profile/machine.js +0 -2
- package/dist/lib/json-codes.d.ts +1 -1
- package/dist/lib/json-codes.js +0 -1
- package/dist/lib/json-output.js +0 -2
- package/dist/program-deps.js +3 -3
- package/package.json +1 -1
- package/dist/core/profile/push.d.ts +0 -16
- package/dist/core/profile/push.js +0 -49
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ personal access token: `AGENTAGE_TOKEN=aga_... agentage status`.
|
|
|
52
52
|
You need git and network. Run these in an empty folder:
|
|
53
53
|
|
|
54
54
|
```bash
|
|
55
|
-
agentage extension create my-ext
|
|
55
|
+
agentage extension create my-ext app api # parts to keep; none = all eight
|
|
56
56
|
cd extension-my-ext
|
|
57
57
|
npm ci
|
|
58
58
|
npm run verify
|
|
@@ -6,17 +6,14 @@ installs its extensions with npm, without signing in. --dry-run prints the plan,
|
|
|
6
6
|
would change (+ add, ~ update, - remove, o off, then one summary line), and writes nothing;
|
|
7
7
|
--check prints the plan and exits 1 when anything would change, the lock in the extensions
|
|
8
8
|
directory does not match the installed tree, or machine.json is missing; it writes nothing.
|
|
9
|
-
Neither signs in. Plain setup signs in,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
https://registry.npmjs.org) into AGENTAGE_EXTENSIONS_DIR (default ~/.agentage/extensions). After
|
|
18
|
-
installing, setup lists the secrets extensions declare and this machine has not set (asking for
|
|
19
|
-
them on a terminal), then loads every extension once and prints ok or failed for each; it exits
|
|
9
|
+
Neither signs in. Plain setup signs in, registers this machine in the account and installs
|
|
10
|
+
this machine's profile.json; with none it installs nothing and says so on one line. It never
|
|
11
|
+
reads or writes a profile in the account. With AGENTAGE_TOKEN set, setup signs in with that
|
|
12
|
+
token and opens no browser. Then setup starts the daemon on 127.0.0.1:4243, which needs no
|
|
13
|
+
account (--no-daemon or AGENTAGE_NO_DAEMON=1: not started). Each package comes from the registry the profile's registries
|
|
14
|
+
map names for its scope (default https://registry.npmjs.org) into AGENTAGE_EXTENSIONS_DIR
|
|
15
|
+
(default ~/.agentage/extensions). After installing, setup lists the secrets extensions declare
|
|
16
|
+
and this machine has not set (asking for them on a terminal), then loads every extension once and prints ok or failed for each; it exits
|
|
20
17
|
1 when any failed. Signed in to another account than the one this machine is connected to, setup
|
|
21
18
|
exits 1; run setup --disconnect first, which also marks this machine disconnected in your
|
|
22
19
|
account.
|
|
@@ -24,8 +21,7 @@ account.
|
|
|
24
21
|
--latest runs npm update in a staged copy of the installed tree: each extension moves to the
|
|
25
22
|
newest version its range allows, unless that version needs another extension API (then it is
|
|
26
23
|
kept, saying so). It prints one line per changed extension and a summary, loads the new tree and
|
|
27
|
-
makes it current only when every extension loaded clean
|
|
28
|
-
lock to your account when the profile comes from it. With nothing newer it only prints
|
|
24
|
+
makes it current only when every extension loaded clean. With nothing newer it only prints
|
|
29
25
|
"latest: nothing to update". It never signs in.`;
|
|
30
26
|
const latestOption = () => new Option('--latest', 'update extensions to the newest versions their ranges allow').conflicts([
|
|
31
27
|
'profile',
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
const OPEN = /^\s*<!-- part:([a-z+]+) -->\s*$/;
|
|
4
|
+
const CLOSE = /^\s*<!-- \/part:([a-z+]+) -->\s*$/;
|
|
5
|
+
// Marker pairs take whole lines; `a+b` keeps its block only when all named parts are kept. No nesting.
|
|
6
|
+
export const markParts = (text, kept, all, file) => {
|
|
7
|
+
const out = [];
|
|
8
|
+
let open;
|
|
9
|
+
let marked = false;
|
|
10
|
+
const fail = (why) => {
|
|
11
|
+
throw new Error(`${file}: ${why}`);
|
|
12
|
+
};
|
|
13
|
+
for (const line of text.split('\n')) {
|
|
14
|
+
const start = OPEN.exec(line)?.[1];
|
|
15
|
+
const end = CLOSE.exec(line)?.[1];
|
|
16
|
+
if (start !== undefined) {
|
|
17
|
+
if (open)
|
|
18
|
+
fail(`part:${start} opens inside part:${open.name} (no nesting)`);
|
|
19
|
+
const names = start.split('+');
|
|
20
|
+
const unknown = names.find((name) => !all.includes(name));
|
|
21
|
+
if (unknown)
|
|
22
|
+
fail(`part:${start} names unknown part ${unknown}`);
|
|
23
|
+
open = { name: start, drop: names.some((name) => !kept.includes(name)) };
|
|
24
|
+
marked = true;
|
|
25
|
+
}
|
|
26
|
+
else if (end !== undefined) {
|
|
27
|
+
if (open?.name !== end)
|
|
28
|
+
fail(`/part:${end} has no matching part:${end}`);
|
|
29
|
+
open = undefined;
|
|
30
|
+
}
|
|
31
|
+
else if (!open?.drop)
|
|
32
|
+
out.push(line);
|
|
33
|
+
}
|
|
34
|
+
if (open)
|
|
35
|
+
fail(`part:${open.name} is not closed`);
|
|
36
|
+
return marked
|
|
37
|
+
? out
|
|
38
|
+
.join('\n')
|
|
39
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
40
|
+
.replace(/\n\n$/, '\n')
|
|
41
|
+
: text;
|
|
42
|
+
};
|
|
43
|
+
// Every .md file of the new folder, so a marker error names the file.
|
|
44
|
+
export const markFolder = (dir, kept, all) => {
|
|
45
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
46
|
+
const path = join(dir, entry.name);
|
|
47
|
+
if (entry.isDirectory())
|
|
48
|
+
markFolder(path, kept, all);
|
|
49
|
+
if (!entry.isFile() || !entry.name.endsWith('.md'))
|
|
50
|
+
continue;
|
|
51
|
+
const text = readFileSync(path, 'utf8');
|
|
52
|
+
const next = markParts(text, kept, all, path);
|
|
53
|
+
if (next !== text)
|
|
54
|
+
writeFileSync(path, next);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const PRUNE: Record<string, {
|
|
2
|
+
keys?: string[][];
|
|
3
|
+
paths?: string[];
|
|
4
|
+
dev?: string[];
|
|
5
|
+
}>;
|
|
6
|
+
export declare const devDropped: (kept: string[]) => string[];
|
|
7
|
+
export declare const syncLockRoot: (root: Record<string, unknown>, dev: Record<string, unknown>) => void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// What else goes with a dropped part: manifest keys the kit refuses without it, the store's SQL, the contract file of api and mcp,
|
|
2
|
+
// and the dev dependencies no kept file imports or configures.
|
|
3
|
+
export const PRUNE = {
|
|
4
|
+
cli: { keys: [['agentage', 'commands']] },
|
|
5
|
+
mcp: { keys: [['agentage', 'settings', 'mcp.listing']], paths: ['template.mcpc.json'] },
|
|
6
|
+
api: { paths: ['openapi.json'] },
|
|
7
|
+
store: { paths: ['migrations', 'seeds'] },
|
|
8
|
+
app: {
|
|
9
|
+
keys: [['agentage', 'app']],
|
|
10
|
+
paths: ['src/app-part.test.tsx', 'src/app-lint.test.ts'],
|
|
11
|
+
dev: ['@agentage/components', 'jsdom'],
|
|
12
|
+
},
|
|
13
|
+
ui: { dev: ['@agentage/widget', '@axe-core/playwright'] },
|
|
14
|
+
};
|
|
15
|
+
// Used by app and by ui: dropped only when both are.
|
|
16
|
+
const REACT = ['react', 'react-dom', '@types/react', '@types/react-dom'];
|
|
17
|
+
// eslint, tsconfig and playwright config still name playwright, jsx-a11y and react-hooks: those stay.
|
|
18
|
+
export const devDropped = (kept) => [
|
|
19
|
+
...Object.entries(PRUNE).flatMap(([part, { dev = [] }]) => (kept.includes(part) ? [] : dev)),
|
|
20
|
+
...(kept.includes('app') || kept.includes('ui') ? [] : REACT),
|
|
21
|
+
];
|
|
22
|
+
// npm ci tolerates packages left in the lock, but its root entry should match package.json.
|
|
23
|
+
export const syncLockRoot = (root, dev) => {
|
|
24
|
+
const locked = root['devDependencies'];
|
|
25
|
+
if (!locked)
|
|
26
|
+
return;
|
|
27
|
+
for (const name of Object.keys(locked))
|
|
28
|
+
if (!(name in dev))
|
|
29
|
+
delete locked[name];
|
|
30
|
+
};
|
|
@@ -12,5 +12,6 @@ export interface ExtensionCreateOptions {
|
|
|
12
12
|
yes?: boolean;
|
|
13
13
|
json?: boolean;
|
|
14
14
|
}
|
|
15
|
+
export declare const splitParts: (parts: string[]) => string[];
|
|
15
16
|
export declare const runExtensionCreate: (opts: ExtensionCreateOptions, url?: (from: string) => string, after?: typeof installCreated, io?: ConfirmIo) => Promise<unknown>;
|
|
16
17
|
export declare const addCreate: (group: Command, create: (o: ExtensionCreateOptions) => Promise<void>) => Command;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { existsSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
-
import { join } from 'node:path';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
3
|
import {} from 'commander';
|
|
4
4
|
import { addJson, CliError } from '../../lib/json-output.js';
|
|
5
5
|
import { siteFqdn } from '../../lib/net/origins.js';
|
|
6
6
|
import { installCreated, trustFrom } from './create-install.js';
|
|
7
|
+
import { markFolder } from './create-markers.js';
|
|
8
|
+
import { devDropped, PRUNE, syncLockRoot } from './create-prune.js';
|
|
7
9
|
import { clone, fromRefusal, repoRef, TEMPLATE } from './create-source.js';
|
|
8
10
|
import { terminalIo } from './lifecycle.js';
|
|
9
11
|
export { TEMPLATE };
|
|
@@ -12,14 +14,8 @@ const ROUTED = ['pages', 'ui', 'api', 'mcp'];
|
|
|
12
14
|
// `_` is a separator here, so `template_echo` is the id too.
|
|
13
15
|
const WORD = /(?<![A-Za-z0-9])template(?![A-Za-z0-9])/g;
|
|
14
16
|
const LOCK = 'package-lock.json';
|
|
15
|
-
//
|
|
16
|
-
const
|
|
17
|
-
cli: { keys: [['agentage', 'commands']] },
|
|
18
|
-
mcp: { keys: [['agentage', 'settings', 'mcp.listing']], paths: ['template.mcpc.json'] },
|
|
19
|
-
api: { paths: ['openapi.json'] },
|
|
20
|
-
store: { paths: ['migrations', 'seeds'] },
|
|
21
|
-
app: { keys: [['agentage', 'app']], paths: ['src/app-part.test.tsx', 'src/app-lint.test.ts'] },
|
|
22
|
-
};
|
|
17
|
+
// Parts come as separate words, comma lists or both: `app,api`, `app api`, `app, api`.
|
|
18
|
+
export const splitParts = (parts) => parts.flatMap((part) => part.split(/[\s,]+/)).filter(Boolean);
|
|
23
19
|
const refusal = ({ id, parts, from = TEMPLATE }, kept) => {
|
|
24
20
|
const unknown = parts.find((part) => !PARTS.includes(part));
|
|
25
21
|
if (unknown)
|
|
@@ -51,6 +47,8 @@ const drop = (dir, kept) => {
|
|
|
51
47
|
delete parent[path.at(-1) ?? ''];
|
|
52
48
|
}
|
|
53
49
|
}
|
|
50
|
+
for (const name of devDropped(kept))
|
|
51
|
+
delete pkg['devDependencies']?.[name];
|
|
54
52
|
// The new repo is the folder itself: a monorepo path of the template would point into nothing.
|
|
55
53
|
delete pkg['repository']?.['directory'];
|
|
56
54
|
save(join(dir, 'package.json'), pkg);
|
|
@@ -79,6 +77,9 @@ const renameLock = (dir, id) => {
|
|
|
79
77
|
for (const at of [lock, lock['packages']?.['']])
|
|
80
78
|
if (at)
|
|
81
79
|
Object.assign(at, { name });
|
|
80
|
+
const root = lock['packages']?.[''];
|
|
81
|
+
if (root)
|
|
82
|
+
syncLockRoot(root, json(join(dir, 'package.json'))['devDependencies'] ?? {});
|
|
82
83
|
save(join(dir, LOCK), lock);
|
|
83
84
|
};
|
|
84
85
|
const CI = join('.github', 'workflows', 'ci.yml');
|
|
@@ -100,6 +101,7 @@ const lowerFloor = (dir, kept) => {
|
|
|
100
101
|
const shape = (dir, from, id, kept) => {
|
|
101
102
|
try {
|
|
102
103
|
drop(dir, kept);
|
|
104
|
+
markFolder(dir, kept, PARTS);
|
|
103
105
|
rewrite(dir, id);
|
|
104
106
|
renameLock(dir, id);
|
|
105
107
|
return lowerFloor(dir, kept);
|
|
@@ -119,15 +121,18 @@ const printNext = (dir, from, id, kept) => {
|
|
|
119
121
|
const branch = ref ? `branch ${ref}` : 'default branch';
|
|
120
122
|
console.log(` template ${repo}${folder ? `:${folder}` : ''} (${branch}, kit ${kit.replace(/^[\^~]/, '')})`);
|
|
121
123
|
console.log(` kept ${kept.join(', ')}`);
|
|
122
|
-
|
|
124
|
+
const contract = kept.includes('mcp') ? ` · contract ${id}.mcpc.json` : '';
|
|
125
|
+
console.log(` folder ${resolve(dir)}`);
|
|
126
|
+
console.log(` id ${id} · ${hostLine(id)}${contract}`);
|
|
123
127
|
console.log(` next cd ${dir} && agentage extension up`);
|
|
124
128
|
};
|
|
125
129
|
// Clones the template's default branch (or the #branch of --from; only its :folder) into ./extension-<id>, no history;
|
|
126
130
|
// then installs its dependencies, builds it and (unless --no-install) gives it to the daemon.
|
|
127
131
|
export const runExtensionCreate = async (opts, url = (from) => `https://github.com/${from}`, after = installCreated, io = terminalIo()) => {
|
|
128
132
|
const { id, from = TEMPLATE } = opts;
|
|
129
|
-
const
|
|
130
|
-
const
|
|
133
|
+
const parts = splitParts(opts.parts);
|
|
134
|
+
const kept = parts.length > 0 ? [...new Set(parts)] : PARTS;
|
|
135
|
+
const refused = refusal({ ...opts, parts }, kept);
|
|
131
136
|
if (refused)
|
|
132
137
|
throw new CliError('invalid_argument', refused);
|
|
133
138
|
await trustFrom(from, opts, opts.json ? { ...io, isTTY: false } : io);
|
|
@@ -146,4 +151,5 @@ export const addCreate = (group, create) => addJson(group
|
|
|
146
151
|
.argument('[parts...]', `parts to keep (default: all eight): ${PARTS.join(', ')}`)
|
|
147
152
|
.option('--from <owner/repo[#branch][:folder]>', `the template repo and folder (default: ${TEMPLATE})`)
|
|
148
153
|
.option('--no-install', 'build the folder, but do not give it to the daemon')
|
|
149
|
-
.option('--yes', 'run code from another --from repo or branch without asking')
|
|
154
|
+
.option('--yes', 'run code from another --from repo or branch without asking')
|
|
155
|
+
.addHelpText('after', '\nExample:\n agentage extension create my-ext app api (parts may also be app,api)')).action((id, parts, o) => create({ id, parts, ...o }));
|
|
@@ -9,7 +9,7 @@ import {} from '../../core/profile/schema.js';
|
|
|
9
9
|
import { line } from './line.js';
|
|
10
10
|
import { accountProfileInstalled, NOT_EXTENSION, profilePlan, } from './profile-plan.js';
|
|
11
11
|
// What setup would send to the daemon, with this machine's own entry from profile.json kept. A
|
|
12
|
-
// file (`--profile`) is never gated; the local profile is, once
|
|
12
|
+
// file (`--profile`) is never gated; the local profile is, once it was installed.
|
|
13
13
|
const planFor = (profile, file) => {
|
|
14
14
|
const local = readProfile();
|
|
15
15
|
const desired = keepMachine(profile, local?.ok ? local.profile : null, readMachine()?.id ?? '');
|
|
@@ -26,7 +26,7 @@ export const planLines = ({ extensions, summary: s, skipped }) => {
|
|
|
26
26
|
return [...shown.map(entryLine), ...skips, `plan: ${total}`];
|
|
27
27
|
};
|
|
28
28
|
// --check and --dry-run read no account: the plan says which profile it is of.
|
|
29
|
-
const sourceLine = ({ file, from }) => line('from', file ? from : `${from} on this machine
|
|
29
|
+
const sourceLine = ({ file, from }) => line('from', file ? from : `${from} on this machine`);
|
|
30
30
|
// One line per scope the profile routes, then the registry for everything else.
|
|
31
31
|
const registryLines = (registries) => {
|
|
32
32
|
const all = [...Object.entries(registries), ['default', DEFAULT_REGISTRY]];
|
|
@@ -6,7 +6,7 @@ import { readMachine } from '../../core/profile/machine.js';
|
|
|
6
6
|
import {} from '../../core/profile/schema.js';
|
|
7
7
|
import { getConfigDir } from '../../lib/fs/config.js';
|
|
8
8
|
const PACKAGE_ID = /^(?:@agentage\/extension-|agentage-extension-)([a-z][a-z0-9-]*)$/;
|
|
9
|
-
// Written once plain setup tried every entry of the
|
|
9
|
+
// Written once plain setup tried every entry of the profile: once per machine.
|
|
10
10
|
export const profileMarkerPath = () => join(getConfigDir(), 'setup.profile-installed');
|
|
11
11
|
export const accountProfileInstalled = () => existsSync(profileMarkerPath());
|
|
12
12
|
// A profile entry the daemon can never install: said once, never a change to make.
|
|
@@ -26,7 +26,7 @@ const verbOf = (entry, have, done) => {
|
|
|
26
26
|
return entry.off ? 'off' : 'add';
|
|
27
27
|
};
|
|
28
28
|
// What setup would send to the daemon: add, add disabled (off), or nothing (unchanged). Reads
|
|
29
|
-
// installation.json only; `done` is a plain setup that installed the
|
|
29
|
+
// installation.json only; `done` is a plain setup that installed the profile already.
|
|
30
30
|
export const profilePlan = (profile, done) => {
|
|
31
31
|
const rows = new Map(installationRows().map((row) => [row.id, row]));
|
|
32
32
|
const entries = profileEntries(profile);
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
|
-
import {
|
|
3
|
-
import { forgetAccount, loadMachine, readMachine, setAccount,
|
|
4
|
-
import { defaultProfile,
|
|
5
|
-
import { pushWithReplay } from '../../core/profile/push.js';
|
|
2
|
+
import {} from '../../core/profile/cloud.js';
|
|
3
|
+
import { forgetAccount, loadMachine, readMachine, setAccount, } from '../../core/profile/machine.js';
|
|
4
|
+
import { defaultProfile, readProfile } from '../../core/profile/profile.js';
|
|
6
5
|
import { disconnectMachine, registerMachine } from '../../core/profile/register.js';
|
|
7
6
|
import {} from '../../core/profile/schema.js';
|
|
8
7
|
import { line } from './line.js';
|
|
9
|
-
import {
|
|
10
|
-
const FILE_NOTE = 'from a file (setup --profile)
|
|
8
|
+
import { checkAccount } from './sync.js';
|
|
9
|
+
const FILE_NOTE = 'from a file (setup --profile)';
|
|
11
10
|
// profile.json: null when absent; an invalid one stops setup naming each problem.
|
|
12
11
|
const localCopy = () => {
|
|
13
12
|
const parsed = readProfile();
|
|
@@ -17,55 +16,18 @@ const localCopy = () => {
|
|
|
17
16
|
return parsed.profile;
|
|
18
17
|
throw new Error(`profile.json is invalid:\n${parsed.errors.map((e) => ` ${e}`).join('\n')}`);
|
|
19
18
|
};
|
|
20
|
-
//
|
|
21
|
-
const
|
|
22
|
-
// Without the account: the local profile.json; with none, nothing to install, and setup goes on.
|
|
23
|
-
const fromLocalCopy = async (reason, deps) => {
|
|
19
|
+
// Plain setup installs this machine's profile.json; with none, nothing, and setup goes on.
|
|
20
|
+
const fromLocalCopy = async (deps) => {
|
|
24
21
|
const local = localCopy();
|
|
25
|
-
const unavailable = 'cloud profile unavailable';
|
|
26
22
|
if (local === null)
|
|
27
|
-
return console.error(chalk.dim(
|
|
28
|
-
console.error(chalk.dim(`${unavailable}, using local profile.json (${reason})`));
|
|
23
|
+
return console.error(chalk.dim('no local profile.json, nothing installed'));
|
|
29
24
|
await deps.installProfile(local);
|
|
30
25
|
};
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
};
|
|
37
|
-
// A create the account did not take fails nothing: the next setup creates it.
|
|
38
|
-
const notCreated = (err) => {
|
|
39
|
-
if (!(err instanceof NotPushedError))
|
|
40
|
-
throw err;
|
|
41
|
-
console.error(chalk.dim(`${err.message}; the next agentage setup tries again`));
|
|
42
|
-
return null;
|
|
43
|
-
};
|
|
44
|
-
// No profile in the account yet: this machine's own, or the default, becomes it once it loaded.
|
|
45
|
-
const createIn = async (account, deps) => {
|
|
46
|
-
const base = localProfile(localCopy() ?? defaultProfile());
|
|
47
|
-
const apply = applying((profile) => profile, deps);
|
|
48
|
-
const done = await pushWithReplay(account.fetch, base, apply).catch(notCreated);
|
|
49
|
-
if (done === null)
|
|
50
|
-
return;
|
|
51
|
-
await setCloud(done.etag);
|
|
52
|
-
const where = done.pushed ? 'created in' : 'already in';
|
|
53
|
-
console.log(line('profile', `${where} your account (etag ${done.etag})`));
|
|
54
|
-
};
|
|
55
|
-
// Plain setup pulls and never pushes, except to create the account's first profile.
|
|
56
|
-
const profileFrom = async (account, machine, deps) => {
|
|
57
|
-
if (machine.profileSource === 'file') {
|
|
58
|
-
console.log(line('profile', FILE_NOTE));
|
|
59
|
-
return deps.installProfile(localCopy() ?? defaultProfile());
|
|
60
|
-
}
|
|
61
|
-
const pulled = await pullProfile(account.fetch, mirroredEtag(machine));
|
|
62
|
-
if (pulled.kind === 'found')
|
|
63
|
-
return installPulled(pulled.doc, deps);
|
|
64
|
-
if (pulled.kind === 'none')
|
|
65
|
-
return createIn(account, deps);
|
|
66
|
-
if (pulled.kind === 'current')
|
|
67
|
-
return deps.installProfile(localCopy() ?? defaultProfile());
|
|
68
|
-
return fromLocalCopy(pulled.reason, deps);
|
|
26
|
+
const profileFrom = async (machine, deps) => {
|
|
27
|
+
if (machine.profileSource !== 'file')
|
|
28
|
+
return fromLocalCopy(deps);
|
|
29
|
+
console.log(line('profile', FILE_NOTE));
|
|
30
|
+
return deps.installProfile(localCopy() ?? defaultProfile());
|
|
69
31
|
};
|
|
70
32
|
// The first signed-in setup records the account; another account is refused until --disconnect.
|
|
71
33
|
const connect = async (account) => {
|
|
@@ -77,12 +39,12 @@ const connect = async (account) => {
|
|
|
77
39
|
export const accountSetup = async (deps) => {
|
|
78
40
|
const account = await deps.account();
|
|
79
41
|
if (account === null)
|
|
80
|
-
return fromLocalCopy(
|
|
42
|
+
return fromLocalCopy(deps);
|
|
81
43
|
const platform = await account.platform();
|
|
82
44
|
const machine = await connect(account);
|
|
83
45
|
const registered = registerMachine(account.machines, machine);
|
|
84
46
|
try {
|
|
85
|
-
await profileFrom(
|
|
47
|
+
await profileFrom(machine, deps);
|
|
86
48
|
}
|
|
87
49
|
finally {
|
|
88
50
|
if (await registered)
|
|
@@ -6,7 +6,7 @@ import { addOnce } from './install-once.js';
|
|
|
6
6
|
import { NOT_EXTENSION, profileEntries } from './profile-plan.js';
|
|
7
7
|
import { readMarker, writeMarker } from './setup-marker.js';
|
|
8
8
|
const EXACT = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
|
9
|
-
const KEPT = '
|
|
9
|
+
const KEPT = 'the profile was installed here already; setup installs nothing (agentage extension install)';
|
|
10
10
|
// An exact version is sent as it is; a range installs the market's newest version.
|
|
11
11
|
const bodyOf = (id, range) => EXACT.test(range) ? { id, version: range } : { id };
|
|
12
12
|
// One entry through POST /host/extensions; an off entry is added disabled (it never starts).
|
|
@@ -1,17 +1,13 @@
|
|
|
1
1
|
import { type Npm } from '../../core/installer/npm.js';
|
|
2
2
|
import { type Account } from '../../core/profile/cloud.js';
|
|
3
3
|
import { type MachineFile } from '../../core/profile/machine.js';
|
|
4
|
-
import { type Apply } from '../../core/profile/push.js';
|
|
5
4
|
import { type Profile } from '../../core/profile/schema.js';
|
|
6
5
|
import { type AccountOf } from './account.js';
|
|
7
6
|
export interface EditDeps {
|
|
8
7
|
npm: () => Promise<Npm>;
|
|
9
|
-
npmMajor: () => Promise<number>;
|
|
10
8
|
account: AccountOf;
|
|
11
9
|
}
|
|
12
10
|
export interface ProfileDeps extends EditDeps {
|
|
13
11
|
installProfile: (profile: Profile) => Promise<void>;
|
|
14
12
|
}
|
|
15
|
-
export type Change = (profile: Profile) => Profile;
|
|
16
13
|
export declare const checkAccount: (account: Account, machine: MachineFile) => Promise<string | null>;
|
|
17
|
-
export declare const applying: (change: Change, deps: ProfileDeps, note?: string | null) => Apply;
|
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
import { join } from 'node:path';
|
|
2
|
-
import { parseLock } from '../../core/installer/compare-lock.js';
|
|
3
1
|
import {} from '../../core/installer/npm.js';
|
|
4
|
-
import { LOCK_FILE, readText } from '../../core/installer/tree.js';
|
|
5
|
-
import { extensionsDir } from '../../core/paths.js';
|
|
6
2
|
import {} from '../../core/profile/cloud.js';
|
|
7
3
|
import { accountMismatch } from '../../core/profile/machine.js';
|
|
8
|
-
import { mutateProfile } from '../../core/profile/profile.js';
|
|
9
|
-
import { pushBody } from '../../core/profile/push.js';
|
|
10
4
|
import {} from '../../core/profile/schema.js';
|
|
11
5
|
import {} from './account.js';
|
|
12
|
-
import { line } from './line.js';
|
|
13
|
-
const REPLAY = 'changed in your account meanwhile, replaying this change on it';
|
|
14
6
|
// The signed-in account's id, refused when this machine is connected to another account.
|
|
15
7
|
export const checkAccount = async (account, machine) => {
|
|
16
8
|
const userId = await account.id();
|
|
@@ -19,21 +11,3 @@ export const checkAccount = async (account, machine) => {
|
|
|
19
11
|
throw mismatch;
|
|
20
12
|
return userId;
|
|
21
13
|
};
|
|
22
|
-
// extensions/package-lock.json and the npm major that wrote it, pushed with the profile.
|
|
23
|
-
const localLock = async (deps) => {
|
|
24
|
-
const lock = parseLock(readText(join(extensionsDir(), LOCK_FILE)));
|
|
25
|
-
return { lock, npmMajor: lock === null ? null : await deps.npmMajor() };
|
|
26
|
-
};
|
|
27
|
-
// Nothing is pushed unless the install worked: installProfile throws otherwise.
|
|
28
|
-
export const applying = (change, deps, note = null) => {
|
|
29
|
-
let calls = 0;
|
|
30
|
-
return async (base) => {
|
|
31
|
-
const next = change(base.profile);
|
|
32
|
-
await mutateProfile(() => next);
|
|
33
|
-
const text = calls++ === 0 ? note : line('profile', REPLAY);
|
|
34
|
-
if (text !== null)
|
|
35
|
-
console.log(text);
|
|
36
|
-
await deps.installProfile(next);
|
|
37
|
-
return pushBody(next, base, await localLock(deps));
|
|
38
|
-
};
|
|
39
|
-
};
|
|
@@ -43,8 +43,7 @@ const addUp = (group, up) => {
|
|
|
43
43
|
.command('up')
|
|
44
44
|
.description('Run the working copy on the daemon and reload each change; --docker = on Docker')
|
|
45
45
|
.option(DIR, DIR_TEXT)
|
|
46
|
-
.option('--docker', 'the images, store, issuer and gateway on Docker')
|
|
47
|
-
.option('--local', 'deprecated: the old name of plain up (removed in the next minor)'))
|
|
46
|
+
.option('--docker', 'the images, store, issuer and gateway on Docker'))
|
|
48
47
|
.addHelpText('after', UP_HELP)
|
|
49
48
|
.action(up);
|
|
50
49
|
};
|
|
@@ -14,11 +14,9 @@ export interface ExtensionTokenOptions extends ExtensionUpOptions {
|
|
|
14
14
|
}
|
|
15
15
|
export interface ExtensionUpFlags extends ExtensionUpOptions {
|
|
16
16
|
docker?: boolean;
|
|
17
|
-
local?: boolean;
|
|
18
17
|
}
|
|
19
|
-
export declare const LOCAL_DEPRECATED = "--local is deprecated (removed in the next minor): extension up runs the working copy on the daemon";
|
|
20
18
|
type LocalRun = (dir: string | undefined, json: boolean) => Promise<unknown>;
|
|
21
|
-
export declare const runExtensionUp: ({ docker,
|
|
19
|
+
export declare const runExtensionUp: ({ docker, ...opts }?: ExtensionUpFlags, tool?: LoadKitTool, local?: LocalRun) => Promise<unknown>;
|
|
22
20
|
type Host = () => Promise<HostClient>;
|
|
23
21
|
export declare const runExtensionDown: (opts?: ExtensionDownOptions, tool?: LoadKitTool, host?: Host, stopMs?: number) => Promise<unknown>;
|
|
24
22
|
export declare const runExtensionToken: ({ dir, json, ...opts }: ExtensionTokenOptions, tool?: LoadKitTool, exec?: Exec) => Promise<unknown>;
|
|
@@ -2,7 +2,7 @@ import { existsSync } from 'node:fs';
|
|
|
2
2
|
import { join, resolve } from 'node:path';
|
|
3
3
|
import { ensureDaemon } from '../../lib/daemon/daemon-client.js';
|
|
4
4
|
import { hostClient } from '../../lib/daemon/host-client.js';
|
|
5
|
-
import {
|
|
5
|
+
import { omitJson } from '../../lib/json-output.js';
|
|
6
6
|
import { folderRow } from './install-once.js';
|
|
7
7
|
import { loadKitTool } from './kit-tool.js';
|
|
8
8
|
import { execIn } from './release-guard.js';
|
|
@@ -15,17 +15,10 @@ const printed = () => {
|
|
|
15
15
|
const lines = [];
|
|
16
16
|
return { lines, onLine: (line) => void (console.log(line), lines.push(line)) };
|
|
17
17
|
};
|
|
18
|
-
// S1.1.1 D10: an old flag is kept for one minor with a line that names the new form.
|
|
19
|
-
export const LOCAL_DEPRECATED = '--local is deprecated (removed in the next minor): extension up runs the working copy on the daemon';
|
|
20
18
|
// No Docker: the working copy on the daemon, watched (D12); in CI too. --docker: the kit's path.
|
|
21
|
-
export const runExtensionUp = async ({ docker,
|
|
22
|
-
if (docker
|
|
23
|
-
throw new CliError('invalid_argument', '--docker and --local: pick one');
|
|
24
|
-
if (!docker) {
|
|
25
|
-
if (asked)
|
|
26
|
-
console.error(LOCAL_DEPRECATED);
|
|
19
|
+
export const runExtensionUp = async ({ docker, ...opts } = {}, tool = loadKitTool, local = (dir, json) => runLocalUp(dir, undefined, json)) => {
|
|
20
|
+
if (!docker)
|
|
27
21
|
return local(opts.dir, opts.json === true);
|
|
28
|
-
}
|
|
29
22
|
const { up } = await tool();
|
|
30
23
|
const { lines, onLine } = printed();
|
|
31
24
|
await up({ ...omitJson(opts), onLine });
|
|
@@ -7,7 +7,6 @@ export type NpmManifest = (command: LookupCommand, spec: string, registries: Rec
|
|
|
7
7
|
export declare const configFlags: (env: NodeJS.ProcessEnv) => string[];
|
|
8
8
|
export declare const npmArgs: (command: NpmCommand, tree: string, env?: NodeJS.ProcessEnv, packages?: string[]) => string[];
|
|
9
9
|
export declare const manifestArgs: (command: LookupCommand, spec: string, registries: Record<string, string>, prefix: string, env?: NodeJS.ProcessEnv) => string[];
|
|
10
|
-
export declare const npmMajor: (env?: NodeJS.ProcessEnv) => Promise<number>;
|
|
11
10
|
export declare const prepareNpm: (env?: NodeJS.ProcessEnv) => Promise<Npm>;
|
|
12
11
|
export declare const prepareManifest: (env?: NodeJS.ProcessEnv) => Promise<NpmManifest>;
|
|
13
12
|
export declare const lastLines: (output: string, count?: number) => string;
|
|
@@ -57,8 +57,6 @@ const checkedNpm = async (env) => {
|
|
|
57
57
|
return { npmCli, version: await requireNpm10(run) };
|
|
58
58
|
};
|
|
59
59
|
const checkedNpmCli = async (env) => (await checkedNpm(env)).npmCli;
|
|
60
|
-
// The npm major that writes the lock, pushed next to it.
|
|
61
|
-
export const npmMajor = async (env = process.env) => Number.parseInt((await checkedNpm(env)).version, 10);
|
|
62
60
|
export const prepareNpm = async (env = process.env) => {
|
|
63
61
|
const npmCli = await checkedNpmCli(env);
|
|
64
62
|
return (command, tree, packages = []) => runNpm(npmArgs(command, tree, env, packages), {
|
|
@@ -14,12 +14,6 @@ export interface Account {
|
|
|
14
14
|
platform: () => Promise<string>;
|
|
15
15
|
id: () => Promise<string | null>;
|
|
16
16
|
}
|
|
17
|
-
export interface ProfileWrite {
|
|
18
|
-
version: number;
|
|
19
|
-
profile: JsonObject;
|
|
20
|
-
lock: JsonObject | null;
|
|
21
|
-
npmMajor: number | null;
|
|
22
|
-
}
|
|
23
17
|
export interface CloudProfile {
|
|
24
18
|
profile: Profile;
|
|
25
19
|
readOnly: boolean;
|
|
@@ -40,16 +34,5 @@ export type Pull = {
|
|
|
40
34
|
kind: 'unavailable';
|
|
41
35
|
reason: string;
|
|
42
36
|
};
|
|
43
|
-
export type Push = {
|
|
44
|
-
kind: 'stored';
|
|
45
|
-
etag: string;
|
|
46
|
-
} | {
|
|
47
|
-
kind: 'conflict';
|
|
48
|
-
current: CloudProfile | null;
|
|
49
|
-
};
|
|
50
|
-
export declare class NotPushedError extends Error {
|
|
51
|
-
}
|
|
52
|
-
export declare const localProfile: (profile: Profile) => CloudProfile;
|
|
53
37
|
export declare const errorCode: (res: Response) => Promise<string | null>;
|
|
54
38
|
export declare const pullProfile: (fetch: CloudFetch, etag: string | null, timeoutMs?: number) => Promise<Pull>;
|
|
55
|
-
export declare const pushProfile: (fetch: CloudFetch, body: ProfileWrite, etag: string | null) => Promise<Push>;
|
|
@@ -2,17 +2,6 @@ import { isRecord } from '../../lib/fs/json-file.js';
|
|
|
2
2
|
import { parseProfile } from './schema.js';
|
|
3
3
|
// One resource per account: the profile, npm's lock and the npm major that wrote it, one ETag.
|
|
4
4
|
export const PROFILE_PATH = '/v1/profile';
|
|
5
|
-
// The account answered a push with something other than a new ETag or a 412.
|
|
6
|
-
export class NotPushedError extends Error {
|
|
7
|
-
}
|
|
8
|
-
// A profile that is not in the account yet: pushing it creates it.
|
|
9
|
-
export const localProfile = (profile) => ({
|
|
10
|
-
profile,
|
|
11
|
-
readOnly: profile.version > 1,
|
|
12
|
-
lock: null,
|
|
13
|
-
npmMajor: null,
|
|
14
|
-
etag: null,
|
|
15
|
-
});
|
|
16
5
|
// What a request ended with: the Response, or why there was none.
|
|
17
6
|
const attempt = async (request) => {
|
|
18
7
|
try {
|
|
@@ -78,32 +67,3 @@ export const pullProfile = async (fetch, etag, timeoutMs) => {
|
|
|
78
67
|
const doc = served(await bodyOf(res), res.headers.get('etag'));
|
|
79
68
|
return typeof doc === 'string' ? unavailable(doc) : { kind: 'found', doc };
|
|
80
69
|
};
|
|
81
|
-
// A 412 carries the account's current resource and etag (both null when it holds none).
|
|
82
|
-
const conflictOf = (body) => {
|
|
83
|
-
const error = isRecord(body) && isRecord(body['error']) ? body['error'] : {};
|
|
84
|
-
const details = isRecord(error['details']) ? error['details'] : {};
|
|
85
|
-
if (details['current'] === null)
|
|
86
|
-
return { kind: 'conflict', current: null };
|
|
87
|
-
const current = resourceOf(details['current'], details['etag']);
|
|
88
|
-
if (typeof current === 'string')
|
|
89
|
-
throw new NotPushedError(`not pushed to your account (412: ${current})`);
|
|
90
|
-
return { kind: 'conflict', current };
|
|
91
|
-
};
|
|
92
|
-
const notPushed = (reason) => new NotPushedError(`not pushed to your account (${reason})`);
|
|
93
|
-
// PUT the whole resource: If-Match replaces the copy it was pulled at, no etag creates it.
|
|
94
|
-
export const pushProfile = async (fetch, body, etag) => {
|
|
95
|
-
const precondition = etag === null ? { 'if-none-match': '*' } : { 'if-match': etag };
|
|
96
|
-
const headers = { 'content-type': 'application/json', ...precondition };
|
|
97
|
-
const init = { method: 'PUT', headers, body: JSON.stringify(body) };
|
|
98
|
-
const res = await attempt(() => fetch(PROFILE_PATH, init));
|
|
99
|
-
if (typeof res === 'string')
|
|
100
|
-
throw notPushed(res);
|
|
101
|
-
if (res.status === 412)
|
|
102
|
-
return conflictOf(await bodyOf(res));
|
|
103
|
-
if (res.status !== 200 && res.status !== 201)
|
|
104
|
-
throw notPushed(await failureOf(res));
|
|
105
|
-
const stored = etagOf(await bodyOf(res), res.headers.get('etag'));
|
|
106
|
-
if (typeof stored !== 'string')
|
|
107
|
-
throw notPushed('unexpected answer');
|
|
108
|
-
return { kind: 'stored', etag: stored };
|
|
109
|
-
};
|
|
@@ -11,7 +11,6 @@ export declare const NO_MACHINE: MachineFile;
|
|
|
11
11
|
export declare const readMachine: () => MachineFile | null;
|
|
12
12
|
export declare const loadMachine: () => Promise<MachineFile>;
|
|
13
13
|
export declare const setProfileSource: (source: ProfileSource) => Promise<MachineFile>;
|
|
14
|
-
export declare const setCloud: (etag: string) => Promise<MachineFile>;
|
|
15
14
|
export declare const setAccount: (accountId: string) => Promise<MachineFile>;
|
|
16
15
|
export declare const forgetAccount: () => Promise<MachineFile>;
|
|
17
16
|
export declare const accountMismatch: (machine: MachineFile, userId: string | null) => Error | null;
|
|
@@ -61,8 +61,6 @@ const update = async (change) => withFileLock(machinePath(), () => {
|
|
|
61
61
|
return isDeepStrictEqual(next, machine) ? machine : save(next);
|
|
62
62
|
});
|
|
63
63
|
export const setProfileSource = async (source) => update((machine) => ({ ...machine, profileSource: source }));
|
|
64
|
-
// After a pull or a push: profile.json mirrors the account's profile at `etag`.
|
|
65
|
-
export const setCloud = async (etag) => update((machine) => ({ ...machine, profileSource: 'cloud', profileEtag: etag }));
|
|
66
64
|
export const setAccount = async (accountId) => update((machine) => ({ ...machine, accountId }));
|
|
67
65
|
// `setup --disconnect`: no account; a copy of the account's profile becomes this machine's own.
|
|
68
66
|
export const forgetAccount = async () => update((machine) => ({
|
package/dist/lib/json-codes.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const CODES: readonly ["already_installed", "already_running", "auth_required", "auth_unavailable", "build_failed", "cancelled", "confirmation_required", "daemon_disabled", "daemon_unavailable", "daemon_unresponsive", "disk_full", "error", "extension_failed", "integrity_drift", "invalid_argument", "invalid_manifest", "market_auth", "market_failed", "market_timeout", "market_unreachable", "needed_by_dependents", "network_unavailable", "no_discovery", "no_manifest", "no_market", "not_conforming", "not_installed", "
|
|
1
|
+
export declare const CODES: readonly ["already_installed", "already_running", "auth_required", "auth_unavailable", "build_failed", "cancelled", "confirmation_required", "daemon_disabled", "daemon_unavailable", "daemon_unresponsive", "disk_full", "error", "extension_failed", "integrity_drift", "invalid_argument", "invalid_manifest", "market_auth", "market_failed", "market_timeout", "market_unreachable", "needed_by_dependents", "network_unavailable", "no_discovery", "no_manifest", "no_market", "not_conforming", "not_installed", "permission_denied", "port_in_use", "profile_invalid", "release_refused", "secret_refused", "stop_failed", "timeout", "token_request_failed", "uncommitted_changes", "usage"];
|
|
2
2
|
export type Code = (typeof CODES)[number];
|
package/dist/lib/json-codes.js
CHANGED
package/dist/lib/json-output.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { CommanderError } from 'commander';
|
|
2
|
-
import { NotPushedError } from '../core/profile/cloud.js';
|
|
3
2
|
import { AuthRequiredError, TransientAuthError } from './auth/auth-errors.js';
|
|
4
3
|
import { TokenRequestError } from './auth/oauth.js';
|
|
5
4
|
import {} from './json-codes.js';
|
|
@@ -29,7 +28,6 @@ export class DaemonError extends Error {
|
|
|
29
28
|
const KNOWN = [
|
|
30
29
|
[AuthRequiredError, 'auth_required'],
|
|
31
30
|
[TransientAuthError, 'auth_unavailable'],
|
|
32
|
-
[NotPushedError, 'not_pushed'],
|
|
33
31
|
[TokenRequestError, 'token_request_failed'],
|
|
34
32
|
];
|
|
35
33
|
// What the system says of an expected failure: a folder or file we may not use, a full disk.
|
package/dist/program-deps.js
CHANGED
|
@@ -23,7 +23,7 @@ import { runExtensionSetup } from './commands/extension/setup.js';
|
|
|
23
23
|
import { runExtensionDown, runExtensionToken, runExtensionUp } from './commands/extension/up.js';
|
|
24
24
|
import { runStatus, statusDeps } from './commands/status/status.js';
|
|
25
25
|
import { runUpdate } from './commands/update/update.js';
|
|
26
|
-
import {
|
|
26
|
+
import { prepareManifest, prepareNpm } from './core/installer/npm.js';
|
|
27
27
|
import { ensureSignedIn, signOut } from './lib/auth/sign-in.js';
|
|
28
28
|
import { ensureDaemon } from './lib/daemon/daemon-client.js';
|
|
29
29
|
import { sealOffLine } from './lib/daemon/daemon-health.js';
|
|
@@ -42,8 +42,8 @@ const setupDeps = {
|
|
|
42
42
|
printStatus: (opts) => runStatus(opts, statusDeps(false)),
|
|
43
43
|
startDaemon: () => ensureDaemon(),
|
|
44
44
|
};
|
|
45
|
-
// What
|
|
46
|
-
const editDeps = { npm: () => prepareNpm(),
|
|
45
|
+
// What setup reaches: npm, and the account when signed in.
|
|
46
|
+
const editDeps = { npm: () => prepareNpm(), account: signedInAccount };
|
|
47
47
|
const installer = () => cliInstaller({
|
|
48
48
|
npm: () => prepareNpm(),
|
|
49
49
|
manifest: () => prepareManifest(),
|
package/package.json
CHANGED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { type CloudFetch, type CloudProfile, type JsonObject, type ProfileWrite } from './cloud.js';
|
|
2
|
-
import { type Profile } from './schema.js';
|
|
3
|
-
export type Apply = (base: CloudProfile) => Promise<ProfileWrite>;
|
|
4
|
-
export interface Pushed {
|
|
5
|
-
etag: string;
|
|
6
|
-
pushed: boolean;
|
|
7
|
-
}
|
|
8
|
-
export interface LockPair {
|
|
9
|
-
lock: JsonObject | null;
|
|
10
|
-
npmMajor: number | null;
|
|
11
|
-
}
|
|
12
|
-
export interface ReplayOptions {
|
|
13
|
-
lockCounts?: boolean;
|
|
14
|
-
}
|
|
15
|
-
export declare const pushBody: (next: Profile, base: CloudProfile, local: LockPair) => ProfileWrite;
|
|
16
|
-
export declare const pushWithReplay: (fetch: CloudFetch, base: CloudProfile, apply: Apply, { lockCounts }?: ReplayOptions) => Promise<Pushed>;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
-
import { pushProfile, } from './cloud.js';
|
|
3
|
-
import { toDocument } from './schema.js';
|
|
4
|
-
// A lock from another npm major than the account's is not pushed: the account keeps its own.
|
|
5
|
-
export const pushBody = (next, base, local) => {
|
|
6
|
-
const other = base.npmMajor !== null && local.npmMajor !== null;
|
|
7
|
-
const keep = other && base.npmMajor !== local.npmMajor;
|
|
8
|
-
const pair = keep ? { lock: base.lock, npmMajor: base.npmMajor } : local;
|
|
9
|
-
return { version: next.version, profile: toDocument(next), ...pair };
|
|
10
|
-
};
|
|
11
|
-
const refuseNewer = (base) => {
|
|
12
|
-
if (!base.readOnly)
|
|
13
|
-
return;
|
|
14
|
-
const newer = `version ${base.profile.version}, newer than this CLI understands`;
|
|
15
|
-
throw new Error(`the profile in your account is ${newer}; it was not changed`);
|
|
16
|
-
};
|
|
17
|
-
// The account holds this already: the same profile and, when the lock counts, the same lock.
|
|
18
|
-
const held = (body, base, lockCounts) => base.etag !== null &&
|
|
19
|
-
isDeepStrictEqual(body.profile, toDocument(base.profile)) &&
|
|
20
|
-
(!lockCounts || isDeepStrictEqual(body.lock, base.lock));
|
|
21
|
-
const attemptOn = async (fetch, base, apply, lockCounts) => {
|
|
22
|
-
refuseNewer(base);
|
|
23
|
-
const body = await apply(base);
|
|
24
|
-
if (held(body, base, lockCounts))
|
|
25
|
-
return { kind: 'done', etag: base.etag, pushed: false };
|
|
26
|
-
const res = await pushProfile(fetch, body, base.etag);
|
|
27
|
-
if (res.kind === 'stored')
|
|
28
|
-
return { kind: 'done', etag: res.etag, pushed: true };
|
|
29
|
-
return { kind: 'conflict', body, current: res.current };
|
|
30
|
-
};
|
|
31
|
-
const shown = (doc) => JSON.stringify(doc, null, 2);
|
|
32
|
-
const conflictError = (body, current) => {
|
|
33
|
-
const theirs = current
|
|
34
|
-
? `in your account (etag ${current.etag}):\n${shown(toDocument(current.profile))}`
|
|
35
|
-
: 'in your account: none';
|
|
36
|
-
return new Error('not pushed: the profile in your account changed again while this change was pushed\n' +
|
|
37
|
-
`yours:\n${shown(body.profile)}\n${theirs}`);
|
|
38
|
-
};
|
|
39
|
-
// On a 412 the change is replayed once on the account's document; a second 412 shows both.
|
|
40
|
-
export const pushWithReplay = async (fetch, base, apply, { lockCounts = false } = {}) => {
|
|
41
|
-
const first = await attemptOn(fetch, base, apply, lockCounts);
|
|
42
|
-
if (first.kind === 'done')
|
|
43
|
-
return { etag: first.etag, pushed: first.pushed };
|
|
44
|
-
const retry = first.current ?? { ...base, etag: null };
|
|
45
|
-
const second = await attemptOn(fetch, retry, apply, lockCounts);
|
|
46
|
-
if (second.kind === 'done')
|
|
47
|
-
return { etag: second.etag, pushed: second.pushed };
|
|
48
|
-
throw conflictError(second.body, second.current);
|
|
49
|
-
};
|