@krtclcdy/tersio 2.4.1 → 2.5.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.
@@ -0,0 +1,171 @@
1
+ // cli/uninstall.ts — remove managed extensions, plugins, and binaries.
2
+ import { promises as fs } from 'node:fs';
3
+ import path from 'node:path';
4
+ import { cancel as clackCancel, confirm as clackConfirm } from '@clack/prompts';
5
+ import { BUN_BIN_DIR, OMP_AGENT_DIR, OMP_PLUGINS_DIR, PACKAGE_NAME, RTK_BINARY_NAME, dryRun, keepPonytail, removePonytail, removeRtk, yes, debug, writeConfigLines, } from './common.js';
6
+ import { ask, closeRL, tty } from './interactive.js';
7
+ import { readTextIfExists } from '../extensions/lib/utils.js';
8
+ // Read-modify-write a JSON file. mutate returns true when it changed
9
+ // something; no change (or unreadable file) means no output at all.
10
+ async function updateJsonFile(filePath, mutate, dryRunNote, writeNote, dryRun) {
11
+ const raw = await readTextIfExists(filePath);
12
+ if (!raw)
13
+ return;
14
+ let data;
15
+ try {
16
+ data = JSON.parse(raw);
17
+ }
18
+ catch {
19
+ debug(`Could not parse ${filePath}`);
20
+ return;
21
+ }
22
+ if (!mutate(data))
23
+ return;
24
+ if (dryRun) {
25
+ console.log(` [dry-run] ${dryRunNote}`);
26
+ return;
27
+ }
28
+ await fs.writeFile(filePath, JSON.stringify(data, null, 2) + '\n', 'utf8');
29
+ console.log(` [write] ${writeNote}`);
30
+ }
31
+ function dropKey(section, key) {
32
+ if (!section || typeof section !== 'object')
33
+ return false;
34
+ const record = section;
35
+ if (!(key in record))
36
+ return false;
37
+ delete record[key];
38
+ return true;
39
+ }
40
+ async function removeUninstallTarget(target, shouldDryRun, recursive = true) {
41
+ try {
42
+ if (shouldDryRun) {
43
+ console.log(` [dry-run] would remove ${target}`);
44
+ return;
45
+ }
46
+ if (recursive)
47
+ await fs.rm(target, { recursive: true, force: true });
48
+ else
49
+ await fs.unlink(target);
50
+ console.log(` [rm] ${target}`);
51
+ }
52
+ catch {
53
+ debug(`Could not remove ${target}`);
54
+ }
55
+ }
56
+ async function runUninstall(options = {}) {
57
+ const shouldDryRun = options.dryRun ?? dryRun;
58
+ const confirmed = (options.yes ?? yes) || shouldDryRun;
59
+ // Ponytail ships with Tersio's presets, so a full uninstall removes it too;
60
+ // --keep-ponytail opts out and reinstall always preserves it.
61
+ const shouldRemovePonytail = options.removePonytail ?? (removePonytail || !keepPonytail);
62
+ const shouldRemoveRtk = options.removeRtk ?? removeRtk;
63
+ console.log('\n=== Tersio Uninstall ===\n');
64
+ const extDir = path.join(OMP_AGENT_DIR, 'extensions');
65
+ const configPath = path.join(OMP_AGENT_DIR, 'config.yml');
66
+ const rtkBin = path.join(BUN_BIN_DIR, RTK_BINARY_NAME);
67
+ const pluginsDir = OMP_PLUGINS_DIR;
68
+ const ponytailPkgDir = path.join(pluginsDir, 'node_modules', '@dietrichgebert', 'ponytail');
69
+ const targets = [
70
+ 'caveman-session',
71
+ 'rtk-session',
72
+ 'ai-addons-updater',
73
+ 'combo-toggle',
74
+ 'shared',
75
+ // Only consumed by ai-addons-updater (removed above); otherwise orphaned.
76
+ 'lib',
77
+ // Legacy always-on combo helper; imports shared/session-state.js, so it
78
+ // breaks with a module-not-found warning once the shared dir is removed.
79
+ 'aaa-combo-boot',
80
+ ].map((dir) => path.join(extDir, dir));
81
+ console.log('Will remove:');
82
+ for (const t of targets) {
83
+ console.log(` ${t}`);
84
+ }
85
+ if (shouldRemovePonytail) {
86
+ console.log(` ${ponytailPkgDir} (ponytail plugin package)`);
87
+ }
88
+ if (shouldRemoveRtk) {
89
+ console.log(` ${rtkBin}`);
90
+ }
91
+ if (!confirmed) {
92
+ if (tty()) {
93
+ closeRL();
94
+ const confirmedChoice = await clackConfirm({ message: 'Remove the listed Tersio files?', initialValue: false });
95
+ if (typeof confirmedChoice !== 'boolean') {
96
+ clackCancel('Aborted.');
97
+ closeRL();
98
+ return false;
99
+ }
100
+ if (!confirmedChoice) {
101
+ console.log('Aborted.');
102
+ closeRL();
103
+ return false;
104
+ }
105
+ }
106
+ else {
107
+ const answer = await ask('\nProceed? [y/N]: ');
108
+ if (!answer.toLowerCase().startsWith('y')) {
109
+ console.log('Aborted.');
110
+ closeRL();
111
+ return false;
112
+ }
113
+ }
114
+ }
115
+ // Remove extension directories (independent paths, so concurrently).
116
+ await Promise.all(targets.map((t) => removeUninstallTarget(t, shouldDryRun)));
117
+ // Remove Combo and mode-reinforcement registrations; Ponytail only when requested.
118
+ const configRaw = await readTextIfExists(configPath);
119
+ if (configRaw) {
120
+ let lines = configRaw.split('\n');
121
+ const before = lines.length;
122
+ lines = lines.filter((l) => {
123
+ if (l.includes('combo-toggle') || l.includes('mode-reinforcement'))
124
+ return false;
125
+ if (shouldRemovePonytail && l.includes('ponytail') && l.includes('pi-extension'))
126
+ return false;
127
+ return true;
128
+ });
129
+ if (lines.length !== before) {
130
+ if (shouldDryRun)
131
+ console.log(` [dry-run] would remove ${before - lines.length} config.yml entries`);
132
+ else
133
+ await writeConfigLines(configPath, lines, ` [write] Updated config.yml (removed ${before - lines.length} entries)`);
134
+ }
135
+ }
136
+ // Remove the Ponytail plugin package the installer added (dep, files,
137
+ // lock entry); the config.yml entry was filtered above.
138
+ if (shouldRemovePonytail) {
139
+ const pluginsPkgPath = path.join(pluginsDir, 'package.json');
140
+ await updateJsonFile(pluginsPkgPath, (data) => dropKey(data.dependencies, '@dietrichgebert/ponytail'), `would remove @dietrichgebert/ponytail from ${pluginsPkgPath}`, 'Removed @dietrichgebert/ponytail from plugins/package.json', shouldDryRun);
141
+ try {
142
+ if (shouldDryRun)
143
+ console.log(` [dry-run] would remove ${ponytailPkgDir}`);
144
+ else {
145
+ await fs.rm(ponytailPkgDir, { recursive: true, force: true });
146
+ console.log(` [rm] ${ponytailPkgDir}`);
147
+ await fs.rm(path.dirname(ponytailPkgDir));
148
+ console.log(` [rm] ${path.dirname(ponytailPkgDir)} (empty scope)`);
149
+ }
150
+ }
151
+ catch {
152
+ debug('Could not remove ponytail package dir (scope may hold other packages)');
153
+ }
154
+ const lockPath = path.join(pluginsDir, 'omp-plugins.lock.json');
155
+ await updateJsonFile(lockPath, (data) => dropKey(data.plugins, '@dietrichgebert/ponytail'), `would remove @dietrichgebert/ponytail from ${lockPath}`, `Removed @dietrichgebert/ponytail from ${lockPath}`, shouldDryRun);
156
+ }
157
+ // Remove our plugin registration from ~/.omp/plugins
158
+ const pluginsPkgPath = path.join(pluginsDir, 'package.json');
159
+ await updateJsonFile(pluginsPkgPath, (data) => dropKey(data.dependencies, PACKAGE_NAME), `would remove ${PACKAGE_NAME} from ${pluginsPkgPath}`, `Removed ${PACKAGE_NAME} from plugins/package.json`, shouldDryRun);
160
+ const selfPluginDir = path.join(pluginsDir, 'node_modules', PACKAGE_NAME);
161
+ if ((await readTextIfExists(path.join(selfPluginDir, 'package.json'))) !== null) {
162
+ await removeUninstallTarget(selfPluginDir, shouldDryRun);
163
+ }
164
+ // Remove RTK binary if requested
165
+ if (shouldRemoveRtk)
166
+ await removeUninstallTarget(rtkBin, shouldDryRun, false);
167
+ console.log('\nDone. Restart OMP for changes to take effect.');
168
+ return true;
169
+ }
170
+ const SCOPE_MAP = { user: '1', project: '2', both: '3' };
171
+ export { runUninstall };
package/cli/update.js ADDED
@@ -0,0 +1,202 @@
1
+ // cli/update.ts — update check, per-add-on plan, latest-installer run.
2
+ import { promises as fs } from 'node:fs';
3
+ import path from 'node:path';
4
+ import { spawn } from 'node:child_process';
5
+ import { BUN_BIN_DIR, IS_WINDOWS, OMP_AGENT_DIR, OMP_PLUGINS_DIR, PACKAGE_BIN, PACKAGE_NAME, PACKAGE_VERSION, RTK_BINARY_NAME, SCOPE_MAP, dryRun, execP, parseJsonObject, scopeFlag, verbose, } from './common.js';
6
+ import { execNetwork } from './interactive.js';
7
+ import { readTextIfExists } from '../extensions/lib/utils.js';
8
+ import { CAVEMAN_REMOTE_RULE, RTK_RELEASE_API, fetchJson, httpsGet, normalizeRtkVersion, sha256Hex, } from '../extensions/lib/utils.js';
9
+ const UPDATE_CHECK_TTL_MS = 6 * 60 * 60 * 1000;
10
+ function newerThan(a, b) {
11
+ const pa = a.replace(/^v/, '').split('.').map((n) => parseInt(n, 10) || 0);
12
+ const pb = b.replace(/^v/, '').split('.').map((n) => parseInt(n, 10) || 0);
13
+ for (let i = 0; i < 3; i++) {
14
+ if ((pa[i] || 0) !== (pb[i] || 0))
15
+ return (pa[i] || 0) > (pb[i] || 0);
16
+ }
17
+ return false;
18
+ }
19
+ async function latestPublishedVersion() {
20
+ try {
21
+ const args = IS_WINDOWS
22
+ ? ['/d', '/s', '/c', 'npm', 'view', PACKAGE_NAME, 'version', '--prefer-online']
23
+ : ['view', PACKAGE_NAME, 'version', '--prefer-online'];
24
+ const r = await execNetwork('Checking npm registry for tersio updates', IS_WINDOWS ? process.env.ComSpec || 'cmd.exe' : 'npm', args, {
25
+ timeout: 4000,
26
+ maxBuffer: 1024 * 1024,
27
+ windowsHide: true,
28
+ shell: false,
29
+ });
30
+ return r.stdout.trim() || null;
31
+ }
32
+ catch {
33
+ return null;
34
+ }
35
+ }
36
+ // Returns a newer published version, or null. Reads the registry at most once
37
+ // per TTL (cached under OMP_PLUGINS_DIR) and stays silent on failure.
38
+ async function checkForUpdate() {
39
+ const cachePath = path.join(OMP_PLUGINS_DIR, 'tersio-update-check.json');
40
+ const cached = parseJsonObject(await readTextIfExists(cachePath));
41
+ const cacheFresh = typeof cached?.lastCheck === 'number' && Date.now() - cached.lastCheck < UPDATE_CHECK_TTL_MS;
42
+ if (cacheFresh && cached?.latest)
43
+ return newerThan(cached.latest, PACKAGE_VERSION) ? cached.latest : null;
44
+ const latest = await latestPublishedVersion();
45
+ if (latest) {
46
+ await fs.mkdir(OMP_PLUGINS_DIR, { recursive: true }).catch(() => { });
47
+ await fs.writeFile(cachePath, JSON.stringify({ latest, lastCheck: Date.now() }) + '\n', 'utf8').catch(() => { });
48
+ return newerThan(latest, PACKAGE_VERSION) ? latest : null;
49
+ }
50
+ // Registry unreachable: fall back to a stale cache rather than staying silent.
51
+ return cached?.latest && newerThan(cached.latest, PACKAGE_VERSION) ? cached.latest : null;
52
+ }
53
+ // Race a probe against a timeout; slow or failing probes resolve null so the
54
+ // update plan stays best-effort and never blocks the update itself.
55
+ async function settle(work, ms) {
56
+ let timer;
57
+ try {
58
+ return await Promise.race([
59
+ work.then((value) => value, () => null),
60
+ new Promise((resolve) => { timer = setTimeout(() => resolve(null), ms); }),
61
+ ]);
62
+ }
63
+ finally {
64
+ clearTimeout(timer);
65
+ }
66
+ }
67
+ // Plain `npm view <spec> version` with no spinner, for background probes.
68
+ async function npmViewVersion(spec, timeoutMs) {
69
+ try {
70
+ const exe = IS_WINDOWS ? process.env.ComSpec || 'cmd.exe' : 'npm';
71
+ const args = IS_WINDOWS
72
+ ? ['/d', '/s', '/c', 'npm', 'view', spec, 'version', '--prefer-online']
73
+ : ['view', spec, 'version', '--prefer-online'];
74
+ const r = await execP(exe, args, { timeout: timeoutMs, maxBuffer: 1024 * 1024, windowsHide: true, shell: false });
75
+ return r.stdout.trim() || null;
76
+ }
77
+ catch {
78
+ return null;
79
+ }
80
+ }
81
+ // Run a child with inherited stdio for live output. Used when the child
82
+ // renders its own interactive UI (the delegated installer runs Clack
83
+ // spinners), which a concurrent outer spinner would corrupt into stray bars.
84
+ async function execInherit(cmd, args) {
85
+ const code = await new Promise((resolve, reject) => {
86
+ const child = spawn(cmd, args, { stdio: 'inherit' });
87
+ child.on('error', reject);
88
+ child.on('close', resolve);
89
+ });
90
+ if (code !== 0)
91
+ throw new Error(`${cmd} exited with code ${code}`);
92
+ }
93
+ // Current → latest per add-on. Every probe is capped and nullable; the plan
94
+ // prints `unknown` for anything unreachable and the update proceeds anyway.
95
+ async function probeUpdatePlan(cliLatest) {
96
+ const rtkBin = path.join(BUN_BIN_DIR, RTK_BINARY_NAME);
97
+ const ponytailPkg = path.join(OMP_PLUGINS_DIR, 'node_modules', '@dietrichgebert', 'ponytail', 'package.json');
98
+ const ruleDest = path.join(OMP_AGENT_DIR, 'extensions', 'caveman-session', 'rule.md');
99
+ const [rtkLocal, rtkRelease, ruleLocalText, ruleRemoteText, ponytailLocalText, ponytailLatest] = await Promise.all([
100
+ settle(execP(rtkBin, ['--version'], { timeout: 5000 }).then((r) => normalizeRtkVersion(r.stdout.trim() || r.stderr.trim()) || null), 6000),
101
+ settle(fetchJson(RTK_RELEASE_API).then((r) => normalizeRtkVersion(r.tag_name) || null), 4000),
102
+ readTextIfExists(ruleDest),
103
+ settle(httpsGet(CAVEMAN_REMOTE_RULE), 4000),
104
+ readTextIfExists(ponytailPkg),
105
+ settle(npmViewVersion('@dietrichgebert/ponytail', 4000), 5000),
106
+ ]);
107
+ const shortHash = (text) => text === null ? null : sha256Hex(text).slice(0, 8);
108
+ return {
109
+ cli: cliLatest,
110
+ rtk: [rtkLocal, rtkRelease],
111
+ rule: [shortHash(ruleLocalText), shortHash(ruleRemoteText)],
112
+ ponytail: [ponytailLocalText ? parseJsonObject(ponytailLocalText)?.version ?? null : null, ponytailLatest],
113
+ };
114
+ }
115
+ function planLine(name, current, latest) {
116
+ if (!current && !latest)
117
+ console.log(` ${name}: unknown`);
118
+ else if (!latest)
119
+ console.log(` ${name}: ${current} (latest unknown)`);
120
+ else if (current === latest)
121
+ console.log(` ${name}: ${current} (up to date)`);
122
+ else
123
+ console.log(` ${name}: ${current ?? 'missing'} → ${latest}`);
124
+ }
125
+ async function runLatestUpdate() {
126
+ const updateScope = scopeFlag || 'user';
127
+ if (!Object.hasOwn(SCOPE_MAP, updateScope)) {
128
+ console.error(`[fail] Invalid --scope: ${updateScope}. Use: user, project, both`);
129
+ process.exitCode = 1;
130
+ return;
131
+ }
132
+ const forwardedArgs = ['--yes', '--scope', updateScope];
133
+ if (dryRun)
134
+ forwardedArgs.push('--dry-run');
135
+ if (verbose)
136
+ forwardedArgs.push('--verbose');
137
+ const npmCommand = IS_WINDOWS ? process.env.ComSpec || 'cmd.exe' : 'npm';
138
+ console.log('=== Updating Tersio ===');
139
+ // Resolve the target explicitly: `npm view --prefer-online` beats the local
140
+ // metadata cache, so a stale `@latest` can never pin an older release.
141
+ const cliLatest = await latestPublishedVersion();
142
+ const target = cliLatest && /^\d+\.\d+\.\d+$/.test(cliLatest) ? `@${cliLatest}` : '@latest';
143
+ // Current → latest per add-on. Best-effort; unreachable probes print as
144
+ // unknown and never block the update.
145
+ const plan = await probeUpdatePlan(cliLatest);
146
+ planLine('Tersio', PACKAGE_VERSION, plan.cli);
147
+ planLine('RTK', plan.rtk[0], plan.rtk[1]);
148
+ planLine('Caveman rule', plan.rule[0], plan.rule[1]);
149
+ planLine('Ponytail', plan.ponytail[0], plan.ponytail[1]);
150
+ console.log('');
151
+ // The npx delegation below only refreshes the OMP-side files; the globally
152
+ // installed CLI keeps its old version until npm -g runs. Refresh both.
153
+ const globalArgs = IS_WINDOWS
154
+ ? ['/d', '/s', '/c', 'npm', 'install', '-g', `${PACKAGE_NAME}${target}`, '--no-audit', '--no-fund', '--prefer-online']
155
+ : ['install', '-g', `${PACKAGE_NAME}${target}`, '--no-audit', '--no-fund', '--prefer-online'];
156
+ if (dryRun) {
157
+ console.log(` [dry-run] would run: npm install -g ${PACKAGE_NAME}${target} --no-audit --no-fund --prefer-online`);
158
+ }
159
+ else {
160
+ try {
161
+ const globalResult = await execNetwork(`Updating global Tersio CLI to ${PACKAGE_NAME}${target}`, npmCommand, globalArgs, {
162
+ timeout: 300000,
163
+ maxBuffer: 10 * 1024 * 1024,
164
+ windowsHide: true,
165
+ shell: false,
166
+ });
167
+ if (globalResult.stdout)
168
+ process.stdout.write(globalResult.stdout);
169
+ if (globalResult.stderr)
170
+ process.stderr.write(globalResult.stderr);
171
+ console.log(` [ok] CLI updated to ${PACKAGE_NAME}${target}`);
172
+ }
173
+ catch (e) {
174
+ console.log(` [warn] Global CLI update skipped: ${e.message}`);
175
+ console.log(` [hint] Manual: npm install -g ${PACKAGE_NAME}${target} --no-audit --no-fund --prefer-online`);
176
+ }
177
+ }
178
+ const npmArgs = [
179
+ 'exec',
180
+ '--yes',
181
+ '--prefer-online',
182
+ `--package=${PACKAGE_NAME}${target}`,
183
+ '--',
184
+ PACKAGE_BIN,
185
+ '--apply-update',
186
+ ...forwardedArgs,
187
+ ];
188
+ const npmCommandArgs = IS_WINDOWS ? ['/d', '/s', '/c', 'npm', ...npmArgs] : npmArgs;
189
+ // Inherited stdio, no outer spinner: the delegated installer renders its
190
+ // own Clack UI live, which capture-then-dump would corrupt into stray bars.
191
+ console.log(` Running ${PACKAGE_NAME}${target} installer...`);
192
+ try {
193
+ await execInherit(npmCommand, npmCommandArgs);
194
+ console.log('\n=== Update complete ===');
195
+ }
196
+ catch (e) {
197
+ const err = e;
198
+ console.error(`\n[fail] Could not run ${PACKAGE_NAME}${target}: ${err.message}`);
199
+ process.exitCode = 1;
200
+ }
201
+ }
202
+ export { checkForUpdate, runLatestUpdate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krtclcdy/tersio",
3
- "version": "2.4.1",
3
+ "version": "2.5.0",
4
4
  "description": "✂️ Token-saving OMP add-ons: terse Caveman replies, compact RTK shell output, lean Ponytail code calls — one-command combo presets.",
5
5
  "keywords": [
6
6
  "omp",
@@ -107,10 +107,11 @@
107
107
  },
108
108
  "files": [
109
109
  "tersio.js",
110
+ "cli/**/*.js",
110
111
  "extensions/**/*.js"
111
112
  ],
112
113
  "scripts": {
113
- "clean": "rm -f tersio.js extensions/**/*.js",
114
+ "clean": "rm -f tersio.js cli/*.js extensions/**/*.js",
114
115
  "build": "tsc",
115
116
  "prepublishOnly": "npm run build",
116
117
  "check": "tsc --noEmit",