ccstatusline-zh 2.2.33 → 2.2.35

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,180 @@
1
+ import {
2
+ SettingsSchema
3
+ } from "./ccstatusline-88bs860s.js";
4
+
5
+ // src/utils/dev-reload.ts
6
+ import { spawn as spawnProcess } from "node:child_process";
7
+ import * as fs from "node:fs";
8
+ import * as os from "node:os";
9
+ import * as path from "node:path";
10
+ var DEV_RELOAD_ENV = "CCSTATUSLINE_DEV_RELOAD";
11
+ var DEV_RELOAD_STATE_ENV = "CCSTATUSLINE_DEV_RELOAD_STATE";
12
+ var DEV_RELOAD_EXIT_CODE2 = 75;
13
+ function getDevReloadMode2(env) {
14
+ if (env[DEV_RELOAD_ENV] !== "1") {
15
+ return "off";
16
+ }
17
+ return env[DEV_RELOAD_STATE_ENV] ? "child" : "supervisor";
18
+ }
19
+ function getDevReloadStateFile2(env) {
20
+ return getDevReloadMode2(env) === "child" ? env[DEV_RELOAD_STATE_ENV] ?? null : null;
21
+ }
22
+ var RESTORABLE_SCREENS = [
23
+ "main",
24
+ "lines",
25
+ "items",
26
+ "colorLines",
27
+ "colors",
28
+ "terminalWidth",
29
+ "terminalConfig",
30
+ "globalOverrides",
31
+ "powerline"
32
+ ];
33
+ function toRestorableScreen(screen) {
34
+ return RESTORABLE_SCREENS.find((restorable) => restorable === screen) ?? "main";
35
+ }
36
+ function toCount(value) {
37
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : 0;
38
+ }
39
+ function writeDevReloadSnapshot2(stateFile, snapshot) {
40
+ fs.writeFileSync(stateFile, JSON.stringify(snapshot), "utf-8");
41
+ }
42
+ function discardDevReloadSnapshot2(stateFile) {
43
+ fs.rmSync(stateFile, { force: true });
44
+ }
45
+ function readDevReloadSnapshot2(stateFile) {
46
+ let raw;
47
+ try {
48
+ raw = JSON.parse(fs.readFileSync(stateFile, "utf-8"));
49
+ } catch {
50
+ return null;
51
+ }
52
+ if (typeof raw !== "object" || raw === null) {
53
+ return null;
54
+ }
55
+ const candidate = raw;
56
+ const settings = SettingsSchema.safeParse(candidate.settings);
57
+ const originalSettings = SettingsSchema.safeParse(candidate.originalSettings);
58
+ if (!settings.success || !originalSettings.success) {
59
+ return null;
60
+ }
61
+ const menuSelections = typeof candidate.menuSelections === "object" && candidate.menuSelections !== null ? Object.fromEntries(Object.entries(candidate.menuSelections).map(([key, value]) => [key, toCount(value)])) : {};
62
+ return {
63
+ settings: settings.data,
64
+ originalSettings: originalSettings.data,
65
+ screen: toRestorableScreen(candidate.screen),
66
+ selectedLine: toCount(candidate.selectedLine),
67
+ menuSelections,
68
+ itemsCursor: toCount(candidate.itemsCursor)
69
+ };
70
+ }
71
+ function createDevReloadStateDir() {
72
+ return fs.mkdtempSync(path.join(os.tmpdir(), "ccstatusline-dev-reload-"));
73
+ }
74
+ function removeDevReloadStateDir(stateDir) {
75
+ fs.rmSync(stateDir, { recursive: true, force: true });
76
+ }
77
+ var reloadRequested = false;
78
+ function requestDevReload2() {
79
+ reloadRequested = true;
80
+ }
81
+ function isDevReloadRequested2() {
82
+ return reloadRequested;
83
+ }
84
+ async function runDevReloadSupervisor(deps) {
85
+ for (;; ) {
86
+ const exitCode = await deps.launch();
87
+ if (exitCode === DEV_RELOAD_EXIT_CODE2) {
88
+ continue;
89
+ }
90
+ if (exitCode === 0 || !await deps.promptRetry(exitCode)) {
91
+ deps.cleanup();
92
+ return exitCode;
93
+ }
94
+ }
95
+ }
96
+ function promptRetryOnTerminal(exitCode, terminal = process) {
97
+ terminal.stdout.write(`
98
+ 重载失败(退出码 ${exitCode})。修复代码后按 Enter 重试,或按 q 退出。
99
+ `);
100
+ return new Promise((resolve) => {
101
+ const stdin = terminal.stdin;
102
+ const onData = (data) => {
103
+ const key = data.toString();
104
+ const retry = key === "\r" || key === `
105
+ `;
106
+ const quit = key === "q" || key === "Q" || key === "\x03";
107
+ if (!retry && !quit) {
108
+ return;
109
+ }
110
+ stdin.off("data", onData);
111
+ stdin.setRawMode(false);
112
+ stdin.pause();
113
+ resolve(retry);
114
+ };
115
+ stdin.setRawMode(true);
116
+ stdin.resume();
117
+ stdin.on("data", onData);
118
+ });
119
+ }
120
+ function spawnInTerminal(command, args, env) {
121
+ const child = spawnProcess(command, args, { stdio: "inherit", env });
122
+ return {
123
+ exited: new Promise((resolve) => {
124
+ child.once("exit", (code) => {
125
+ resolve(code ?? 1);
126
+ });
127
+ child.once("error", () => {
128
+ resolve(1);
129
+ });
130
+ }),
131
+ kill: (signal) => {
132
+ child.kill(signal);
133
+ }
134
+ };
135
+ }
136
+ var STOP_SIGNALS = ["SIGTERM", "SIGINT", "SIGHUP"];
137
+ function signalExitCode(signal) {
138
+ return 128 + os.constants.signals[signal];
139
+ }
140
+ async function superviseDevReload2(launchArgs, spawn = spawnInTerminal, host = process) {
141
+ const stateDir = createDevReloadStateDir();
142
+ const stateFile = path.join(stateDir, "snapshot.json");
143
+ const script = process.argv[1];
144
+ const command = [...process.execArgv, ...script ? [script] : [], ...launchArgs];
145
+ const env = { ...process.env, [DEV_RELOAD_STATE_ENV]: stateFile };
146
+ let child = null;
147
+ let stopSignal = null;
148
+ const stop = (signal) => {
149
+ stopSignal = signal;
150
+ if (child) {
151
+ child.kill(signal);
152
+ return;
153
+ }
154
+ removeDevReloadStateDir(stateDir);
155
+ host.exit(signalExitCode(signal));
156
+ };
157
+ for (const signal of STOP_SIGNALS) {
158
+ host.on(signal, stop);
159
+ }
160
+ try {
161
+ return await runDevReloadSupervisor({
162
+ launch: async () => {
163
+ child = spawn(process.execPath, command, env);
164
+ const exitCode = await child.exited;
165
+ child = null;
166
+ return stopSignal ? signalExitCode(stopSignal) : exitCode;
167
+ },
168
+ promptRetry: (exitCode) => stopSignal ? Promise.resolve(false) : promptRetryOnTerminal(exitCode, host),
169
+ cleanup: () => {
170
+ removeDevReloadStateDir(stateDir);
171
+ }
172
+ });
173
+ } finally {
174
+ for (const signal of STOP_SIGNALS) {
175
+ host.off(signal, stop);
176
+ }
177
+ }
178
+ }
179
+
180
+ export { DEV_RELOAD_EXIT_CODE2, getDevReloadMode2, getDevReloadStateFile2, writeDevReloadSnapshot2, discardDevReloadSnapshot2, readDevReloadSnapshot2, requestDevReload2, isDevReloadRequested2, superviseDevReload2 };