opencode-rules-md 0.8.4 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.mjs +906 -0
- package/dist/src/cli/config.d.ts +104 -0
- package/dist/src/cli/config.d.ts.map +1 -0
- package/dist/src/cli/config.js +397 -0
- package/dist/src/cli/config.js.map +1 -0
- package/dist/src/cli/install.d.ts +50 -0
- package/dist/src/cli/install.d.ts.map +1 -0
- package/dist/src/cli/install.js +67 -0
- package/dist/src/cli/install.js.map +1 -0
- package/dist/src/cli/main.d.ts +19 -0
- package/dist/src/cli/main.d.ts.map +1 -0
- package/dist/src/cli/main.js +247 -0
- package/dist/src/cli/main.js.map +1 -0
- package/dist/src/cli/real-fs.d.ts +3 -0
- package/dist/src/cli/real-fs.d.ts.map +1 -0
- package/dist/src/cli/real-fs.js +32 -0
- package/dist/src/cli/real-fs.js.map +1 -0
- package/dist/src/cli/registry.d.ts +12 -0
- package/dist/src/cli/registry.d.ts.map +1 -0
- package/dist/src/cli/registry.js +34 -0
- package/dist/src/cli/registry.js.map +1 -0
- package/dist/src/cli/spawn.d.ts +29 -0
- package/dist/src/cli/spawn.d.ts.map +1 -0
- package/dist/src/cli/spawn.js +48 -0
- package/dist/src/cli/spawn.js.map +1 -0
- package/dist/src/cli/status.d.ts +51 -0
- package/dist/src/cli/status.d.ts.map +1 -0
- package/dist/src/cli/status.js +214 -0
- package/dist/src/cli/status.js.map +1 -0
- package/dist/src/cli/uninstall.d.ts +30 -0
- package/dist/src/cli/uninstall.d.ts.map +1 -0
- package/dist/src/cli/uninstall.js +128 -0
- package/dist/src/cli/uninstall.js.map +1 -0
- package/dist/src/cli/update.d.ts +65 -0
- package/dist/src/cli/update.d.ts.map +1 -0
- package/dist/src/cli/update.js +205 -0
- package/dist/src/cli/update.js.map +1 -0
- package/package.json +7 -2
- package/src/cli/config.ts +480 -0
- package/src/cli/install.ts +103 -0
- package/src/cli/main.ts +308 -0
- package/src/cli/real-fs.ts +44 -0
- package/src/cli/registry.ts +36 -0
- package/src/cli/spawn.ts +76 -0
- package/src/cli/status.ts +293 -0
- package/src/cli/uninstall.ts +177 -0
- package/src/cli/update.ts +254 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// src/cli/update.ts — `omd update` command implementation.
|
|
3
|
+
//
|
|
4
|
+
// Like install, update is now a thin wrapper around OpenCode's own CLI.
|
|
5
|
+
// We compare the installed version (read from `data['plugin']` — singular —
|
|
6
|
+
// with a backward-compat fallback to the legacy `data['plugins']`) against
|
|
7
|
+
// the latest npm version. When stale we purge the on-disk cache under
|
|
8
|
+
// ~/.cache/opencode/packages/ (the actual location OpenCode uses) and
|
|
9
|
+
// invoke `opencode plugin opencode-rules-md --global --force` to refresh
|
|
10
|
+
// the registration.
|
|
11
|
+
//
|
|
12
|
+
// The cache purge matters because OpenCode caches the resolved package
|
|
13
|
+
// by specifier — once a cached copy exists, `--force` re-registers without
|
|
14
|
+
// re-fetching unless we first remove the stale entry.
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
|
|
17
|
+
import { homedir } from 'os';
|
|
18
|
+
import { join } from 'path';
|
|
19
|
+
import { fetchLatestVersion, isStale } from './registry.js';
|
|
20
|
+
import {
|
|
21
|
+
loadGlobalConfig,
|
|
22
|
+
matchesPlugin,
|
|
23
|
+
readInstalledPlugins,
|
|
24
|
+
type CliFs,
|
|
25
|
+
type LoadedConfig,
|
|
26
|
+
} from './config.js';
|
|
27
|
+
import { spawnOpencodePlugin } from './spawn.js';
|
|
28
|
+
|
|
29
|
+
const CONFIG_BASENAMES = ['opencode', 'tui'] as const;
|
|
30
|
+
|
|
31
|
+
/** Package directory used by OpenCode to cache plugin installs. */
|
|
32
|
+
export const PACKAGES_DIR_BASENAME = ['.cache', 'opencode', 'packages'] as const;
|
|
33
|
+
|
|
34
|
+
/** Exact cache directory name we look for (bare specifier, no version suffix). */
|
|
35
|
+
export const CACHE_DIR_BASENAME = 'opencode-rules-md';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolve the user's home directory, honoring a custom HOME env var.
|
|
39
|
+
* Used by both resolveCachePaths and the config loader.
|
|
40
|
+
*/
|
|
41
|
+
export function resolveHome(env: NodeJS.ProcessEnv = process.env): string {
|
|
42
|
+
return env.HOME ?? homedir();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Return the absolute path of the OpenCode packages cache directory.
|
|
47
|
+
*/
|
|
48
|
+
export function resolvePackagesDir(env: NodeJS.ProcessEnv = process.env): string {
|
|
49
|
+
return join(resolveHome(env), ...PACKAGES_DIR_BASENAME);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Return the cache directories that match the opencode-rules-md prefix.
|
|
54
|
+
*
|
|
55
|
+
* Real-world layout under ~/.cache/opencode/packages/ looks like:
|
|
56
|
+
* opencode-rules-md/
|
|
57
|
+
* opencode-rules-md@latest/
|
|
58
|
+
* some-other-plugin/
|
|
59
|
+
*
|
|
60
|
+
* We glob-match anything starting with `opencode-rules-md` (exact or with
|
|
61
|
+
* an `@<version>` suffix). When an injected fs is provided we list the
|
|
62
|
+
* directory and filter; without one we return the two most common shapes
|
|
63
|
+
* so callers can pre-check existence cheaply.
|
|
64
|
+
*/
|
|
65
|
+
export function resolveCachePaths(
|
|
66
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
67
|
+
fs?: CliFs,
|
|
68
|
+
): string[] {
|
|
69
|
+
const packagesDir = resolvePackagesDir(env);
|
|
70
|
+
if (fs && fs.existsSync(packagesDir)) {
|
|
71
|
+
try {
|
|
72
|
+
const entries = fs.readdirSync(packagesDir);
|
|
73
|
+
return entries
|
|
74
|
+
.filter(
|
|
75
|
+
(name) => name === CACHE_DIR_BASENAME || name.startsWith(`${CACHE_DIR_BASENAME}@`),
|
|
76
|
+
)
|
|
77
|
+
.map((name) => join(packagesDir, name));
|
|
78
|
+
} catch {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// No fs injection — return the conventional candidates. Callers that care
|
|
83
|
+
// about existence should still check with existsSync().
|
|
84
|
+
return [
|
|
85
|
+
join(packagesDir, CACHE_DIR_BASENAME),
|
|
86
|
+
join(packagesDir, `${CACHE_DIR_BASENAME}@latest`),
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface UpdateOptions {
|
|
91
|
+
/** Injected latest version (test seam). Defaults to a real npm lookup. */
|
|
92
|
+
latestVersion?: string | null | undefined;
|
|
93
|
+
/** Print the plan without writing or spawning. */
|
|
94
|
+
dryRun?: boolean;
|
|
95
|
+
/** Injected spawn function (test seam). Defaults to spawnOpencodePlugin. */
|
|
96
|
+
spawn?: typeof spawnOpencodePlugin;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface UpdateResult {
|
|
100
|
+
status: 'stale' | 'current' | 'unreachable';
|
|
101
|
+
cachePaths: string[];
|
|
102
|
+
/** Re-install instruction; empty when status === 'current'. */
|
|
103
|
+
instruction: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Look up the installed specifier across both config files, returning the
|
|
108
|
+
* first one we find. Returns null when neither file registers the plugin.
|
|
109
|
+
*/
|
|
110
|
+
function findInstalledSpecifier(loaded: readonly LoadedConfig[]): string | null {
|
|
111
|
+
for (const cfg of loaded) {
|
|
112
|
+
const plugins = readInstalledPlugins(cfg);
|
|
113
|
+
const match = plugins.find((p) => matchesPlugin(p));
|
|
114
|
+
if (match) return match;
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Extract the version portion of an `opencode-rules-md@<version>` specifier.
|
|
121
|
+
* Returns null when the specifier has no `@version` segment.
|
|
122
|
+
*/
|
|
123
|
+
export function extractVersion(specifier: string): string | null {
|
|
124
|
+
const at = specifier.lastIndexOf('@');
|
|
125
|
+
if (at < 0 || at === specifier.length - 1) return null;
|
|
126
|
+
return specifier.slice(at + 1);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Recursively delete a directory and all its contents using the injected fs.
|
|
131
|
+
* Best-effort — a failed purge is not fatal; we want the update to keep going.
|
|
132
|
+
*/
|
|
133
|
+
export function purgeDirectory(fs: CliFs, dirPath: string): void {
|
|
134
|
+
let entries: string[] = [];
|
|
135
|
+
try {
|
|
136
|
+
entries = fs.readdirSync(dirPath);
|
|
137
|
+
} catch {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
for (const entry of entries) {
|
|
142
|
+
const entryPath = join(dirPath, entry);
|
|
143
|
+
try {
|
|
144
|
+
if (!fs.existsSync(entryPath)) continue;
|
|
145
|
+
// Probe to differentiate empty dirs / non-empty dirs / files. The injected
|
|
146
|
+
// fs interface does not expose stat, so we try readdir first and fall back
|
|
147
|
+
// to unlink for leaves.
|
|
148
|
+
try {
|
|
149
|
+
const subEntries = fs.readdirSync(entryPath);
|
|
150
|
+
if (subEntries.length === 0) {
|
|
151
|
+
fs.rmdirSync(entryPath);
|
|
152
|
+
} else {
|
|
153
|
+
purgeDirectory(fs, entryPath);
|
|
154
|
+
fs.rmdirSync(entryPath);
|
|
155
|
+
}
|
|
156
|
+
} catch {
|
|
157
|
+
// Not a directory (or unreadable) — best-effort unlink.
|
|
158
|
+
fs.unlinkSync(entryPath);
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
// best-effort per entry
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
fs.rmdirSync(dirPath);
|
|
167
|
+
} catch {
|
|
168
|
+
// best-effort
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Run the update command.
|
|
174
|
+
*
|
|
175
|
+
* 1. Resolve the latest published version from npm (or use the injected mock).
|
|
176
|
+
* 2. Read the installed version from the user's opencode config.
|
|
177
|
+
* 3. If unreachable, log and return.
|
|
178
|
+
* 4. If current, log and return.
|
|
179
|
+
* 5. Otherwise: purge stale cache directories, then spawn
|
|
180
|
+
* `opencode plugin opencode-rules-md --global --force`.
|
|
181
|
+
*/
|
|
182
|
+
export const runUpdate = async (
|
|
183
|
+
fs: CliFs,
|
|
184
|
+
env: NodeJS.ProcessEnv,
|
|
185
|
+
log: (s: string) => void,
|
|
186
|
+
_error: (s: string) => void,
|
|
187
|
+
opts: UpdateOptions = {},
|
|
188
|
+
): Promise<UpdateResult> => {
|
|
189
|
+
// 1. Resolve latest.
|
|
190
|
+
const latest = opts.latestVersion !== undefined ? opts.latestVersion : await fetchLatestVersion();
|
|
191
|
+
|
|
192
|
+
const cachePaths = resolveCachePaths(env, fs);
|
|
193
|
+
const instruction = 'opencode plugin opencode-rules-md --global --force';
|
|
194
|
+
const spawnFn = opts.spawn ?? spawnOpencodePlugin;
|
|
195
|
+
|
|
196
|
+
// 2. Unreachable.
|
|
197
|
+
if (latest === null) {
|
|
198
|
+
log('omd: could not determine latest version from npm registry');
|
|
199
|
+
return { status: 'unreachable', cachePaths, instruction };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// 3. Installed version (read both configs).
|
|
203
|
+
const configs = CONFIG_BASENAMES.map((basename) => loadGlobalConfig(fs, env, basename));
|
|
204
|
+
const installedSpecifier = findInstalledSpecifier(configs);
|
|
205
|
+
const installedVersion = installedSpecifier ? extractVersion(installedSpecifier) : null;
|
|
206
|
+
|
|
207
|
+
// 4. Current.
|
|
208
|
+
if (installedVersion !== null && !isStale(installedVersion, latest)) {
|
|
209
|
+
log(`omd: opencode-rules-md@${installedVersion} is already the latest`);
|
|
210
|
+
return { status: 'current', cachePaths, instruction: '' };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 5. Stale — describe plan.
|
|
214
|
+
if (opts.dryRun) {
|
|
215
|
+
log('omd: update check (dry-run)');
|
|
216
|
+
log(` latest version: ${latest}`);
|
|
217
|
+
log(` installed version: ${installedVersion ?? 'not installed'}`);
|
|
218
|
+
log(` would purge: ${cachePaths.join(', ') || '(none found)'}`);
|
|
219
|
+
log(` would instruct: ${instruction}`);
|
|
220
|
+
return { status: 'stale', cachePaths, instruction };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
log(
|
|
224
|
+
installedVersion === null
|
|
225
|
+
? `omd: opencode-rules-md is not installed; registering latest (${latest})`
|
|
226
|
+
: `omd: opencode-rules-md is stale (installed ${installedVersion}, latest ${latest})`,
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
// Purge each matching cache directory. Best-effort per path.
|
|
230
|
+
for (const cachePath of cachePaths) {
|
|
231
|
+
try {
|
|
232
|
+
if (fs.existsSync(cachePath)) {
|
|
233
|
+
purgeDirectory(fs, cachePath);
|
|
234
|
+
log(`omd: purged cache ${cachePath}`);
|
|
235
|
+
}
|
|
236
|
+
} catch {
|
|
237
|
+
// ignore individual purge failures
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Re-register via OpenCode's CLI. Failure here is fatal — surface it.
|
|
242
|
+
const result = await spawnFn(['opencode-rules-md', '--global', '--force'], {
|
|
243
|
+
env: process.env,
|
|
244
|
+
stdio: 'inherit',
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
if ((result.status ?? 0) !== 0) {
|
|
248
|
+
throw new Error(
|
|
249
|
+
`opencode plugin opencode-rules-md --global --force exited with status ${String(result.status)}`,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return { status: 'stale', cachePaths, instruction: '' };
|
|
254
|
+
};
|