@countinghouse/ch-local 0.0.0-stage → 1.0.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.
- package/LICENSE +202 -0
- package/README.md +361 -2
- package/bin/ch-local.js +472 -0
- package/clients/claude-code/.claude-plugin/plugin.json +6 -0
- package/clients/claude-code/.mcp.json +11 -0
- package/clients/claude-code/bin/launch.js +235 -0
- package/clients/claude-code/bin/win-acl.js +420 -0
- package/clients/claude-code/skills/ci-failure-triage/SKILL.md +21 -0
- package/clients/claude-code/skills/countinghouse-module/SKILL.md +177 -0
- package/clients/claude-code/skills/countinghouse-setup/SKILL.md +74 -0
- package/lib/allowlist.js +65 -0
- package/lib/aside.js +16 -0
- package/lib/atomic.js +45 -0
- package/lib/auto-apply.js +245 -0
- package/lib/auto-upgrade.js +98 -0
- package/lib/availability.js +307 -0
- package/lib/ceiling.js +280 -0
- package/lib/clean-text.js +20 -0
- package/lib/cli-change.js +38 -0
- package/lib/client-conditions-fixtures.js +143 -0
- package/lib/client-conditions.js +656 -0
- package/lib/clients.js +179 -0
- package/lib/command-line.js +202 -0
- package/lib/conditions-command.js +116 -0
- package/lib/control.js +134 -0
- package/lib/credential-store.js +414 -0
- package/lib/credentials-command.js +344 -0
- package/lib/daemon-requests.js +137 -0
- package/lib/daemon.js +888 -0
- package/lib/describe-module.js +137 -0
- package/lib/description-check.js +175 -0
- package/lib/failed-json.js +43 -0
- package/lib/gc.js +274 -0
- package/lib/github-token.js +57 -0
- package/lib/handoff-io.js +73 -0
- package/lib/held-back.js +116 -0
- package/lib/hidden-input.js +64 -0
- package/lib/history.js +49 -0
- package/lib/home.js +372 -0
- package/lib/in-place.js +1036 -0
- package/lib/install-plan.js +142 -0
- package/lib/install.js +846 -0
- package/lib/installed-description.js +153 -0
- package/lib/integrity.js +7 -0
- package/lib/lock.js +456 -0
- package/lib/lockfile-rules.js +98 -0
- package/lib/machine.js +20 -0
- package/lib/module-credentials.js +403 -0
- package/lib/module-record.js +298 -0
- package/lib/module-roles.js +44 -0
- package/lib/module-seams.js +26 -0
- package/lib/module-store.js +202 -0
- package/lib/module-values.js +334 -0
- package/lib/names.js +31 -0
- package/lib/next-steps.js +8 -0
- package/lib/node-check.js +56 -0
- package/lib/npm-fetch.js +123 -0
- package/lib/npm-release.js +99 -0
- package/lib/npm-upgrade.js +146 -0
- package/lib/npm.js +41 -0
- package/lib/outside-edits.js +162 -0
- package/lib/own-applied.js +175 -0
- package/lib/own-command.js +380 -0
- package/lib/own-file.js +120 -0
- package/lib/own-record.js +96 -0
- package/lib/own-tree.js +336 -0
- package/lib/pack-table.js +290 -0
- package/lib/packs-command.js +570 -0
- package/lib/packs.js +219 -0
- package/lib/pins.js +64 -0
- package/lib/pipe-probe.js +82 -0
- package/lib/plain-read.js +63 -0
- package/lib/private-file.js +90 -0
- package/lib/private-write.js +33 -0
- package/lib/provenance.js +234 -0
- package/lib/record.js +82 -0
- package/lib/registry.js +22 -0
- package/lib/release-pins.js +350 -0
- package/lib/release-pins.json +4225 -0
- package/lib/release-tags.js +12 -0
- package/lib/release.js +36 -0
- package/lib/restart-command.js +161 -0
- package/lib/rollback.js +227 -0
- package/lib/run-liveness.js +15 -0
- package/lib/runtime-log.js +287 -0
- package/lib/sandbox-support.js +17 -0
- package/lib/semver-order.js +40 -0
- package/lib/session-socket.js +69 -0
- package/lib/session-table.js +238 -0
- package/lib/session.js +281 -0
- package/lib/settings-command.js +82 -0
- package/lib/settings.js +564 -0
- package/lib/shim-binary.js +152 -0
- package/lib/shim-config.js +159 -0
- package/lib/shim-install.js +365 -0
- package/lib/sigstore/README.md +44 -0
- package/lib/sigstore/npm-keys.json +18 -0
- package/lib/sigstore/trusted-root.json +126 -0
- package/lib/snippets.js +76 -0
- package/lib/start.js +721 -0
- package/lib/store-files.js +48 -0
- package/lib/store-paths.js +33 -0
- package/lib/swap-preload.js +38 -0
- package/lib/tar.js +220 -0
- package/lib/uninstall.js +314 -0
- package/lib/unpack.js +41 -0
- package/lib/upgrade-stamp.js +63 -0
- package/lib/upgrade.js +281 -0
- package/lib/watch.js +40 -0
- package/lib/win-acl.js +420 -0
- package/node_modules/@sigstore/bundle/LICENSE +202 -0
- package/node_modules/@sigstore/bundle/README.md +19 -0
- package/node_modules/@sigstore/bundle/dist/build.d.ts +18 -0
- package/node_modules/@sigstore/bundle/dist/build.js +100 -0
- package/node_modules/@sigstore/bundle/dist/bundle.d.ts +74 -0
- package/node_modules/@sigstore/bundle/dist/bundle.js +24 -0
- package/node_modules/@sigstore/bundle/dist/error.d.ts +4 -0
- package/node_modules/@sigstore/bundle/dist/error.js +25 -0
- package/node_modules/@sigstore/bundle/dist/index.d.ts +8 -0
- package/node_modules/@sigstore/bundle/dist/index.js +43 -0
- package/node_modules/@sigstore/bundle/dist/serialized.d.ts +74 -0
- package/node_modules/@sigstore/bundle/dist/serialized.js +49 -0
- package/node_modules/@sigstore/bundle/dist/utility.d.ts +14 -0
- package/node_modules/@sigstore/bundle/dist/utility.js +2 -0
- package/node_modules/@sigstore/bundle/dist/validate.d.ts +7 -0
- package/node_modules/@sigstore/bundle/dist/validate.js +199 -0
- package/node_modules/@sigstore/bundle/package.json +35 -0
- package/node_modules/@sigstore/core/LICENSE +202 -0
- package/node_modules/@sigstore/core/README.md +10 -0
- package/node_modules/@sigstore/core/dist/asn1/error.d.ts +4 -0
- package/node_modules/@sigstore/core/dist/asn1/error.js +24 -0
- package/node_modules/@sigstore/core/dist/asn1/index.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/asn1/index.js +20 -0
- package/node_modules/@sigstore/core/dist/asn1/length.d.ts +3 -0
- package/node_modules/@sigstore/core/dist/asn1/length.js +62 -0
- package/node_modules/@sigstore/core/dist/asn1/obj.d.ts +14 -0
- package/node_modules/@sigstore/core/dist/asn1/obj.js +155 -0
- package/node_modules/@sigstore/core/dist/asn1/parse.d.ts +6 -0
- package/node_modules/@sigstore/core/dist/asn1/parse.js +124 -0
- package/node_modules/@sigstore/core/dist/asn1/tag.d.ts +16 -0
- package/node_modules/@sigstore/core/dist/asn1/tag.js +89 -0
- package/node_modules/@sigstore/core/dist/crypto.d.ts +6 -0
- package/node_modules/@sigstore/core/dist/crypto.js +69 -0
- package/node_modules/@sigstore/core/dist/dsse.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/dsse.js +29 -0
- package/node_modules/@sigstore/core/dist/encoding.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/encoding.js +27 -0
- package/node_modules/@sigstore/core/dist/index.d.ts +9 -0
- package/node_modules/@sigstore/core/dist/index.js +66 -0
- package/node_modules/@sigstore/core/dist/json.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/json.js +60 -0
- package/node_modules/@sigstore/core/dist/oid.d.ts +3 -0
- package/node_modules/@sigstore/core/dist/oid.js +20 -0
- package/node_modules/@sigstore/core/dist/pem.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/pem.js +43 -0
- package/node_modules/@sigstore/core/dist/rfc3161/error.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/rfc3161/error.js +21 -0
- package/node_modules/@sigstore/core/dist/rfc3161/index.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/rfc3161/index.js +20 -0
- package/node_modules/@sigstore/core/dist/rfc3161/timestamp.d.ts +36 -0
- package/node_modules/@sigstore/core/dist/rfc3161/timestamp.js +212 -0
- package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.d.ts +12 -0
- package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.js +72 -0
- package/node_modules/@sigstore/core/dist/stream.d.ts +21 -0
- package/node_modules/@sigstore/core/dist/stream.js +117 -0
- package/node_modules/@sigstore/core/dist/x509/cert.d.ts +43 -0
- package/node_modules/@sigstore/core/dist/x509/cert.js +245 -0
- package/node_modules/@sigstore/core/dist/x509/ext.d.ts +41 -0
- package/node_modules/@sigstore/core/dist/x509/ext.js +146 -0
- package/node_modules/@sigstore/core/dist/x509/index.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/x509/index.js +23 -0
- package/node_modules/@sigstore/core/dist/x509/sct.d.ts +25 -0
- package/node_modules/@sigstore/core/dist/x509/sct.js +158 -0
- package/node_modules/@sigstore/core/package.json +31 -0
- package/node_modules/@sigstore/protobuf-specs/LICENSE +202 -0
- package/node_modules/@sigstore/protobuf-specs/README.md +5 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.d.ts +44 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.js +59 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.d.ts +64 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.js +174 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.d.ts +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.js +141 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.d.ts +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.js +35 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.d.ts +1309 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.js +2191 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.d.ts +113 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.js +29 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.d.ts +23 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.js +55 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.d.ts +43 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.js +81 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.d.ts +22 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.js +56 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.d.ts +35 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.js +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.d.ts +146 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.js +103 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.d.ts +304 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.js +612 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.d.ts +33 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.js +55 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.d.ts +144 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.js +137 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.d.ts +386 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.js +284 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.d.ts +191 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.js +281 -0
- package/node_modules/@sigstore/protobuf-specs/dist/index.d.ts +6 -0
- package/node_modules/@sigstore/protobuf-specs/dist/index.js +37 -0
- package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.d.ts +4 -0
- package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.js +35 -0
- package/node_modules/@sigstore/protobuf-specs/package.json +35 -0
- package/node_modules/@sigstore/verify/README.md +9 -0
- package/node_modules/@sigstore/verify/dist/bundle/dsse.d.ts +12 -0
- package/node_modules/@sigstore/verify/dist/bundle/dsse.js +44 -0
- package/node_modules/@sigstore/verify/dist/bundle/index.d.ts +4 -0
- package/node_modules/@sigstore/verify/dist/bundle/index.js +59 -0
- package/node_modules/@sigstore/verify/dist/bundle/message.d.ts +13 -0
- package/node_modules/@sigstore/verify/dist/bundle/message.js +54 -0
- package/node_modules/@sigstore/verify/dist/error.d.ts +16 -0
- package/node_modules/@sigstore/verify/dist/error.js +34 -0
- package/node_modules/@sigstore/verify/dist/index.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/index.js +28 -0
- package/node_modules/@sigstore/verify/dist/key/certificate.d.ts +21 -0
- package/node_modules/@sigstore/verify/dist/key/certificate.js +216 -0
- package/node_modules/@sigstore/verify/dist/key/index.d.ts +10 -0
- package/node_modules/@sigstore/verify/dist/key/index.js +75 -0
- package/node_modules/@sigstore/verify/dist/key/sct.d.ts +4 -0
- package/node_modules/@sigstore/verify/dist/key/sct.js +78 -0
- package/node_modules/@sigstore/verify/dist/policy.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/policy.js +50 -0
- package/node_modules/@sigstore/verify/dist/shared.types.d.ts +50 -0
- package/node_modules/@sigstore/verify/dist/shared.types.js +2 -0
- package/node_modules/@sigstore/verify/dist/timestamp/index.d.ts +11 -0
- package/node_modules/@sigstore/verify/dist/timestamp/index.js +24 -0
- package/node_modules/@sigstore/verify/dist/timestamp/tsa.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/timestamp/tsa.js +63 -0
- package/node_modules/@sigstore/verify/dist/tlog/checkpoint.d.ts +11 -0
- package/node_modules/@sigstore/verify/dist/tlog/checkpoint.js +154 -0
- package/node_modules/@sigstore/verify/dist/tlog/dsse.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/tlog/dsse.js +106 -0
- package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.js +94 -0
- package/node_modules/@sigstore/verify/dist/tlog/index.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/tlog/index.js +92 -0
- package/node_modules/@sigstore/verify/dist/tlog/intoto.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/intoto.js +62 -0
- package/node_modules/@sigstore/verify/dist/tlog/merkle.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/merkle.js +104 -0
- package/node_modules/@sigstore/verify/dist/tlog/set.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/set.js +60 -0
- package/node_modules/@sigstore/verify/dist/trust/filter.d.ts +8 -0
- package/node_modules/@sigstore/verify/dist/trust/filter.js +23 -0
- package/node_modules/@sigstore/verify/dist/trust/index.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/trust/index.js +90 -0
- package/node_modules/@sigstore/verify/dist/trust/trust.types.d.ts +29 -0
- package/node_modules/@sigstore/verify/dist/trust/trust.types.js +2 -0
- package/node_modules/@sigstore/verify/dist/verifier.d.ts +31 -0
- package/node_modules/@sigstore/verify/dist/verifier.js +160 -0
- package/node_modules/@sigstore/verify/package.json +36 -0
- package/package.json +37 -4
package/bin/ch-local.js
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
// ch-local: the countinghouse packs' launcher.
|
|
4
|
+
//
|
|
5
|
+
// ch-local install --pack <name> --from <dir> the pack's pinned tarballs into <home>/app
|
|
6
|
+
// ch-local install [--from <dir>] reinstalls what this home lists, at this
|
|
7
|
+
// ch-local's own release: never adds a pack,
|
|
8
|
+
// never moves to another release
|
|
9
|
+
// ch-local snippet <client> the client's config entry, on stdout alone
|
|
10
|
+
// ch-local add <client> writes that entry, and records it
|
|
11
|
+
// ch-local uninstall [--keep-data] takes back what install and add wrote
|
|
12
|
+
// ch-local restart the shared runtime takes what is stored; its sessions resume
|
|
13
|
+
// ch-local upgrade [--check | --auto on|off] installed and available versions; asks once (--check: never)
|
|
14
|
+
// ch-local session [--home <abs>] what a client runs: the shared runtime through the shim, or its own
|
|
15
|
+
// ch-local start --stdio [options] this session's own runtime with the installed packs
|
|
16
|
+
// ch-local add-pack <name> --from <dir> another pack beside the installed ones
|
|
17
|
+
// ch-local remove-pack <name> a pack, and the parts no other pack calls
|
|
18
|
+
// ch-local add-module <dir> your own module, asked in your own terminal
|
|
19
|
+
// ch-local remove-module <name> one of your own modules; its data and key kept
|
|
20
|
+
// ch-local status the installed packs, their tools and missing credentials
|
|
21
|
+
// ch-local rollback [--yes] back to the release the last install replaced
|
|
22
|
+
//
|
|
23
|
+
// The Node floor is checked before anything else is loaded, with plain
|
|
24
|
+
// require names, so a Node far below it still gets a plain answer.
|
|
25
|
+
const os = require('os');
|
|
26
|
+
const {check, offerNode} = require('../lib/node-check');
|
|
27
|
+
|
|
28
|
+
const CLIENT_LIST = 'claude-code|cursor|vscode|codex|claude-desktop';
|
|
29
|
+
const USAGE = `usage: ch-local snippet <${CLIENT_LIST}>
|
|
30
|
+
ch-local install --pack <name> --from <directory of tarballs>
|
|
31
|
+
ch-local install [--from <directory of tarballs>]
|
|
32
|
+
reinstalls what this home lists, at this ch-local's own release:
|
|
33
|
+
it never adds a pack, and never moves to another release
|
|
34
|
+
ch-local add-pack <name> --from <directory of tarballs>
|
|
35
|
+
ch-local remove-pack <name>
|
|
36
|
+
ch-local add-module <directory of your own module>
|
|
37
|
+
ch-local remove-module <name>
|
|
38
|
+
ch-local status
|
|
39
|
+
ch-local rollback [--yes]
|
|
40
|
+
ch-local add <cursor|vscode|codex>
|
|
41
|
+
ch-local uninstall [--keep-data]
|
|
42
|
+
ch-local restart
|
|
43
|
+
ch-local upgrade [--check | --auto on|off]
|
|
44
|
+
ch-local settings [get | set <name> <value> | reset]
|
|
45
|
+
ch-local settings modules [<module>]
|
|
46
|
+
ch-local settings set <module>.<setting> [<value>]
|
|
47
|
+
ch-local settings reset <module>[.<setting>]
|
|
48
|
+
ch-local credentials [list | add <module> [--to <origin>] | remove <module>]
|
|
49
|
+
ch-local session [--home <absolute home>]
|
|
50
|
+
ch-local start --stdio [countinghouse options]
|
|
51
|
+
`;
|
|
52
|
+
|
|
53
|
+
// argv -> {flags, values, positional} | null when something is not understood.
|
|
54
|
+
function parse(argv, {flags = [], values = []}) {
|
|
55
|
+
const out = {flags: {}, values: {}, positional: []};
|
|
56
|
+
for (let i = 0; i < argv.length; i++) {
|
|
57
|
+
const a = argv[i];
|
|
58
|
+
if (flags.includes(a)) out.flags[a] = true;
|
|
59
|
+
else if (values.includes(a) && i + 1 < argv.length && !argv[i + 1].startsWith('--')) out.values[a] = argv[++i];
|
|
60
|
+
else if (!a.startsWith('--')) out.positional.push(a);
|
|
61
|
+
else return null;
|
|
62
|
+
}
|
|
63
|
+
return out;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// argv -> {command, ...its arguments} | null (a usage error). The command
|
|
67
|
+
// line's syntax alone, the one place it is decided: main dispatches on what
|
|
68
|
+
// this returns, and the hints' pin (test/command-line.test.js) parses every
|
|
69
|
+
// printed command with it. Checks that read the disk (an absolute --home, a
|
|
70
|
+
// directory) stay with the command.
|
|
71
|
+
const ON_HOME = new Set(['add-pack', 'remove-pack', 'add-module', 'remove-module', 'rollback', 'status', 'add', 'credentials', 'uninstall', 'settings', 'upgrade', 'restart']);
|
|
72
|
+
function parseCommand(argv) {
|
|
73
|
+
const [command, ...rest] = argv;
|
|
74
|
+
const one = () => rest.length === 1 && !rest[0].startsWith('--');
|
|
75
|
+
switch (command) {
|
|
76
|
+
case 'snippet': return rest.length === 1 ? {command, client: rest[0]} : null;
|
|
77
|
+
case 'install': {
|
|
78
|
+
const a = parse(rest, {values: ['--pack', '--from', '--unreleased-pins']});
|
|
79
|
+
if (a == null || a.positional.length > 0) return null;
|
|
80
|
+
// (no --pack: reinstall what the record lists; install says why
|
|
81
|
+
// --unreleased-pins needs one, the medium on the install hints, 6)
|
|
82
|
+
return {command, pack: a.values['--pack'], from: a.values['--from'], unreleasedPins: a.values['--unreleased-pins']};
|
|
83
|
+
}
|
|
84
|
+
case 'add-pack': {
|
|
85
|
+
const a = parse(rest, {values: ['--from', '--unreleased-pins']});
|
|
86
|
+
return a == null || a.positional.length !== 1 ? null : {command, pack: a.positional[0], from: a.values['--from'], unreleasedPins: a.values['--unreleased-pins']};
|
|
87
|
+
}
|
|
88
|
+
case 'remove-pack': return one() ? {command, pack: rest[0]} : null;
|
|
89
|
+
case 'add-module': return one() ? {command, dir: rest[0]} : null;
|
|
90
|
+
case 'remove-module': return one() ? {command, name: rest[0]} : null;
|
|
91
|
+
case 'rollback': {
|
|
92
|
+
const a = parse(rest, {flags: ['--yes']});
|
|
93
|
+
return a == null || a.positional.length > 0 ? null : {command, yes: a.flags['--yes'] === true};
|
|
94
|
+
}
|
|
95
|
+
case 'status': return rest.length === 0 ? {command} : null;
|
|
96
|
+
case 'restart': return rest.length === 0 ? {command} : null;
|
|
97
|
+
case 'add': return rest.length === 1 ? {command, client: rest[0]} : null;
|
|
98
|
+
case 'credentials': {
|
|
99
|
+
const a = parse(rest, {values: ['--to']});
|
|
100
|
+
if (a == null) return null;
|
|
101
|
+
const [action = 'list', name, ...more] = a.positional;
|
|
102
|
+
if (more.length > 0 || (action === 'list' && (name !== undefined || a.values['--to'] !== undefined))) return null;
|
|
103
|
+
if ((action === 'add' || action === 'remove') && name === undefined) return null;
|
|
104
|
+
if (action === 'remove' && a.values['--to'] !== undefined) return null;
|
|
105
|
+
return {command, action, name, to: a.values['--to'] || null};
|
|
106
|
+
}
|
|
107
|
+
case 'uninstall': {
|
|
108
|
+
const a = parse(rest, {flags: ['--keep-data']});
|
|
109
|
+
return a == null || a.positional.length > 0 ? null : {command, keepData: a.flags['--keep-data'] === true};
|
|
110
|
+
}
|
|
111
|
+
case 'conditions': {
|
|
112
|
+
const a = parse(rest, {values: ['--for', '--home']});
|
|
113
|
+
return a == null || a.positional.length > 0 || a.values['--for'] === undefined || a.values['--home'] === undefined ? null : {command, pid: a.values['--for'], home: a.values['--home']};
|
|
114
|
+
}
|
|
115
|
+
case 'daemon': {
|
|
116
|
+
// --handoff: started by a daemon re-execing onto a new ch-local (C3b),
|
|
117
|
+
// with the session table on fd 3, never in argv or env
|
|
118
|
+
const a = parse(rest, {values: ['--home'], flags: ['--handoff']});
|
|
119
|
+
return a == null || a.positional.length > 0 || a.values['--home'] === undefined ? null : {command, home: a.values['--home'], handoff: a.flags['--handoff'] === true};
|
|
120
|
+
}
|
|
121
|
+
case 'session': {
|
|
122
|
+
// (--home: the entry clients add writes, so a client's
|
|
123
|
+
// COUNTINGHOUSE_HOME can't pick another home; the plugin's has none,
|
|
124
|
+
// and takes the home rule, as its launch.js found this launcher by)
|
|
125
|
+
const a = parse(rest, {values: ['--home']});
|
|
126
|
+
// (once at most: a second --home is refused, never the last one taken)
|
|
127
|
+
if (a == null || a.positional.length > 0 || rest.filter((x) => x === '--home').length > 1) return null;
|
|
128
|
+
return {command, home: a.values['--home']};
|
|
129
|
+
}
|
|
130
|
+
// (start's own options are read with the disk checks they need, below)
|
|
131
|
+
case 'start': return rest[0] === '--stdio' ? {command, rest} : null;
|
|
132
|
+
case 'settings': {
|
|
133
|
+
if (rest.length === 0 || (rest.length === 1 && rest[0] === 'get')) return {command, action: 'get'};
|
|
134
|
+
if ((rest.length === 3 || rest.length === 2) && rest[0] === 'set') return {command, action: 'set', name: rest[1], value: rest[2]};
|
|
135
|
+
if ((rest.length === 1 || rest.length === 2) && rest[0] === 'reset') return {command, action: 'reset', name: rest[1]};
|
|
136
|
+
if ((rest.length === 1 || rest.length === 2) && rest[0] === 'modules') return {command, action: 'modules', name: rest[1]};
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
case 'upgrade': {
|
|
140
|
+
const a = parse(rest, {values: ['--auto'], flags: ['--apply-auto', '--check']});
|
|
141
|
+
if (a == null || a.positional.length > 0) return null;
|
|
142
|
+
// (--check is read-only: never with another mode; the medium on the
|
|
143
|
+
// easy upgrades, 1)
|
|
144
|
+
if (a.flags['--check'] && (a.values['--auto'] !== undefined || a.flags['--apply-auto'])) return null;
|
|
145
|
+
if (a.flags['--apply-auto'] && a.values['--auto'] !== undefined) return null;
|
|
146
|
+
return {command, auto: a.values['--auto'], check: a.flags['--check'] === true, applyAuto: a.flags['--apply-auto'] === true};
|
|
147
|
+
}
|
|
148
|
+
default: return null;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function snippetCommand(client, io) {
|
|
153
|
+
const {snippet, CLIENTS} = require('../lib/snippets');
|
|
154
|
+
const {launcherPath} = require('../lib/home');
|
|
155
|
+
if (!CLIENTS.includes(client)) {
|
|
156
|
+
if (client !== undefined) io.stderr.write(`ch-local: unknown client "${client}"\n`);
|
|
157
|
+
io.stderr.write(USAGE);
|
|
158
|
+
return 2;
|
|
159
|
+
}
|
|
160
|
+
const {home} = require('../lib/home');
|
|
161
|
+
const h = home(io.env, io.homedir);
|
|
162
|
+
const found = require('../lib/shim-binary').shimFor({home: h, locate: io.locateShim ?? undefined});
|
|
163
|
+
if (found.note != null) io.stderr.write(found.note);
|
|
164
|
+
const {text, note} = snippet(client, {node: io.node, launcher: launcherPath(io.env, io.homedir), home: h});
|
|
165
|
+
io.stdout.write(text);
|
|
166
|
+
if (note != null) {
|
|
167
|
+
io.stderr.write(note);
|
|
168
|
+
} else {
|
|
169
|
+
io.stderr.write(`ch-local: the entry runs the Node at ${io.node}.\n`);
|
|
170
|
+
const where = require('../lib/home').layout(io.env, io.homedir);
|
|
171
|
+
io.stderr.write(`ch-local: an nvm or system Node upgrade changes this path; run ${require('../lib/command-line').hintCommand(where, `snippet ${client}`, io.platform)} again to reprint the entry.\n`);
|
|
172
|
+
}
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async function main(argv, io) {
|
|
177
|
+
if (!check(io.version).ok) {
|
|
178
|
+
io.stderr.write(`ch-local: needs Node 22.16 or later; this is ${io.version}\n`);
|
|
179
|
+
await offerNode(io);
|
|
180
|
+
return 1;
|
|
181
|
+
}
|
|
182
|
+
const usage = () => { io.stderr.write(USAGE); return 2; };
|
|
183
|
+
const p = parseCommand(argv);
|
|
184
|
+
if (p == null) return usage();
|
|
185
|
+
const {command} = p;
|
|
186
|
+
try {
|
|
187
|
+
// every command that acts on a home refuses one another user could
|
|
188
|
+
// change, here, before any of it runs (the reviewer's Fable high on
|
|
189
|
+
// #126, F5). install, session, start, daemon and conditions make their
|
|
190
|
+
// own check, with their own words; snippet acts on none.
|
|
191
|
+
// (the background check, upgrade --apply-auto, says its own paused line:
|
|
192
|
+
// the medium on #126's round 2)
|
|
193
|
+
if (ON_HOME.has(command) && !(command === 'upgrade' && p.applyAuto)) {
|
|
194
|
+
const {layout, openHomeLine} = require('../lib/home');
|
|
195
|
+
const open = openHomeLine(layout(io.env, io.homedir).home);
|
|
196
|
+
if (open != null) {
|
|
197
|
+
io.stderr.write(`ch-local: ${open}; nothing was changed\n`);
|
|
198
|
+
// (uninstall: the client entries it would remove are named from
|
|
199
|
+
// ch-local's own list of the files `add` writes, never from the home's
|
|
200
|
+
// record of them: the reviewer on #126's round 2, (6))
|
|
201
|
+
if (command === 'uninstall') {
|
|
202
|
+
const {FILES} = require('../lib/clients');
|
|
203
|
+
const files = Object.values(FILES).map((f) => f(io.homedir(), io.env));
|
|
204
|
+
io.stderr.write(`ch-local: the client entries \`add\` writes are not removed from a home that can't be trusted: remove the "countinghouse" server from each of these by hand, if it is there: ${files.join(', ')}; or move the home aside, install again, and uninstall then. (add writes no file for Claude Code or Claude Desktop: the Claude Code plugin is removed with Claude Code's own /plugin)\n`);
|
|
205
|
+
}
|
|
206
|
+
return 2;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (command === 'snippet') return snippetCommand(p.client, io);
|
|
210
|
+
if (command === 'install') {
|
|
211
|
+
const code = await require('../lib/install').install({pack: p.pack, from: p.from, unreleasedPins: p.unreleasedPins}, io);
|
|
212
|
+
// asked once whether to turn automatic upgrades on (off with no
|
|
213
|
+
// terminal), and the last line, what to do next (A-firstrun d1): a
|
|
214
|
+
// first install's, never a reinstall's (the medium on the install
|
|
215
|
+
// hints, 10). A reinstall at a terminal still asks for a credential
|
|
216
|
+
// a module is missing, as every install does; with none, it names
|
|
217
|
+
// the command instead.
|
|
218
|
+
if (code === 0 && p.pack !== undefined) await require('../lib/upgrade').offerAuto(io);
|
|
219
|
+
if (code === 0 && p.pack !== undefined) io.stdout.write(`${require('../lib/next-steps').READY_LINE}\n`);
|
|
220
|
+
return code;
|
|
221
|
+
}
|
|
222
|
+
if (command === 'add-pack') {
|
|
223
|
+
const report = {added: false};
|
|
224
|
+
const code = await require('../lib/packs-command').addPack({pack: p.pack, from: p.from, unreleasedPins: p.unreleasedPins, report}, io);
|
|
225
|
+
// the last line, when it added the pack: what to do next (A-firstrun d1)
|
|
226
|
+
if (code === 0 && report.added) io.stdout.write(`${require('../lib/next-steps').READY_LINE}\n`);
|
|
227
|
+
return code;
|
|
228
|
+
}
|
|
229
|
+
if (command === 'remove-pack') return await require('../lib/packs-command').removePack({pack: p.pack}, io);
|
|
230
|
+
if (command === 'add-module') return await require('../lib/own-command').addModule({dir: p.dir}, io);
|
|
231
|
+
if (command === 'remove-module') return await require('../lib/own-command').removeModule({name: p.name}, io);
|
|
232
|
+
if (command === 'rollback') return await require('../lib/rollback').rollback({yes: p.yes}, io);
|
|
233
|
+
if (command === 'status') return await require('../lib/packs-command').status({}, io);
|
|
234
|
+
if (command === 'restart') return await require('../lib/restart-command').restart({}, io);
|
|
235
|
+
if (command === 'add') return await require('../lib/clients').add(p.client, io);
|
|
236
|
+
if (command === 'credentials') return await require('../lib/credentials-command').credentials({action: p.action, name: p.name, to: p.to}, io);
|
|
237
|
+
if (command === 'uninstall') return await require('../lib/uninstall').uninstall({keepData: p.keepData}, io);
|
|
238
|
+
if (command === 'conditions') {
|
|
239
|
+
const {isFullPath} = require('../lib/home');
|
|
240
|
+
if (!isFullPath(p.home)) { io.stderr.write('ch-local conditions: --home must be an absolute path\n'); return 2; }
|
|
241
|
+
return await require('../lib/conditions-command').conditions({pid: p.pid, home: p.home}, io);
|
|
242
|
+
}
|
|
243
|
+
if (command === 'daemon') {
|
|
244
|
+
const {isFullPath} = require('../lib/home');
|
|
245
|
+
if (!isFullPath(p.home)) { io.stderr.write('ch-local daemon: --home must be an absolute path\n'); return 2; }
|
|
246
|
+
// (daemon.js loaded first, so the version it records is read before the
|
|
247
|
+
// handoff's wait, which an install's swap could land in: the medium on
|
|
248
|
+
// A6, 3)
|
|
249
|
+
const {daemon} = require('../lib/daemon');
|
|
250
|
+
const handoff = p.handoff ? await io.readHandoff() : null;
|
|
251
|
+
// (a cold start's wait counts from this process's start, monotonic; and
|
|
252
|
+
// --handoff is a hand-over even when its pipe could not be read)
|
|
253
|
+
io.daemonUptimeMs = () => process.uptime() * 1000;
|
|
254
|
+
return await daemon({home: p.home, handoff, handedOver: p.handoff === true}, io);
|
|
255
|
+
}
|
|
256
|
+
if (command === 'session') {
|
|
257
|
+
let home = null;
|
|
258
|
+
if (p.home !== undefined) {
|
|
259
|
+
const v = p.home;
|
|
260
|
+
const {isFullPath} = require('../lib/home');
|
|
261
|
+
if (!isFullPath(v)) { io.stderr.write('ch-local session: --home must be an absolute path\n'); return 2; }
|
|
262
|
+
const fs = require('node:fs');
|
|
263
|
+
let st = null;
|
|
264
|
+
try { st = fs.statSync(v); } catch (err) { st = null; }
|
|
265
|
+
if (st == null || !st.isDirectory()) { io.stderr.write(`ch-local session: --home ${v} is not a directory\n`); return 2; }
|
|
266
|
+
// (whose it is, and how open, is the session's choice to judge: a
|
|
267
|
+
// home the shared runtime's daemon would refuse runs the session's
|
|
268
|
+
// own runtime, said -- never a session that ends here)
|
|
269
|
+
home = fs.realpathSync(v);
|
|
270
|
+
}
|
|
271
|
+
return await require('../lib/session').session({home}, io);
|
|
272
|
+
}
|
|
273
|
+
if (command === 'start') {
|
|
274
|
+
const rest = p.rest;
|
|
275
|
+
// ch-local's own options, before any pass-through runtime flag: the
|
|
276
|
+
// native shim's hand-over is `start --stdio --own-runtime --home <abs>`
|
|
277
|
+
// (#236 section 5), so a client's COUNTINGHOUSE_HOME can't pick the home
|
|
278
|
+
let i = 1;
|
|
279
|
+
let home = null;
|
|
280
|
+
// the shim's hand-over says why the session can't share
|
|
281
|
+
// (--own-runtime-reason, a short token: token, credential,
|
|
282
|
+
// conditions-failed; --own-runtime-why: the conditions check's own
|
|
283
|
+
// words), told to the client in its first result's notice (#236
|
|
284
|
+
// section 5). An older shim sends neither: then only that it runs its own.
|
|
285
|
+
let ownRuntime = null;
|
|
286
|
+
for (;;) {
|
|
287
|
+
if (rest[i] === '--own-runtime') { i += 1; ownRuntime = ownRuntime || {}; continue; }
|
|
288
|
+
if (rest[i] === '--own-runtime-reason' || rest[i] === '--own-runtime-why') {
|
|
289
|
+
const v = rest[i + 1];
|
|
290
|
+
// only on the shim's hand-over, after --own-runtime, and never an option as its value
|
|
291
|
+
if (ownRuntime == null || typeof v !== 'string' || v.startsWith('--')) { io.stderr.write(`ch-local start: ${rest[i]} needs --own-runtime before it and a value\n`); return 2; }
|
|
292
|
+
if (rest[i] === '--own-runtime-reason') ownRuntime.reason = /^[a-z][a-z-]{0,31}$/.test(v) ? v : 'unknown';
|
|
293
|
+
// cut and cleaned at the source (lib/clean-text.js)
|
|
294
|
+
else ownRuntime.why = require('../lib/clean-text').cleanText(v, 200);
|
|
295
|
+
i += 2;
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (rest[i] === '--home') {
|
|
299
|
+
const v = rest[i + 1];
|
|
300
|
+
const {isFullPath} = require('../lib/home');
|
|
301
|
+
if (typeof v !== 'string' || !isFullPath(v)) { io.stderr.write('ch-local start: --home must be an absolute path\n'); return 2; }
|
|
302
|
+
let st = null;
|
|
303
|
+
const fs = require('node:fs');
|
|
304
|
+
try { st = fs.statSync(v); } catch (err) { st = null; }
|
|
305
|
+
if (st == null || !st.isDirectory()) { io.stderr.write(`ch-local start: --home ${v} is not a directory\n`); return 2; }
|
|
306
|
+
home = fs.realpathSync(v);
|
|
307
|
+
const owned = require('../lib/home').homeOwnershipProblem(home);
|
|
308
|
+
if (owned != null) { io.stderr.write(`ch-local start: --home ${v}: ${owned}\n`); return 2; }
|
|
309
|
+
i += 2;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
break;
|
|
313
|
+
}
|
|
314
|
+
return await require('../lib/start').start({passThrough: rest.slice(i), home, ownRuntime}, io);
|
|
315
|
+
}
|
|
316
|
+
if (command === 'settings') return await require('../lib/settings-command').settings({action: p.action, name: p.name, value: p.value}, io);
|
|
317
|
+
if (command === 'upgrade') {
|
|
318
|
+
// start's background check runs this, in a child with the allowlisted
|
|
319
|
+
// environment (lib/auto-upgrade.js); it re-checks everything itself
|
|
320
|
+
if (p.applyAuto) {
|
|
321
|
+
const r = await require('../lib/auto-apply').autoApply({io});
|
|
322
|
+
if (r.line != null) io.stdout.write(`${r.line}\n`);
|
|
323
|
+
return 0;
|
|
324
|
+
}
|
|
325
|
+
return await require('../lib/upgrade').upgrade({auto: p.auto, check: p.check}, io);
|
|
326
|
+
}
|
|
327
|
+
} catch (err) {
|
|
328
|
+
// (a home that can't be placed: COUNTINGHOUSE_HOME relative, or on Windows no absolute LOCALAPPDATA: one line, exit 2)
|
|
329
|
+
if (err.code === 'HOME_RELATIVE' || err.code === 'HOME_NO_LOCALAPPDATA') { io.stderr.write(`ch-local: ${err.message}\n`); return 2; }
|
|
330
|
+
throw err;
|
|
331
|
+
}
|
|
332
|
+
return usage();
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// The real io: this process, its terminal, npm through child processes, fetch.
|
|
336
|
+
function realIo() {
|
|
337
|
+
// stdout, stderr and interactive are read only when used: `session` runs
|
|
338
|
+
// the shim in this process's place, and Node opening a stdio pipe makes
|
|
339
|
+
// it O_NONBLOCK for the shim too (lib/session.js)
|
|
340
|
+
return {
|
|
341
|
+
get stdout() { return process.stdout; },
|
|
342
|
+
get stderr() { return process.stderr; },
|
|
343
|
+
env: process.env,
|
|
344
|
+
node: process.execPath,
|
|
345
|
+
version: process.version,
|
|
346
|
+
platform: process.platform,
|
|
347
|
+
homedir: os.homedir,
|
|
348
|
+
ask: (question) => new Promise((resolve) => {
|
|
349
|
+
const rl = require('readline').createInterface({input: process.stdin, output: process.stderr});
|
|
350
|
+
// Resolved before close(): close() fires the 'close' listener at once,
|
|
351
|
+
// which would answer '' first (test/cli.test.js).
|
|
352
|
+
rl.question(question, (answer) => { resolve(answer); rl.close(); });
|
|
353
|
+
rl.on('close', () => resolve(''));
|
|
354
|
+
}),
|
|
355
|
+
// env: the child's whole environment (default: this process's); timeout:
|
|
356
|
+
// ms after which the child is killed (it then answers status null).
|
|
357
|
+
exec: (cmd, args, {cwd, env, timeout} = {}) => new Promise((resolve) => {
|
|
358
|
+
const child = require('child_process').spawn(cmd, args, {cwd, env, timeout, killSignal: 'SIGKILL', stdio: ['ignore', 'pipe', 'pipe']});
|
|
359
|
+
let stdout = '';
|
|
360
|
+
let stderr = '';
|
|
361
|
+
child.stdout.on('data', (d) => { stdout += d; });
|
|
362
|
+
child.stderr.on('data', (d) => { stderr += d; });
|
|
363
|
+
child.on('error', (err) => resolve({status: null, stdout, stderr: stderr + String(err.message)}));
|
|
364
|
+
child.on('close', (status) => resolve({status, stdout, stderr}));
|
|
365
|
+
}),
|
|
366
|
+
fetch: (url, options) => globalThis.fetch(url, options),
|
|
367
|
+
pid: process.pid,
|
|
368
|
+
// A terminal to ask on: rollback over a security fix asks, or needs --yes.
|
|
369
|
+
get interactive() { return Boolean(process.stdin.isTTY); },
|
|
370
|
+
// A terminal both ways (credentials add: the prompt seen, the typing not)
|
|
371
|
+
get terminal() { return Boolean(process.stdin.isTTY && process.stderr.isTTY); },
|
|
372
|
+
// (a terminal on standard input alone: credentials add refuses it)
|
|
373
|
+
get stdinIsTTY() { return Boolean(process.stdin.isTTY); },
|
|
374
|
+
// (add-module's lines are seen: standard output a terminal too)
|
|
375
|
+
get stdoutTTY() { return Boolean(process.stdout.isTTY); },
|
|
376
|
+
readHidden: (prompt) => require('../lib/hidden-input').readHidden(prompt),
|
|
377
|
+
// standard input, read whole up to max bytes (credentials add --to: a
|
|
378
|
+
// key piped in); more than max is cut there, and the caller refuses it
|
|
379
|
+
// at most `max` bytes, until the input ends or closes; an open pipe
|
|
380
|
+
// that sends nothing for timeoutMs is a TIMEOUT, never a wait forever
|
|
381
|
+
// (the Reviewer's high review of L5a, 4). The wait restarts with each
|
|
382
|
+
// piece that arrives: a slow producer (a password manager's prompt) is
|
|
383
|
+
// not cut off while it is sending.
|
|
384
|
+
readStdin: (max, {timeoutMs = 60000} = {}) => new Promise((resolve, reject) => {
|
|
385
|
+
const chunks = [];
|
|
386
|
+
let size = 0;
|
|
387
|
+
let done = false;
|
|
388
|
+
let timer = null;
|
|
389
|
+
const finish = (fn, v) => { if (done) return; done = true; clearTimeout(timer); fn(v); };
|
|
390
|
+
const arm = () => {
|
|
391
|
+
clearTimeout(timer);
|
|
392
|
+
timer = setTimeout(() => {
|
|
393
|
+
process.stdin.destroy();
|
|
394
|
+
finish(reject, Object.assign(new Error(`standard input sent nothing for ${timeoutMs / 1000} s`), {code: 'TIMEOUT'}));
|
|
395
|
+
}, timeoutMs);
|
|
396
|
+
};
|
|
397
|
+
arm();
|
|
398
|
+
const text = () => Buffer.concat(chunks).subarray(0, max).toString('utf8');
|
|
399
|
+
// past max, no more waiting for an end that may never come (`yes |`):
|
|
400
|
+
// refused as too long at once (the Reviewer's round 3, 7)
|
|
401
|
+
process.stdin.on('data', (c) => {
|
|
402
|
+
if (done) return;
|
|
403
|
+
chunks.push(c);
|
|
404
|
+
size += c.length;
|
|
405
|
+
if (size > max) {
|
|
406
|
+
process.stdin.destroy();
|
|
407
|
+
finish(reject, Object.assign(new Error(`standard input held more than ${max} bytes`), {code: 'TOO_LONG'}));
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
arm();
|
|
411
|
+
});
|
|
412
|
+
process.stdin.on('end', () => finish(resolve, text()));
|
|
413
|
+
process.stdin.on('close', () => finish(resolve, text()));
|
|
414
|
+
process.stdin.on('error', (e) => finish(reject, e));
|
|
415
|
+
}),
|
|
416
|
+
// Read when used: a directory removed under a shell makes process.cwd()
|
|
417
|
+
// throw, and only start needs it (null: git runs wherever it is).
|
|
418
|
+
get cwd() { try { return process.cwd(); } catch (err) { return null; } },
|
|
419
|
+
alive: require('../lib/run-liveness').processAlive,
|
|
420
|
+
hostname: os.hostname(),
|
|
421
|
+
exit: (code) => process.exit(code),
|
|
422
|
+
// a signal this process takes and does nothing with (ch-local daemon:
|
|
423
|
+
// SIGHUP, so it outlives the shim's parent ending)
|
|
424
|
+
ignoreSignal: (name) => process.on(name, () => {}),
|
|
425
|
+
// ch-local daemon's re-exec (C3b): the new daemon, with the table on its
|
|
426
|
+
// fd 3; and that fd's bytes, read by the daemon started so
|
|
427
|
+
spawnDaemon: (args, {handoff}) => require('../lib/handoff-io').spawnDaemon(args[2], args, {handoff, env: process.env}),
|
|
428
|
+
readHandoff: () => require('../lib/handoff-io').readHandoff(3),
|
|
429
|
+
// SIGINT, SIGTERM and SIGHUP (or the signals named) go to handler(name)
|
|
430
|
+
// until the returned function is called; start decides what each means.
|
|
431
|
+
trapSignals: (handler, signals = ['SIGINT', 'SIGTERM', 'SIGHUP']) => {
|
|
432
|
+
const on = Object.fromEntries(signals.map((s) => [s, () => handler(s)]));
|
|
433
|
+
for (const [s, fn] of Object.entries(on)) process.on(s, fn);
|
|
434
|
+
return () => { for (const [s, fn] of Object.entries(on)) process.removeListener(s, fn); };
|
|
435
|
+
},
|
|
436
|
+
// The runtime, on this process's own stdin, stdout and stderr: the client's
|
|
437
|
+
// MCP channel goes straight through. -> {kill(signal), exited: Promise<exit code>}.
|
|
438
|
+
// control: an IPC fd for the runtime's control channel (packs in place)
|
|
439
|
+
// detached: in its own process group (a session runtime: a terminal's
|
|
440
|
+
// hang-up never reaches it)
|
|
441
|
+
// output: a shared runtime's log (lib/runtime-log.js), which takes its
|
|
442
|
+
// stdout and stderr through pipes, always read
|
|
443
|
+
spawn: (cmd, args, {env, control = false, detached = false, output = null} = {}) => {
|
|
444
|
+
const out = output != null ? 'pipe' : 'inherit';
|
|
445
|
+
const child = require('child_process').spawn(cmd, args, {stdio: control ? ['inherit', out, out, 'ipc'] : 'inherit', env, detached, windowsHide: true});
|
|
446
|
+
if (output != null && control) { output.forward(child.stdout); output.forward(child.stderr); }
|
|
447
|
+
const exited = new Promise((resolve) => {
|
|
448
|
+
child.on('error', (err) => {
|
|
449
|
+
const line = `ch-local: could not start the runtime (${err.code || err.message})\n`;
|
|
450
|
+
process.stderr.write(line);
|
|
451
|
+
// (a shared runtime's: in its log too)
|
|
452
|
+
if (output != null) output.write(line);
|
|
453
|
+
resolve(1);
|
|
454
|
+
});
|
|
455
|
+
child.on('exit', (code, signal) => resolve(code != null ? code : (signal != null ? 1 : 0)));
|
|
456
|
+
});
|
|
457
|
+
return {kill: (signal) => { try { child.kill(signal); } catch (err) { /* already gone */ } }, exited, pid: child.pid,
|
|
458
|
+
...(control ? {send: (m) => child.send(m), onMessage: (fn) => child.on('message', fn)} : {})};
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function run() {
|
|
464
|
+
main(process.argv.slice(2), realIo()).then((code) => { process.exitCode = code; }, (err) => {
|
|
465
|
+
process.stderr.write(`ch-local: ${err && err.message ? err.message : err}\n`);
|
|
466
|
+
process.exitCode = 1;
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (require.main === module) run();
|
|
471
|
+
|
|
472
|
+
module.exports = {main, run, parseCommand, realIo};
|