upgradr 0.4.0 → 0.4.1

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/index.js CHANGED
@@ -57,9 +57,12 @@ export function createUpdater(config) {
57
57
  let outcome;
58
58
  switch (method) {
59
59
  case "bun":
60
+ // upgradr just confirmed this version on the registry, so skip
61
+ // bun's manifest cache — it can lag right after a publish.
60
62
  outcome = await upgradeWithPackageManager("bun", [
61
63
  "install",
62
64
  "-g",
65
+ "--no-cache",
63
66
  `${cfg.packageName}@${info.latestVersion}`,
64
67
  ]);
65
68
  break;
package/dist/upgrade.js CHANGED
@@ -4,20 +4,42 @@ import { tmpdir } from "node:os";
4
4
  import { join } from "node:path";
5
5
  import { promisify } from "node:util";
6
6
  const execFileAsync = promisify(execFile);
7
+ /** Max number of trailing output lines kept in an error message. */
8
+ const OUTPUT_TAIL_LINES = 10;
7
9
  /**
8
- * Run a command without throwing, returning its exit code. Cross-runtime
9
- * (Node + Bun) replacement for Bun's `$`…`.nothrow()`.
10
+ * Run a command without throwing, returning its exit code and output.
11
+ * Cross-runtime (Node + Bun) replacement for Bun's `$`…`.nothrow()`.
10
12
  */
11
13
  async function run(command, args) {
12
14
  try {
13
- await execFileAsync(command, args);
14
- return { exitCode: 0 };
15
+ const { stdout, stderr } = await execFileAsync(command, args);
16
+ return { exitCode: 0, stdout: String(stdout), stderr: String(stderr) };
15
17
  }
16
18
  catch (err) {
17
- const code = err.code;
18
- return { exitCode: typeof code === "number" ? code : 1 };
19
+ const e = err;
20
+ return {
21
+ exitCode: typeof e.code === "number" ? e.code : 1,
22
+ stdout: String(e.stdout ?? ""),
23
+ stderr: String(e.stderr ?? ""),
24
+ spawnError: typeof e.code === "string" ? e.message : undefined,
25
+ };
19
26
  }
20
27
  }
28
+ /**
29
+ * Describe a failed {@link run}: the exact command, its exit code, and the
30
+ * tail of its stderr (or stdout when stderr is empty).
31
+ */
32
+ function describeFailure(command, args, result) {
33
+ const cmd = [command, ...args].join(" ");
34
+ if (result.spawnError)
35
+ return `${cmd} failed: ${result.spawnError}`;
36
+ const output = (result.stderr.trim() || result.stdout.trim())
37
+ .split("\n")
38
+ .slice(-OUTPUT_TAIL_LINES)
39
+ .join("\n");
40
+ const head = `${cmd} exited with code ${result.exitCode}`;
41
+ return output ? `${head}:\n${output}` : head;
42
+ }
21
43
  /**
22
44
  * Build the GitHub release asset name for the current platform.
23
45
  * Defaults to `<binaryName>-<os>-<arch>[.exe]`, or delegates to `assetName`.
@@ -49,10 +71,7 @@ export async function upgradeWithPackageManager(command, args) {
49
71
  const result = await run(command, args);
50
72
  return result.exitCode === 0
51
73
  ? { success: true }
52
- : {
53
- success: false,
54
- error: `${command} exited with code ${result.exitCode}`,
55
- };
74
+ : { success: false, error: describeFailure(command, args, result) };
56
75
  }
57
76
  /**
58
77
  * Download the release binary for `latestVersion` and swap it into place at
@@ -85,11 +104,15 @@ export async function upgradeFromBinary(opts) {
85
104
  if (mv.exitCode !== 0) {
86
105
  // Fall back to sudo.
87
106
  onProgress("Requires elevated permissions...");
88
- const sudo = await run("sudo", ["mv", tmpPath, targetPath]);
107
+ const sudoArgs = ["mv", tmpPath, targetPath];
108
+ const sudo = await run("sudo", sudoArgs);
89
109
  if (sudo.exitCode !== 0) {
110
+ await unlink(tmpPath).catch(() => { });
90
111
  return {
91
112
  success: false,
92
- error: "Failed to install binary. Try running with sudo.",
113
+ error: `Failed to install binary. Try running with sudo.\n` +
114
+ `${describeFailure("mv", [tmpPath, targetPath], mv)}\n` +
115
+ describeFailure("sudo", sudoArgs, sudo),
93
116
  };
94
117
  }
95
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "upgradr",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Self-update for Bun & Node CLIs: check npm for a newer release, then upgrade in place via npm/bun or a GitHub release binary.",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -156,9 +156,12 @@ export function createUpdater(config: UpdaterConfig): Updater {
156
156
  let outcome: InstallOutcome;
157
157
  switch (method) {
158
158
  case "bun":
159
+ // upgradr just confirmed this version on the registry, so skip
160
+ // bun's manifest cache — it can lag right after a publish.
159
161
  outcome = await upgradeWithPackageManager("bun", [
160
162
  "install",
161
163
  "-g",
164
+ "--no-cache",
162
165
  `${cfg.packageName}@${info.latestVersion}`,
163
166
  ]);
164
167
  break;
package/src/upgrade.ts CHANGED
@@ -13,23 +13,60 @@ export interface InstallOutcome {
13
13
  error?: string;
14
14
  }
15
15
 
16
+ /** Max number of trailing output lines kept in an error message. */
17
+ const OUTPUT_TAIL_LINES = 10;
18
+
19
+ interface RunResult {
20
+ exitCode: number;
21
+ stdout: string;
22
+ stderr: string;
23
+ /** Spawn failure (e.g. ENOENT) when the command never ran. */
24
+ spawnError?: string;
25
+ }
26
+
16
27
  /**
17
- * Run a command without throwing, returning its exit code. Cross-runtime
18
- * (Node + Bun) replacement for Bun's `$`…`.nothrow()`.
28
+ * Run a command without throwing, returning its exit code and output.
29
+ * Cross-runtime (Node + Bun) replacement for Bun's `$`…`.nothrow()`.
19
30
  */
20
- async function run(
21
- command: string,
22
- args: string[],
23
- ): Promise<{ exitCode: number }> {
31
+ async function run(command: string, args: string[]): Promise<RunResult> {
24
32
  try {
25
- await execFileAsync(command, args);
26
- return { exitCode: 0 };
33
+ const { stdout, stderr } = await execFileAsync(command, args);
34
+ return { exitCode: 0, stdout: String(stdout), stderr: String(stderr) };
27
35
  } catch (err) {
28
- const code = (err as { code?: number | string }).code;
29
- return { exitCode: typeof code === "number" ? code : 1 };
36
+ const e = err as {
37
+ code?: number | string;
38
+ stdout?: unknown;
39
+ stderr?: unknown;
40
+ message?: string;
41
+ };
42
+ return {
43
+ exitCode: typeof e.code === "number" ? e.code : 1,
44
+ stdout: String(e.stdout ?? ""),
45
+ stderr: String(e.stderr ?? ""),
46
+ spawnError: typeof e.code === "string" ? e.message : undefined,
47
+ };
30
48
  }
31
49
  }
32
50
 
51
+ /**
52
+ * Describe a failed {@link run}: the exact command, its exit code, and the
53
+ * tail of its stderr (or stdout when stderr is empty).
54
+ */
55
+ function describeFailure(
56
+ command: string,
57
+ args: string[],
58
+ result: RunResult,
59
+ ): string {
60
+ const cmd = [command, ...args].join(" ");
61
+ if (result.spawnError) return `${cmd} failed: ${result.spawnError}`;
62
+ const output = (result.stderr.trim() || result.stdout.trim())
63
+ .split("\n")
64
+ .slice(-OUTPUT_TAIL_LINES)
65
+ .join("\n");
66
+ const head = `${cmd} exited with code ${result.exitCode}`;
67
+ return output ? `${head}:\n${output}` : head;
68
+ }
69
+
33
70
  /**
34
71
  * Build the GitHub release asset name for the current platform.
35
72
  * Defaults to `<binaryName>-<os>-<arch>[.exe]`, or delegates to `assetName`.
@@ -71,10 +108,7 @@ export async function upgradeWithPackageManager(
71
108
  const result = await run(command, args);
72
109
  return result.exitCode === 0
73
110
  ? { success: true }
74
- : {
75
- success: false,
76
- error: `${command} exited with code ${result.exitCode}`,
77
- };
111
+ : { success: false, error: describeFailure(command, args, result) };
78
112
  }
79
113
 
80
114
  /**
@@ -122,11 +156,16 @@ export async function upgradeFromBinary(opts: {
122
156
  if (mv.exitCode !== 0) {
123
157
  // Fall back to sudo.
124
158
  onProgress("Requires elevated permissions...");
125
- const sudo = await run("sudo", ["mv", tmpPath, targetPath]);
159
+ const sudoArgs = ["mv", tmpPath, targetPath];
160
+ const sudo = await run("sudo", sudoArgs);
126
161
  if (sudo.exitCode !== 0) {
162
+ await unlink(tmpPath).catch(() => {});
127
163
  return {
128
164
  success: false,
129
- error: "Failed to install binary. Try running with sudo.",
165
+ error:
166
+ `Failed to install binary. Try running with sudo.\n` +
167
+ `${describeFailure("mv", [tmpPath, targetPath], mv)}\n` +
168
+ describeFailure("sudo", sudoArgs, sudo),
130
169
  };
131
170
  }
132
171
  }