@spenceriam/impulse 1.8.0 → 1.8.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.
Files changed (2) hide show
  1. package/bin/impulse +182 -2
  2. package/package.json +7 -7
package/bin/impulse CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { spawn } from "child_process";
4
- import { existsSync } from "fs";
3
+ import { spawn, spawnSync } from "child_process";
4
+ import { existsSync, readFileSync } from "fs";
5
5
  import { join, dirname } from "path";
6
6
  import { fileURLToPath } from "url";
7
7
  import { createRequire } from "module";
@@ -10,6 +10,9 @@ import os from "os";
10
10
 
11
11
  const __dirname = dirname(fileURLToPath(import.meta.url));
12
12
  const require = createRequire(import.meta.url);
13
+ const PACKAGE_NAME = "@spenceriam/impulse";
14
+ const INTERNAL_AUTO_UPDATE_ENV = "IMPULSE_INTERNAL_AUTO_UPDATE";
15
+ const UPDATE_PARENT_PID_ENV = "IMPULSE_UPDATE_PARENT_PID";
13
16
 
14
17
  function detectPlatformAndArch() {
15
18
  let platform;
@@ -74,6 +77,183 @@ function findBinary() {
74
77
  }
75
78
  }
76
79
 
80
+ function npmCommand() {
81
+ return os.platform() === "win32" ? "npm.cmd" : "npm";
82
+ }
83
+
84
+ function useShellForCommandShims() {
85
+ return os.platform() === "win32";
86
+ }
87
+
88
+ function currentWrapperVersion() {
89
+ const packageJson = JSON.parse(readFileSync(join(__dirname, "..", "package.json"), "utf8"));
90
+ return packageJson.version;
91
+ }
92
+
93
+ function currentInstalledVersion() {
94
+ try {
95
+ const binaryPath = findBinary();
96
+ const result = spawnSync(binaryPath, ["--version"], {
97
+ encoding: "utf8",
98
+ });
99
+ const version = result.stdout?.trim().match(/(\d+\.\d+\.\d+)/)?.[1];
100
+ if (version) {
101
+ return version;
102
+ }
103
+ } catch {
104
+ // fall back to wrapper package version
105
+ }
106
+ return currentWrapperVersion();
107
+ }
108
+
109
+ function compareSemver(a, b) {
110
+ const left = String(a).split(".").map((part) => Number.parseInt(part, 10) || 0);
111
+ const right = String(b).split(".").map((part) => Number.parseInt(part, 10) || 0);
112
+ for (let i = 0; i < 3; i++) {
113
+ if ((left[i] ?? 0) > (right[i] ?? 0)) return 1;
114
+ if ((left[i] ?? 0) < (right[i] ?? 0)) return -1;
115
+ }
116
+ return 0;
117
+ }
118
+
119
+ function latestNpmVersion() {
120
+ const result = spawnSync(npmCommand(), ["view", PACKAGE_NAME, "version", "--silent"], {
121
+ encoding: "utf8",
122
+ shell: useShellForCommandShims(),
123
+ });
124
+ if (result.status !== 0) {
125
+ const detail = (result.stderr || result.stdout || "").trim();
126
+ throw new Error(detail || `npm view failed with exit code ${result.status}`);
127
+ }
128
+ return result.stdout.trim();
129
+ }
130
+
131
+ function syncDelay(ms) {
132
+ const deadline = Date.now() + ms;
133
+ while (Date.now() < deadline) {
134
+ // busy-wait for short parent-exit polling
135
+ }
136
+ }
137
+
138
+ function waitForParentExit() {
139
+ const raw = process.env[UPDATE_PARENT_PID_ENV];
140
+ if (!raw) return;
141
+ const parentPid = Number.parseInt(raw, 10);
142
+ if (!Number.isFinite(parentPid) || parentPid <= 1) return;
143
+
144
+ const deadline = Date.now() + 30_000;
145
+ while (Date.now() < deadline) {
146
+ try {
147
+ process.kill(parentPid, 0);
148
+ } catch {
149
+ return;
150
+ }
151
+ syncDelay(100);
152
+ }
153
+ }
154
+
155
+ function installedVersionViaCommand() {
156
+ const result = spawnSync(process.platform === "win32" ? "impulse.cmd" : "impulse", ["--version"], {
157
+ encoding: "utf8",
158
+ shell: useShellForCommandShims(),
159
+ });
160
+ return result.stdout?.trim().match(/(\d+\.\d+\.\d+)/)?.[1];
161
+ }
162
+
163
+ function formatUpdateSuccessLines(latestVersion, installedVersion, relaunch) {
164
+ const lines = [];
165
+ if (installedVersion === latestVersion) {
166
+ lines.push(` Update successful! impulse is now v${latestVersion}`);
167
+ } else if (installedVersion) {
168
+ lines.push(` Update completed. Installed version: v${installedVersion}`);
169
+ lines.push(` (Expected v${latestVersion} - you may need to restart your shell)`);
170
+ } else {
171
+ lines.push(` Update completed! impulse should now be v${latestVersion}`);
172
+ }
173
+ if (relaunch) {
174
+ lines.push(" Relaunching impulse...");
175
+ } else {
176
+ lines.push(" Run `impulse` to start.");
177
+ }
178
+ return lines;
179
+ }
180
+
181
+ function relaunchImpulse() {
182
+ const env = { ...process.env };
183
+ delete env[INTERNAL_AUTO_UPDATE_ENV];
184
+ const child = spawn(process.platform === "win32" ? "impulse.cmd" : "impulse", [], {
185
+ detached: true,
186
+ stdio: "ignore",
187
+ shell: useShellForCommandShims(),
188
+ env,
189
+ });
190
+ child.unref();
191
+ }
192
+
193
+ function runUpdate({ relaunch }) {
194
+ const current = currentInstalledVersion();
195
+ const latest = latestNpmVersion();
196
+ if (compareSemver(latest, current) <= 0) {
197
+ console.log(`Already on latest (v${current}).`);
198
+ if (relaunch) {
199
+ console.log(" Relaunching impulse...");
200
+ relaunchImpulse();
201
+ }
202
+ return 0;
203
+ }
204
+
205
+ console.log(`Updating ${current} -> ${latest}...`);
206
+ console.log(`\nUpdating impulse to v${latest}...`);
207
+ console.log(`Running: npm install -g ${PACKAGE_NAME}\n`);
208
+
209
+ const result = spawnSync(npmCommand(), ["install", "-g", PACKAGE_NAME], {
210
+ stdio: "inherit",
211
+ shell: useShellForCommandShims(),
212
+ });
213
+ if (result.status !== 0) {
214
+ console.log("\n--------------------------------------------------------");
215
+ console.log(` Update failed (exit code ${result.status ?? "unknown"})`);
216
+ console.log(` Try running manually: npm install -g ${PACKAGE_NAME}`);
217
+ console.log("--------------------------------------------------------\n");
218
+ return result.status ?? 1;
219
+ }
220
+
221
+ const installedVersion = installedVersionViaCommand();
222
+ console.log("\n--------------------------------------------------------");
223
+ for (const line of formatUpdateSuccessLines(latest, installedVersion, relaunch)) {
224
+ console.log(line);
225
+ }
226
+ console.log("--------------------------------------------------------\n");
227
+
228
+ if (relaunch) {
229
+ relaunchImpulse();
230
+ }
231
+ return 0;
232
+ }
233
+
234
+ function hasStartupFlag(flag) {
235
+ for (const arg of process.argv.slice(2)) {
236
+ if (arg === "--") return false;
237
+ if (!arg.startsWith("-")) continue;
238
+ if (arg === flag) return true;
239
+ }
240
+ return false;
241
+ }
242
+
243
+ if (hasStartupFlag("--update") || hasStartupFlag("--auto-update")) {
244
+ try {
245
+ const relaunch =
246
+ hasStartupFlag("--auto-update") && process.env[INTERNAL_AUTO_UPDATE_ENV] === "1";
247
+ if (relaunch) {
248
+ waitForParentExit();
249
+ }
250
+ process.exit(runUpdate({ relaunch }));
251
+ } catch (error) {
252
+ console.error(`Update failed: ${error.message}`);
253
+ process.exit(1);
254
+ }
255
+ }
256
+
77
257
  try {
78
258
  const binaryPath = findBinary();
79
259
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spenceriam/impulse",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
4
4
  "description": "Provider-flexible terminal AI coding agent",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -21,11 +21,11 @@
21
21
  "access": "public"
22
22
  },
23
23
  "optionalDependencies": {
24
- "@spenceriam/impulse-linux-x64": "1.8.0",
25
- "@spenceriam/impulse-linux-arm64": "1.8.0",
26
- "@spenceriam/impulse-darwin-x64": "1.8.0",
27
- "@spenceriam/impulse-darwin-arm64": "1.8.0",
28
- "@spenceriam/impulse-windows-x64": "1.8.0",
29
- "@spenceriam/impulse-win32-arm64": "1.8.0"
24
+ "@spenceriam/impulse-linux-x64": "1.8.2",
25
+ "@spenceriam/impulse-linux-arm64": "1.8.2",
26
+ "@spenceriam/impulse-darwin-x64": "1.8.2",
27
+ "@spenceriam/impulse-darwin-arm64": "1.8.2",
28
+ "@spenceriam/impulse-windows-x64": "1.8.2",
29
+ "@spenceriam/impulse-win32-arm64": "1.8.2"
30
30
  }
31
31
  }