upgradr 0.1.0 → 0.2.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.
package/README.md CHANGED
@@ -101,10 +101,30 @@ if (!result.hasUpdate) {
101
101
  | `checkIntervalMs` | | 24h | How long a cached check stays fresh. |
102
102
  | `timeoutMs` | | 5000 | Timeout for the background network check. |
103
103
  | `backgroundSkipCommands` | | `["check-update","upgrade"]` | Commands that suppress the startup notice. |
104
+ | `formatNotice` | | built-in | `(ctx) => string` to fully render the startup notice. |
104
105
  | `assetName` | | — | `(target) => string` to fully override the release asset name. |
105
106
  | `fetchImpl` | | global `fetch` | Injectable `fetch` (for testing). |
106
107
  | `onProgress` | | no-op | Progress callback (e.g. the sudo heads-up during a binary swap). |
107
108
 
109
+ ## Customizing the notice
110
+
111
+ By default the startup notice is a yellow "Update available" header, an optional
112
+ dimmed changelog, and a cyan `Run <cli> upgrade` hint. Pass `formatNotice` to render
113
+ it yourself — you get the update details and return the whole string. The built-in
114
+ renderer is exported, so you can compose with it:
115
+
116
+ ```ts
117
+ import { createUpdater, formatNotice } from "upgradr";
118
+
119
+ const updater = createUpdater({
120
+ // …required config…
121
+ formatNotice: (ctx) =>
122
+ `${formatNotice(ctx)}\n (docs: https://example.com/changelog)\n`,
123
+ });
124
+ ```
125
+
126
+ `ctx` is `{ currentVersion, latestVersion, changelog?, cliName }`.
127
+
108
128
  ## Install-method detection
109
129
 
110
130
  `detectInstallMethod()` inspects `process.argv[1]` and `process.execPath`:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "upgradr",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Self-update for Bun 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/config.ts CHANGED
@@ -18,6 +18,18 @@ export interface AssetTarget {
18
18
  arch: string;
19
19
  }
20
20
 
21
+ /** Data passed to a custom {@link UpdaterConfig.formatNotice} renderer. */
22
+ export interface NoticeContext {
23
+ /** The currently-running version. */
24
+ currentVersion: string;
25
+ /** The newer version available. */
26
+ latestVersion: string;
27
+ /** Release changelog, when one was fetched. */
28
+ changelog?: string;
29
+ /** Display name for the CLI (see {@link UpdaterConfig.cliName}). */
30
+ cliName: string;
31
+ }
32
+
21
33
  /**
22
34
  * Configuration for {@link createUpdater}. Every value that would otherwise be
23
35
  * hardcoded to a specific project (repo, package name, cache location, …) is a
@@ -50,6 +62,12 @@ export interface UpdaterConfig {
50
62
  timeoutMs?: number;
51
63
  /** Commands for which the background notice is suppressed. Defaults to `["check-update", "upgrade"]`. */
52
64
  backgroundSkipCommands?: string[];
65
+ /**
66
+ * Render the whole background notice string. Receives the update details and
67
+ * returns the text shown to the user. Defaults to the built-in {@link formatNotice},
68
+ * which you can import and compose with (e.g. prefix or suffix its output).
69
+ */
70
+ formatNotice?: (ctx: NoticeContext) => string;
53
71
  /** Override the full release asset name for a platform. */
54
72
  assetName?: (target: AssetTarget) => string;
55
73
  /** Injectable `fetch` (for testing). Defaults to the global `fetch`. */
@@ -71,6 +89,7 @@ export interface ResolvedConfig {
71
89
  checkIntervalMs: number;
72
90
  timeoutMs: number;
73
91
  backgroundSkipCommands: string[];
92
+ formatNotice: ((ctx: NoticeContext) => string) | undefined;
74
93
  assetName: ((target: AssetTarget) => string) | undefined;
75
94
  fetchImpl: FetchLike;
76
95
  onProgress: (message: string) => void;
@@ -96,6 +115,7 @@ export function resolveConfig(config: UpdaterConfig): ResolvedConfig {
96
115
  timeoutMs: config.timeoutMs ?? DEFAULT_TIMEOUT_MS,
97
116
  backgroundSkipCommands:
98
117
  config.backgroundSkipCommands ?? DEFAULT_BACKGROUND_SKIP_COMMANDS,
118
+ formatNotice: config.formatNotice,
99
119
  assetName: config.assetName,
100
120
  fetchImpl: config.fetchImpl ?? fetch,
101
121
  onProgress: config.onProgress ?? (() => {}),
package/src/index.ts CHANGED
@@ -30,6 +30,7 @@ export type {
30
30
  AssetTarget,
31
31
  FetchLike,
32
32
  InstallMethod,
33
+ NoticeContext,
33
34
  ResolvedConfig,
34
35
  UpdaterConfig,
35
36
  } from "./config.ts";
package/src/notice.ts CHANGED
@@ -1,27 +1,22 @@
1
1
  import { cyan, dim, yellow } from "ansis";
2
2
  import { loadCache, saveCache } from "./cache.ts";
3
3
  import { checkForUpdate, needsCheck, type UpdateCache } from "./checker.ts";
4
- import type { ResolvedConfig } from "./config.ts";
4
+ import type { NoticeContext, ResolvedConfig } from "./config.ts";
5
5
 
6
6
  /** Format the "update available" notice shown on startup. */
7
- export function formatNotice(opts: {
8
- currentVersion: string;
9
- latestVersion: string;
10
- changelog?: string;
11
- cliName: string;
12
- }): string {
7
+ export function formatNotice(ctx: NoticeContext): string {
13
8
  const lines: string[] = [
14
9
  "",
15
- yellow(`Update available: ${opts.currentVersion} → ${opts.latestVersion}`),
10
+ yellow(`Update available: ${ctx.currentVersion} → ${ctx.latestVersion}`),
16
11
  ];
17
12
 
18
- if (opts.changelog) {
13
+ if (ctx.changelog) {
19
14
  lines.push("");
20
- lines.push(dim(opts.changelog));
15
+ lines.push(dim(ctx.changelog));
21
16
  }
22
17
 
23
18
  lines.push("");
24
- lines.push(cyan(`Run \`${opts.cliName} upgrade\` to update`));
19
+ lines.push(cyan(`Run \`${ctx.cliName} upgrade\` to update`));
25
20
  lines.push("");
26
21
 
27
22
  return lines.join("\n");
@@ -59,12 +54,13 @@ export async function maybeBackgroundNotice(
59
54
  // Only show in an interactive terminal.
60
55
  if (!isTTY) return null;
61
56
 
57
+ const render = cfg.formatNotice ?? formatNotice;
62
58
  const cache = await loadCache(cfg.cacheDir);
63
59
 
64
60
  if (!needsCheck(cache, cfg.checkIntervalMs)) {
65
61
  // Cache is fresh — use the cached result.
66
62
  if (cache?.hasUpdate) {
67
- return formatNotice({
63
+ return render({
68
64
  currentVersion: cfg.currentVersion,
69
65
  latestVersion: cache.latestVersion,
70
66
  changelog: cache.changelog,
@@ -96,7 +92,7 @@ export async function maybeBackgroundNotice(
96
92
  await saveCache(cfg.cacheDir, newCache);
97
93
 
98
94
  if (info.hasUpdate) {
99
- return formatNotice({
95
+ return render({
100
96
  currentVersion: cfg.currentVersion,
101
97
  latestVersion: info.latestVersion,
102
98
  changelog: info.changelog,