@hardfin/cli 0.1.1-dev.67 → 0.1.1-dev.69

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,134 @@
1
+ #!/usr/bin/env node
2
+
3
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="85e43a08-810a-5f4e-9d70-a869c9a65ee1")}catch(e){}}();
4
+ import { S as toLastFailureCode, c as toOutdatedTo, i as toUpdateCheckRefusal, n as isReminderDue, o as toPackageDirectory, r as toCheck, t as PACKAGE, u as API_VERSION, x as toJSON } from "./check-CDkHR_Qt.js";
5
+ //#region src/update/notice.ts
6
+ /**
7
+ * The failures a newer CLI may not have, because each can mean the API changed under this one:
8
+ * the API refusing the version or the request, or the CLI not knowing a command, flag or field.
9
+ */
10
+ const SCHEMA_FAILURES = /* @__PURE__ */ new Set([
11
+ "cli_outdated",
12
+ "bad_request",
13
+ "invalid_request",
14
+ "invalid_input",
15
+ "invalid_flag",
16
+ "unknown_command"
17
+ ]);
18
+ /** Commands that never print the notice: the server, which owns its streams, the update itself, and Tab completion. */
19
+ const QUIET_COMMANDS = /* @__PURE__ */ new Set([
20
+ "mcp",
21
+ "update",
22
+ "__complete"
23
+ ]);
24
+ /** The global flags that take a value, which come before the command's name. */
25
+ const VALUE_FLAGS = /* @__PURE__ */ new Set([
26
+ "--api-url",
27
+ "--api-key",
28
+ "--issuer-url"
29
+ ]);
30
+ /** How long a finished command waits for the registry before it gives up on the check. */
31
+ const SETTLE_MS = 1500;
32
+ /** startUpdateCheck begins the daily check, or answers nothing when this run should not ask. */
33
+ function startUpdateCheck(args, env = process.env) {
34
+ if (toUpdateCheckRefusal(env) !== void 0 || QUIET_COMMANDS.has(toCommandName(args) ?? "")) return;
35
+ return toPending(false);
36
+ }
37
+ /**
38
+ * writeUpdateNotice says on stderr when a newer CLI is published. It repeats a plain reminder
39
+ * once a day, and says it every time a command fails in a way a newer CLI may fix.
40
+ */
41
+ async function writeUpdateNotice(pending, args) {
42
+ if (!pending) return;
43
+ const failure = toLastFailureCode();
44
+ const outdatedTo = toOutdatedTo();
45
+ const reason = outdatedTo !== void 0 ? "api_version_outdated" : failure !== void 0 && SCHEMA_FAILURES.has(failure) ? "failure" : "reminder";
46
+ let check = await toSettled(pending);
47
+ if (reason !== "reminder" && (check === void 0 || check.isCached)) check = await toSettled(toPending(true)) ?? check;
48
+ const isJSON = args.includes("--json") || !process.stdout.isTTY;
49
+ if (check?.isOutdated && (reason !== "reminder" || isReminderDue(check.latest))) {
50
+ writeNotice(toUpdateNotice(check, reason), isJSON);
51
+ return;
52
+ }
53
+ if (outdatedTo !== void 0) writeNotice(toApiNotice(outdatedTo), isJSON);
54
+ }
55
+ function toUpdateNotice(check, reason) {
56
+ const command = toUpdateCommand(check.tag);
57
+ const because = reason === "api_version_outdated" ? ` The API has changed since version ${API_VERSION}, which this CLI speaks.` : reason === "failure" ? " What this command was refused for may be fixed in it." : "";
58
+ return {
59
+ fields: {
60
+ event: "update_available",
61
+ version: check.version,
62
+ latest: check.latest,
63
+ reason,
64
+ command
65
+ },
66
+ text: `hardfin ${check.latest} is available, and this is ${check.version}.${because} Run ${command} to update.`
67
+ };
68
+ }
69
+ function toApiNotice(latestVersion) {
70
+ return {
71
+ fields: {
72
+ event: "api_version_outdated",
73
+ apiVersion: API_VERSION,
74
+ latestVersion,
75
+ command: "hardfin update"
76
+ },
77
+ text: `The API answered from an earlier version of an endpoint, because this CLI speaks API version ${API_VERSION} and the endpoint changed by ${latestVersion}. Run hardfin update to look for a CLI that speaks it.`
78
+ };
79
+ }
80
+ function writeNotice(notice, isJSON) {
81
+ process.stderr.write(isJSON ? `${toJSON(notice.fields, process.stderr)}\n` : `${notice.text}\n`);
82
+ }
83
+ /**
84
+ * toUpdateCommand names how to update. A copy npx runs has no hardfin update to run, because
85
+ * the next npx run would start the cached copy again.
86
+ */
87
+ function toUpdateCommand(tag) {
88
+ try {
89
+ return /[\\/]_npx[\\/]/.test(toPackageDirectory()) ? `npx -y ${PACKAGE}@${tag}` : "hardfin update";
90
+ } catch {
91
+ return "hardfin update";
92
+ }
93
+ }
94
+ function toPending(isFresh) {
95
+ const controller = new AbortController();
96
+ return {
97
+ answer: toCheck({
98
+ isFresh,
99
+ signal: controller.signal
100
+ }).catch(() => void 0),
101
+ controller
102
+ };
103
+ }
104
+ /** toSettled waits a short while for a check, and abandons it after that so the process can end. */
105
+ async function toSettled(pending) {
106
+ let timer;
107
+ const late = new Promise((resolve) => {
108
+ timer = setTimeout(() => {
109
+ pending.controller.abort();
110
+ resolve(void 0);
111
+ }, SETTLE_MS);
112
+ });
113
+ try {
114
+ return await Promise.race([pending.answer, late]);
115
+ } finally {
116
+ clearTimeout(timer);
117
+ }
118
+ }
119
+ /** toCommandName finds the command a command line runs, past the global flags before it. */
120
+ function toCommandName(args) {
121
+ for (let index = 0; index < args.length; index++) {
122
+ const argument = args[index] ?? "";
123
+ if (VALUE_FLAGS.has(argument)) {
124
+ index++;
125
+ continue;
126
+ }
127
+ if (!argument.startsWith("-")) return argument;
128
+ }
129
+ }
130
+ //#endregion
131
+ export { startUpdateCheck, writeUpdateNotice };
132
+
133
+ //# sourceMappingURL=notice-Bq9ifPV6.js.map
134
+ //# debugId=85e43a08-810a-5f4e-9d70-a869c9a65ee1