@wheatley-ai/cli 0.2.0 → 0.3.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.
@@ -3,14 +3,14 @@ import path from "node:path";
3
3
  import { Type } from "typebox";
4
4
  import { Value } from "typebox/value";
5
5
  import { describeFailure } from "./command-runner.js";
6
- import { isDocker } from "./paths.js";
7
- /** The npm package name of the daemon and CLI. */
8
- export const PACKAGE_NAME = "@wheatley-ai/cli";
6
+ import { fetchPublishedVersions, PACKAGE_NAME } from "./registry.js";
9
7
  /** The commands that update a Docker Compose Deployment. Run them in the folder of `docker-compose.yml`. */
10
8
  export const DOCKER_UPDATE_COMMANDS = [
11
9
  "docker compose pull",
12
10
  "docker compose up -d",
13
11
  ];
12
+ /** The `curl` installer. Run again with `--version`, it replaces the app in its XDG folder. */
13
+ export const INSTALLER_URL = "https://raw.githubusercontent.com/SphericalKat/wheatley/main/scripts/install.sh";
14
14
  const VERSION = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?<prerelease>-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/u;
15
15
  const parseVersion = (text) => {
16
16
  const groups = VERSION.exec(text)?.groups;
@@ -50,88 +50,97 @@ export const selectUpdate = (current, available) => {
50
50
  target: newest(releases.filter((version) => version.major === installed.major))?.text,
51
51
  };
52
52
  };
53
- const segment = (...names) => `${path.sep}${names.join(path.sep)}${path.sep}`;
53
+ const command = (verify, ...parts) => ({ command: parts, kind: "command", verify });
54
+ const notice = (...lines) => ({
55
+ kind: "notice",
56
+ lines,
57
+ });
54
58
  /**
55
- * Detects the install method from the environment and the real path of the installed package folder.
59
+ * Returns what updates `installation` to `version`.
56
60
  *
57
- * pnpm keeps global packages in a `pnpm/global/` folder and its store in `.pnpm/`. Any other `node_modules/` folder is
58
- * an npm install. A folder outside `node_modules/` is a source checkout.
61
+ * Errors: throws for a Docker install or a source checkout, which `planUpdate` handles before the registry query.
59
62
  */
60
- export const detectInstallMethod = (env, packageDir) => {
61
- if (isDocker(env)) {
62
- return "docker";
63
+ export const upgradeAction = (installation, version) => {
64
+ const spec = `${PACKAGE_NAME}@${version}`;
65
+ switch (installation.method) {
66
+ case "npm": {
67
+ const prefix = installation.prefix === undefined
68
+ ? []
69
+ : ["--prefix", installation.prefix];
70
+ return command(true, ...installation.npm, "install", "--global", ...prefix, spec);
71
+ }
72
+ case "pnpm": {
73
+ return command(false, "pnpm", "add", "--global", spec);
74
+ }
75
+ case "yarn": {
76
+ return command(true, "yarn", "global", "add", spec);
77
+ }
78
+ case "bun": {
79
+ return command(true, "bun", "install", "--global", spec);
80
+ }
81
+ case "brew": {
82
+ return command(false, "brew", "upgrade", installation.formula);
83
+ }
84
+ case "curl": {
85
+ // The script is downloaded before it runs, so that a failed download fails the command. A pipe from `curl` to
86
+ // `sh` would exit 0. The URL and version are arguments, not shell text.
87
+ return command(false, "sh", "-c", 'script="$(curl -fsSL "$0")" && printf "%s\\n" "$script" | sh -s -- --version "$1" --no-modify-path', INSTALLER_URL, version);
88
+ }
89
+ case "volta": {
90
+ return notice("Volta manages this Wheatley. To update it, run:", ` volta install ${spec}`);
91
+ }
92
+ case "mise": {
93
+ return notice("mise manages this Wheatley. To update it, run:", ` mise use -g npm:${spec}`);
94
+ }
95
+ case "aur": {
96
+ return notice("This Wheatley is the AUR package wheatley-cli. Update it with your AUR helper, for example:", " paru -Syu", " yay -Syu");
97
+ }
98
+ case "docker":
99
+ case "source": {
100
+ throw new Error(`wheatley update cannot update a ${installation.method} install`);
101
+ }
102
+ default: {
103
+ const unknown = installation;
104
+ throw new Error(`Unknown installation: ${JSON.stringify(unknown)}`);
105
+ }
63
106
  }
64
- const dir = `${packageDir}${path.sep}`;
65
- if (dir.includes(segment("pnpm", "global")) ||
66
- dir.includes(segment(".pnpm"))) {
67
- return "pnpm";
68
- }
69
- return dir.includes(segment("node_modules")) ? "npm" : "source";
70
107
  };
71
- /** The global install command that updates Wheatley to `version`. */
72
- export const installCommand = (manager, version) => manager === "pnpm"
73
- ? ["pnpm", "add", "--global", `${PACKAGE_NAME}@${version}`]
74
- : ["npm", "install", "--global", `${PACKAGE_NAME}@${version}`];
75
- /** `npm view <package> versions --json` prints a string for one version and an array for more. */
76
- const VersionListSchema = Type.Union([
77
- Type.String(),
78
- Type.Array(Type.String()),
79
- ]);
80
108
  const PackageJsonSchema = Type.Object({ version: Type.String() });
81
- /** Lists the published versions of the package with `npm view`, which works for npm and pnpm installs. */
82
- const publishedVersions = async (runner) => {
83
- const args = ["view", PACKAGE_NAME, "versions", "--json"];
84
- const result = await runner.run("npm", args);
85
- if (result.exitCode !== 0) {
86
- throw new Error(describeFailure("npm", args, result));
87
- }
88
- const parsed = JSON.parse(result.stdout);
89
- if (!Value.Check(VersionListSchema, parsed)) {
90
- throw new Error("npm view returned an unexpected version list");
91
- }
92
- return [parsed].flat();
93
- };
94
109
  /**
95
110
  * Decides what `wheatley update` does. It never installs anything.
96
111
  *
97
112
  * - Docker: returns the Docker Compose commands, without a registry query.
98
- * - npm or pnpm: queries the registry and applies the major-version guard (`selectUpdate`).
113
+ * - Any other install: queries the registry, applies the major-version guard (`selectUpdate`), and returns the
114
+ * `upgradeAction` of the target version.
99
115
  *
100
- * Errors: throws for a source checkout without a `manager` override, if `npm view` fails, or for an invalid current
101
- * version.
116
+ * Errors: throws for a source checkout, if the registry query fails, or for an invalid current version.
102
117
  */
103
118
  export const planUpdate = async (input) => {
104
- if (input.method === "docker") {
119
+ const { installation } = input;
120
+ if (installation.method === "docker") {
105
121
  return { commands: DOCKER_UPDATE_COMMANDS, kind: "docker" };
106
122
  }
107
- const manager = input.manager ?? (input.method === "source" ? undefined : input.method);
108
- if (manager === undefined) {
109
- throw new Error("This Wheatley runs from a source checkout. Update it with git, or pass --manager npm or --manager pnpm.");
123
+ if (installation.method === "source") {
124
+ throw new Error("This Wheatley runs from a source checkout. Update it with git, or pass --method npm|pnpm|yarn|bun|brew|curl.");
110
125
  }
111
- const { newerMajor, target } = selectUpdate(input.current, await publishedVersions(input.runner));
126
+ const { newerMajor, target } = selectUpdate(input.current, await fetchPublishedVersions(input));
127
+ const major = newerMajor === undefined
128
+ ? undefined
129
+ : {
130
+ action: upgradeAction(installation, newerMajor),
131
+ version: newerMajor,
132
+ };
112
133
  if (target === undefined) {
113
- return { current: input.current, kind: "up_to_date", newerMajor };
134
+ return { current: input.current, kind: "up_to_date", newerMajor: major };
114
135
  }
115
136
  return {
116
- command: installCommand(manager, target),
137
+ action: upgradeAction(installation, target),
117
138
  current: input.current,
118
139
  kind: "update",
119
- newerMajor,
140
+ newerMajor: major,
120
141
  target,
121
142
  };
122
143
  };
123
- /**
124
- * Runs the install command of an `update` plan with the standard streams attached.
125
- *
126
- * Errors: rejects if the package manager cannot start or exits with a non-zero code.
127
- */
128
- export const applyUpdate = async (plan, runner) => {
129
- const [command, ...args] = plan.command;
130
- const result = await runner.run(command, args, { inheritStdio: true });
131
- if (result.exitCode !== 0) {
132
- throw new Error(describeFailure(command, args, result));
133
- }
134
- };
135
144
  /** The folder of this package, which holds `package.json`, `bin/`, and `dist/`. */
136
145
  export const packageDir = () => path.resolve(import.meta.dirname, "..", "..");
137
146
  /** Reads the version of the installed package from its `package.json`. */
@@ -143,3 +152,28 @@ export const readPackageVersion = async (dir = packageDir()) => {
143
152
  }
144
153
  return parsed.version;
145
154
  };
155
+ /**
156
+ * Runs an upgrade command with the standard streams attached. If `action.verify` is `true`, it then reads
157
+ * `package.json` in `packageDir` again, because a package manager can exit with code 0 but update another copy.
158
+ *
159
+ * Errors: rejects if the command cannot start, exits with a non-zero code, or leaves the running copy on another
160
+ * version than `target`.
161
+ */
162
+ export const applyUpgrade = async (action, options) => {
163
+ const [program, ...args] = action.command;
164
+ // Corepack shims of pnpm and yarn refuse to run inside a project whose `packageManager` names another manager.
165
+ const result = await options.runner.run(program, args, {
166
+ cwd: options.workDir,
167
+ inheritStdio: true,
168
+ });
169
+ if (result.exitCode !== 0) {
170
+ throw new Error(describeFailure(program, args, result));
171
+ }
172
+ if (!action.verify) {
173
+ return;
174
+ }
175
+ const installed = await readPackageVersion(options.packageDir);
176
+ if (installed !== options.target) {
177
+ throw new Error(`${action.command.join(" ")} finished, but ${options.packageDir} still has Wheatley ${installed}, not ${options.target}. Another copy of Wheatley can be first on PATH, or pass --method to name the package manager that owns this copy.`);
178
+ }
179
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wheatley-ai/cli",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Wheatley daemon and CLI",
5
5
  "license": "ISC",
6
6
  "repository": {
@@ -34,7 +34,7 @@
34
34
  "import-meta-resolve": "4.2.0",
35
35
  "ink": "8.0.0",
36
36
  "react": "19.3.1",
37
- "string-width": "8.3.0",
37
+ "string-width": "9.0.0",
38
38
  "typebox": "1.4.7",
39
39
  "wrap-ansi": "10.0.2",
40
40
  "ws": "8.23.0",
@@ -42,11 +42,11 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@modelcontextprotocol/server": "2.4.0",
45
- "@types/react": "19.3.1",
45
+ "@types/react": "19.3.3",
46
46
  "@types/ws": "8.18.2",
47
47
  "@wheatley-ai/dashboard": "0.0.0",
48
48
  "@wheatley-ai/protocol": "0.0.0",
49
- "@wheatley-ai/sdk": "0.2.0",
49
+ "@wheatley-ai/sdk": "0.3.0",
50
50
  "ink-testing-library": "4.0.0"
51
51
  },
52
52
  "engines": {