@kombai/cli 0.1.5 → 0.2.0-beta.2

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,73 @@
1
+ // The Kombai CLI's launcher: `node "<this file>" <command>` runs the version current.json names, from the managed
2
+ // folder this file sits in (~/.kombai/cli/install/<channel>/). Written by the CLI (src/managed), which copies the
3
+ // current version's own launcher over this file whenever it differs. Node built-ins only, and no syntax newer than
4
+ // Node 14, so a Node too old for the CLI still gets the error below and not a parse failure.
5
+ import { readFileSync, statSync } from 'node:fs';
6
+ import { dirname, join } from 'node:path';
7
+ import { fileURLToPath, pathToFileURL } from 'node:url';
8
+
9
+ // The version that wrote this file: a published one, so its npx line always works. Filled in by the build.
10
+ const WRITTEN_BY = '0.2.0-beta.2';
11
+ const root = dirname(fileURLToPath(import.meta.url));
12
+
13
+ // On stderr, as the CLI's own start-up errors are: under `kombai mcp` stdout carries MCP frames only.
14
+ function fail(code, message, next) {
15
+ const error = next ? { code, message, next } : { code, message };
16
+ process.stderr.write(JSON.stringify({ error }) + '\n');
17
+ process.exit(1);
18
+ }
19
+
20
+ let current = {};
21
+ try {
22
+ const read = JSON.parse(readFileSync(join(root, 'current.json'), 'utf8'));
23
+ if (read && typeof read === 'object') current = read;
24
+ } catch (error) {
25
+ // No readable record: no managed copy.
26
+ }
27
+ const VERSION = /^\d{1,6}\.\d{1,6}\.\d{1,6}(-beta\.\d{1,6})?$/;
28
+ const bootstrap = VERSION.test(current.bootstrap) ? current.bootstrap : WRITTEN_BY;
29
+ const notInstalled = () =>
30
+ fail(
31
+ 'CLI_NOT_INSTALLED',
32
+ 'The Kombai CLI under ' + root + ' is missing or does not load. Run the next command, then go on from what it prints.',
33
+ 'npx -y @kombai/cli@' + bootstrap + ' skill',
34
+ );
35
+
36
+ // A version folder is complete when every file its dist/files.json lists is there (src/managed/folder.js isComplete):
37
+ // a copy that lost one would load and then fail at the command that needs it, with nothing to say how to recover.
38
+ function complete(dist) {
39
+ let files;
40
+ try {
41
+ files = JSON.parse(readFileSync(join(dist, 'files.json'), 'utf8')).files;
42
+ } catch (error) {
43
+ return false;
44
+ }
45
+ if (!Array.isArray(files) || files.length === 0 || files.indexOf('kombai.mjs') === -1) return false;
46
+ for (const file of files) {
47
+ if (typeof file !== 'string' || file === '' || file.split(/[\\/]/).indexOf('..') !== -1 || /^([A-Za-z]:)?[\\/]/.test(file)) {
48
+ return false;
49
+ }
50
+ // A regular file, as the installer checks it (src/managed/folder.js isComplete): a folder of that name is not one.
51
+ let regular = false;
52
+ try {
53
+ regular = statSync(join(dist, file)).isFile();
54
+ } catch (error) {
55
+ regular = false;
56
+ }
57
+ if (!regular) return false;
58
+ }
59
+ return true;
60
+ }
61
+
62
+ if (Number(process.versions.node.split('.')[0]) < 20) {
63
+ fail('CLI_NODE_TOO_OLD', 'kombai needs Node 20 or later; this is ' + process.version + '.');
64
+ } else if (!VERSION.test(current.version) || !complete(join(root, 'versions', current.version, 'dist'))) {
65
+ notInstalled();
66
+ } else {
67
+ // A build sets this as its first statement: unset after a failed import, the build never started.
68
+ globalThis.__kombaiCliStarted = false;
69
+ import(pathToFileURL(join(root, 'versions', current.version, 'dist', 'kombai.mjs')).href).catch((error) => {
70
+ if (globalThis.__kombaiCliStarted === true) throw error;
71
+ notInstalled();
72
+ });
73
+ }