@narumitw/pi-starship 0.22.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/LICENSE +21 -0
- package/NOTICES.md +21 -0
- package/README.md +166 -0
- package/package.json +52 -0
- package/src/commands.ts +108 -0
- package/src/config.ts +569 -0
- package/src/format/formatter.ts +260 -0
- package/src/format/style.ts +247 -0
- package/src/installed-packages.ts +84 -0
- package/src/modules/activity.ts +38 -0
- package/src/modules/brand.ts +13 -0
- package/src/modules/catalog.ts +41 -0
- package/src/modules/context.ts +21 -0
- package/src/modules/cost.ts +17 -0
- package/src/modules/directory.ts +17 -0
- package/src/modules/extension-status.ts +152 -0
- package/src/modules/git/branch.ts +52 -0
- package/src/modules/git/runtime.ts +111 -0
- package/src/modules/git/status.ts +36 -0
- package/src/modules/git/worktree.ts +16 -0
- package/src/modules/helpers.ts +5 -0
- package/src/modules/index.ts +16 -0
- package/src/modules/model.ts +21 -0
- package/src/modules/provider.ts +13 -0
- package/src/modules/render.ts +73 -0
- package/src/modules/thinking.ts +13 -0
- package/src/modules/time.ts +20 -0
- package/src/modules/tokens.ts +18 -0
- package/src/modules/turn.ts +13 -0
- package/src/modules/types.ts +79 -0
- package/src/pi-starship.ts +325 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
linkSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
renameSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from "node:fs";
|
|
11
|
+
import { dirname, join } from "node:path";
|
|
12
|
+
import { parse, type TomlTable } from "smol-toml";
|
|
13
|
+
import {
|
|
14
|
+
type FormatNode,
|
|
15
|
+
formatVariables,
|
|
16
|
+
parseFormat,
|
|
17
|
+
styleVariables,
|
|
18
|
+
} from "./format/formatter.js";
|
|
19
|
+
import { type ColorPalette, isValidStyle, parseColor } from "./format/style.js";
|
|
20
|
+
import { MODULE_DEFINITIONS, MODULE_NAMES, type ModuleName } from "./modules/catalog.js";
|
|
21
|
+
|
|
22
|
+
export const CONFIG_FILE_NAME = "pi-starship.toml";
|
|
23
|
+
export { MODULE_NAMES, type ModuleName } from "./modules/catalog.js";
|
|
24
|
+
|
|
25
|
+
const MODULE_CONTENT_VARIABLES = Object.fromEntries(
|
|
26
|
+
MODULE_DEFINITIONS.map((definition) => [definition.name, definition.variables]),
|
|
27
|
+
) as Record<ModuleName, readonly string[]>;
|
|
28
|
+
|
|
29
|
+
export interface ModuleConfig {
|
|
30
|
+
format: string;
|
|
31
|
+
formatAst: FormatNode[];
|
|
32
|
+
symbol: string;
|
|
33
|
+
style: string;
|
|
34
|
+
disabled: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ExtensionStatusConfig {
|
|
38
|
+
separator: string;
|
|
39
|
+
maxStatuses: number;
|
|
40
|
+
icons: Record<string, string>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface StarshipConfig {
|
|
44
|
+
format: string;
|
|
45
|
+
formatAst: FormatNode[];
|
|
46
|
+
palette: string;
|
|
47
|
+
palettes: Record<string, Record<string, string>>;
|
|
48
|
+
modules: Record<ModuleName, ModuleConfig>;
|
|
49
|
+
extensionStatus: ExtensionStatusConfig;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ConfigDiagnostic {
|
|
53
|
+
severity: "warning" | "error";
|
|
54
|
+
path: string;
|
|
55
|
+
message: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface LoadedStarshipConfig {
|
|
59
|
+
config: StarshipConfig;
|
|
60
|
+
source: "built-in" | "user";
|
|
61
|
+
settingsPath: string;
|
|
62
|
+
rawDocument?: string;
|
|
63
|
+
diagnostics: ConfigDiagnostic[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const BUILT_IN_FORMAT_DOCUMENT = String.raw`format = """
|
|
67
|
+
[░▒▓](lead)\
|
|
68
|
+
$brand\
|
|
69
|
+
$provider\
|
|
70
|
+
$model\
|
|
71
|
+
$thinking\
|
|
72
|
+
[](fg:header bg:directory)\
|
|
73
|
+
$directory\
|
|
74
|
+
[](fg:directory bg:git)\
|
|
75
|
+
$git_worktree\
|
|
76
|
+
$git_branch\
|
|
77
|
+
$git_status\
|
|
78
|
+
[](fg:git bg:runtime)\
|
|
79
|
+
$activity\
|
|
80
|
+
$context\
|
|
81
|
+
$tokens\
|
|
82
|
+
[](fg:runtime bg:meter)\
|
|
83
|
+
$cost\
|
|
84
|
+
$time\
|
|
85
|
+
[](fg:meter)\
|
|
86
|
+
(\n$extension_status)"""`;
|
|
87
|
+
|
|
88
|
+
const BUILT_IN_FORMAT = parseBuiltInFormat(BUILT_IN_FORMAT_DOCUMENT);
|
|
89
|
+
|
|
90
|
+
const BUILT_IN_PALETTE = {
|
|
91
|
+
lead: "#a3aed2",
|
|
92
|
+
header: "#a3aed2",
|
|
93
|
+
header_fg: "#090c0c",
|
|
94
|
+
directory: "#769ff0",
|
|
95
|
+
directory_fg: "#e3e5e5",
|
|
96
|
+
git: "#394260",
|
|
97
|
+
git_fg: "#769ff0",
|
|
98
|
+
runtime: "#212736",
|
|
99
|
+
runtime_fg: "#769ff0",
|
|
100
|
+
meter: "#1d2230",
|
|
101
|
+
meter_fg: "#a0a9cb",
|
|
102
|
+
extension: "#a0a9cb",
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const BUILT_IN_MODULES = Object.fromEntries(
|
|
106
|
+
MODULE_DEFINITIONS.map(({ name, defaults }) => [
|
|
107
|
+
name,
|
|
108
|
+
{
|
|
109
|
+
...defaults,
|
|
110
|
+
formatAst: parseFormat(defaults.format),
|
|
111
|
+
},
|
|
112
|
+
]),
|
|
113
|
+
) as Record<ModuleName, ModuleConfig>;
|
|
114
|
+
|
|
115
|
+
export const BUILT_IN_CONFIG: StarshipConfig = {
|
|
116
|
+
format: BUILT_IN_FORMAT,
|
|
117
|
+
formatAst: parseFormat(BUILT_IN_FORMAT),
|
|
118
|
+
palette: "tokyo-night",
|
|
119
|
+
palettes: { "tokyo-night": BUILT_IN_PALETTE },
|
|
120
|
+
modules: BUILT_IN_MODULES,
|
|
121
|
+
extensionStatus: { separator: " • ", maxStatuses: 5, icons: {} },
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const BUILT_IN_EXAMPLE = `# Native Pi modules with Starship-compatible format and style syntax.\n${BUILT_IN_FORMAT_DOCUMENT}\npalette = "tokyo-night"\n\n[palettes.tokyo-night]\n${Object.entries(
|
|
125
|
+
BUILT_IN_PALETTE,
|
|
126
|
+
)
|
|
127
|
+
.map(([name, color]) => `${name} = "${color}"`)
|
|
128
|
+
.join("\n")}\n`;
|
|
129
|
+
|
|
130
|
+
function parseBuiltInFormat(document: string): string {
|
|
131
|
+
const format = parse(document).format;
|
|
132
|
+
if (typeof format !== "string") throw new Error("Built-in format document must define a string");
|
|
133
|
+
return format;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function settingsFilePath(agentDir: string): string {
|
|
137
|
+
return join(agentDir, CONFIG_FILE_NAME);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function loadStarshipConfig(settingsPath: string): LoadedStarshipConfig {
|
|
141
|
+
let rawDocument: string;
|
|
142
|
+
try {
|
|
143
|
+
rawDocument = readFileSync(settingsPath, "utf8");
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (!existsSync(settingsPath)) {
|
|
146
|
+
return {
|
|
147
|
+
config: cloneBuiltInConfig(),
|
|
148
|
+
source: "built-in",
|
|
149
|
+
settingsPath,
|
|
150
|
+
diagnostics: [],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
config: cloneBuiltInConfig(),
|
|
155
|
+
source: "built-in",
|
|
156
|
+
settingsPath,
|
|
157
|
+
diagnostics: [diagnostic("error", "", `Unable to read settings: ${formatError(error)}`)],
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let parsed: TomlTable;
|
|
162
|
+
try {
|
|
163
|
+
parsed = parse(rawDocument);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
return {
|
|
166
|
+
config: cloneBuiltInConfig(),
|
|
167
|
+
source: "built-in",
|
|
168
|
+
settingsPath,
|
|
169
|
+
rawDocument,
|
|
170
|
+
diagnostics: [diagnostic("error", "", `Unable to parse TOML: ${formatError(error)}`)],
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const normalized = normalizeConfig(parsed);
|
|
174
|
+
return {
|
|
175
|
+
...normalized,
|
|
176
|
+
source: "user",
|
|
177
|
+
settingsPath,
|
|
178
|
+
rawDocument,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface InitialFileSystem {
|
|
183
|
+
mkdirSync: typeof mkdirSync;
|
|
184
|
+
writeFileSync: typeof writeFileSync;
|
|
185
|
+
linkSync: typeof linkSync;
|
|
186
|
+
rmSync: typeof rmSync;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function loadOrCreateStarshipConfig(
|
|
190
|
+
settingsPath: string,
|
|
191
|
+
overrides: Partial<InitialFileSystem> = {},
|
|
192
|
+
): LoadedStarshipConfig {
|
|
193
|
+
const loaded = loadStarshipConfig(settingsPath);
|
|
194
|
+
if (loaded.source === "user" || loaded.diagnostics.length > 0) return loaded;
|
|
195
|
+
|
|
196
|
+
const fs = { mkdirSync, writeFileSync, linkSync, rmSync, ...overrides };
|
|
197
|
+
const tempPath = join(dirname(settingsPath), `.${CONFIG_FILE_NAME}.${randomUUID()}.tmp`);
|
|
198
|
+
try {
|
|
199
|
+
fs.mkdirSync(dirname(settingsPath), { recursive: true });
|
|
200
|
+
fs.writeFileSync(tempPath, BUILT_IN_EXAMPLE, { encoding: "utf8", flag: "wx" });
|
|
201
|
+
try {
|
|
202
|
+
fs.linkSync(tempPath, settingsPath);
|
|
203
|
+
} catch (error) {
|
|
204
|
+
if (isAlreadyExistsError(error)) return loadStarshipConfig(settingsPath);
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
return loadStarshipConfig(settingsPath);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
return {
|
|
210
|
+
...loaded,
|
|
211
|
+
diagnostics: [
|
|
212
|
+
diagnostic("warning", "", `Unable to create default settings: ${formatError(error)}`),
|
|
213
|
+
],
|
|
214
|
+
};
|
|
215
|
+
} finally {
|
|
216
|
+
try {
|
|
217
|
+
fs.rmSync(tempPath, { force: true });
|
|
218
|
+
} catch {
|
|
219
|
+
// Best-effort cleanup must not replace the initialization result.
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function normalizeConfig(value: unknown): {
|
|
225
|
+
config: StarshipConfig;
|
|
226
|
+
diagnostics: ConfigDiagnostic[];
|
|
227
|
+
} {
|
|
228
|
+
const config = cloneBuiltInConfig();
|
|
229
|
+
const diagnostics: ConfigDiagnostic[] = [];
|
|
230
|
+
if (!isRecord(value)) {
|
|
231
|
+
return {
|
|
232
|
+
config,
|
|
233
|
+
diagnostics: [diagnostic("error", "", "Settings must contain a TOML table")],
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const knownRoot = new Set(["format", "palette", "palettes", ...MODULE_NAMES]);
|
|
238
|
+
for (const key of Object.keys(value)) {
|
|
239
|
+
if (!knownRoot.has(key)) diagnostics.push(unknownDiagnostic(key));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (value.format !== undefined) {
|
|
243
|
+
if (typeof value.format !== "string") {
|
|
244
|
+
diagnostics.push(typeDiagnostic("format", "string"));
|
|
245
|
+
} else {
|
|
246
|
+
try {
|
|
247
|
+
config.formatAst = parseFormat(value.format);
|
|
248
|
+
config.format = value.format;
|
|
249
|
+
} catch (error) {
|
|
250
|
+
diagnostics.push(
|
|
251
|
+
diagnostic("warning", "format", `Invalid format; using built-in: ${formatError(error)}`),
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (value.palettes !== undefined) {
|
|
258
|
+
if (!isRecord(value.palettes)) {
|
|
259
|
+
diagnostics.push(typeDiagnostic("palettes", "table"));
|
|
260
|
+
} else {
|
|
261
|
+
for (const [paletteName, paletteValue] of Object.entries(value.palettes)) {
|
|
262
|
+
if (!isRecord(paletteValue)) {
|
|
263
|
+
diagnostics.push(typeDiagnostic(`palettes.${paletteName}`, "table"));
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const palette: Record<string, string> = {};
|
|
267
|
+
for (const [name, color] of Object.entries(paletteValue)) {
|
|
268
|
+
if (typeof color !== "string" || !parseColor(color.toLowerCase())) {
|
|
269
|
+
diagnostics.push(
|
|
270
|
+
diagnostic(
|
|
271
|
+
"warning",
|
|
272
|
+
`palettes.${paletteName}.${name}`,
|
|
273
|
+
"Palette colors must be named, ANSI 0-255, or #RRGGBB",
|
|
274
|
+
),
|
|
275
|
+
);
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
setOwn(palette, name, color);
|
|
279
|
+
}
|
|
280
|
+
setOwn(config.palettes, paletteName, palette);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (value.palette !== undefined) {
|
|
286
|
+
if (typeof value.palette !== "string") diagnostics.push(typeDiagnostic("palette", "string"));
|
|
287
|
+
else if (!Object.hasOwn(config.palettes, value.palette)) {
|
|
288
|
+
diagnostics.push(
|
|
289
|
+
diagnostic("warning", "palette", `Unknown palette ${JSON.stringify(value.palette)}`),
|
|
290
|
+
);
|
|
291
|
+
} else config.palette = value.palette;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
for (const name of MODULE_NAMES) {
|
|
295
|
+
const moduleValue = value[name];
|
|
296
|
+
if (moduleValue === undefined) continue;
|
|
297
|
+
if (!isRecord(moduleValue)) {
|
|
298
|
+
diagnostics.push(typeDiagnostic(name, "table"));
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
normalizeModule(name, moduleValue, config, diagnostics);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
validateFormatVariables(
|
|
305
|
+
config.formatAst,
|
|
306
|
+
new Set([...MODULE_NAMES, "all"]),
|
|
307
|
+
"format",
|
|
308
|
+
diagnostics,
|
|
309
|
+
);
|
|
310
|
+
validateStyleVariables(config.formatAst, new Set(), "format", diagnostics);
|
|
311
|
+
const palette = activePalette(config);
|
|
312
|
+
for (const name of MODULE_NAMES) {
|
|
313
|
+
const module = config.modules[name];
|
|
314
|
+
validateFormatVariables(
|
|
315
|
+
module.formatAst,
|
|
316
|
+
new Set(MODULE_CONTENT_VARIABLES[name]),
|
|
317
|
+
`${name}.format`,
|
|
318
|
+
diagnostics,
|
|
319
|
+
);
|
|
320
|
+
validateStyleVariables(module.formatAst, new Set(["style"]), `${name}.format`, diagnostics);
|
|
321
|
+
if (!isValidStyle(module.style, palette)) {
|
|
322
|
+
diagnostics.push(
|
|
323
|
+
diagnostic(
|
|
324
|
+
"warning",
|
|
325
|
+
`${name}.style`,
|
|
326
|
+
`Invalid style ${JSON.stringify(module.style)}; using the module default`,
|
|
327
|
+
),
|
|
328
|
+
);
|
|
329
|
+
module.style = BUILT_IN_CONFIG.modules[name].style;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return { config, diagnostics };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function normalizeModule(
|
|
337
|
+
name: ModuleName,
|
|
338
|
+
value: Record<string, unknown>,
|
|
339
|
+
config: StarshipConfig,
|
|
340
|
+
diagnostics: ConfigDiagnostic[],
|
|
341
|
+
) {
|
|
342
|
+
const known = new Set(["format", "symbol", "style", "disabled"]);
|
|
343
|
+
if (name === "extension_status") {
|
|
344
|
+
known.add("separator");
|
|
345
|
+
known.add("max_statuses");
|
|
346
|
+
known.add("icons");
|
|
347
|
+
}
|
|
348
|
+
for (const key of Object.keys(value)) {
|
|
349
|
+
if (!known.has(key)) diagnostics.push(unknownDiagnostic(`${name}.${key}`));
|
|
350
|
+
}
|
|
351
|
+
const module = config.modules[name];
|
|
352
|
+
if (value.format !== undefined) {
|
|
353
|
+
if (typeof value.format !== "string")
|
|
354
|
+
diagnostics.push(typeDiagnostic(`${name}.format`, "string"));
|
|
355
|
+
else {
|
|
356
|
+
try {
|
|
357
|
+
module.formatAst = parseFormat(value.format);
|
|
358
|
+
module.format = value.format;
|
|
359
|
+
} catch (error) {
|
|
360
|
+
diagnostics.push(
|
|
361
|
+
diagnostic(
|
|
362
|
+
"warning",
|
|
363
|
+
`${name}.format`,
|
|
364
|
+
`Invalid format; using module default: ${formatError(error)}`,
|
|
365
|
+
),
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
for (const field of ["symbol", "style"] as const) {
|
|
371
|
+
if (value[field] === undefined) continue;
|
|
372
|
+
if (typeof value[field] !== "string") {
|
|
373
|
+
diagnostics.push(typeDiagnostic(`${name}.${field}`, "string"));
|
|
374
|
+
} else module[field] = value[field];
|
|
375
|
+
}
|
|
376
|
+
if (value.disabled !== undefined) {
|
|
377
|
+
if (typeof value.disabled !== "boolean") {
|
|
378
|
+
diagnostics.push(typeDiagnostic(`${name}.disabled`, "boolean"));
|
|
379
|
+
} else module.disabled = value.disabled;
|
|
380
|
+
}
|
|
381
|
+
if (name !== "extension_status") return;
|
|
382
|
+
if (value.separator !== undefined) {
|
|
383
|
+
if (typeof value.separator !== "string") {
|
|
384
|
+
diagnostics.push(typeDiagnostic("extension_status.separator", "string"));
|
|
385
|
+
} else config.extensionStatus.separator = value.separator;
|
|
386
|
+
}
|
|
387
|
+
if (value.max_statuses !== undefined) {
|
|
388
|
+
if (
|
|
389
|
+
typeof value.max_statuses !== "number" ||
|
|
390
|
+
!Number.isInteger(value.max_statuses) ||
|
|
391
|
+
value.max_statuses < 0 ||
|
|
392
|
+
value.max_statuses > 100
|
|
393
|
+
) {
|
|
394
|
+
diagnostics.push(
|
|
395
|
+
diagnostic(
|
|
396
|
+
"warning",
|
|
397
|
+
"extension_status.max_statuses",
|
|
398
|
+
"Expected an integer from 0 through 100",
|
|
399
|
+
),
|
|
400
|
+
);
|
|
401
|
+
} else config.extensionStatus.maxStatuses = value.max_statuses;
|
|
402
|
+
}
|
|
403
|
+
if (value.icons !== undefined) {
|
|
404
|
+
if (!isRecord(value.icons)) diagnostics.push(typeDiagnostic("extension_status.icons", "table"));
|
|
405
|
+
else {
|
|
406
|
+
config.extensionStatus.icons = Object.fromEntries(
|
|
407
|
+
Object.entries(value.icons).flatMap(([key, icon]) => {
|
|
408
|
+
if (typeof icon === "string") return [[key, icon]];
|
|
409
|
+
diagnostics.push(typeDiagnostic(`extension_status.icons.${key}`, "string"));
|
|
410
|
+
return [];
|
|
411
|
+
}),
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
interface AtomicFileSystem {
|
|
418
|
+
mkdirSync: typeof mkdirSync;
|
|
419
|
+
writeFileSync: typeof writeFileSync;
|
|
420
|
+
renameSync: typeof renameSync;
|
|
421
|
+
rmSync: typeof rmSync;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function atomicSaveConfigDocument(
|
|
425
|
+
settingsPath: string,
|
|
426
|
+
rawDocument: string,
|
|
427
|
+
overrides: Partial<AtomicFileSystem> = {},
|
|
428
|
+
): LoadedStarshipConfig {
|
|
429
|
+
let parsed: TomlTable;
|
|
430
|
+
try {
|
|
431
|
+
parsed = parse(rawDocument);
|
|
432
|
+
} catch (error) {
|
|
433
|
+
throw new Error(`Unable to parse TOML: ${formatError(error)}`);
|
|
434
|
+
}
|
|
435
|
+
const normalized = normalizeConfig(parsed);
|
|
436
|
+
if (normalized.diagnostics.some((item) => item.severity === "error")) {
|
|
437
|
+
throw new Error(normalized.diagnostics.map((item) => item.message).join("\n"));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const fs = { mkdirSync, writeFileSync, renameSync, rmSync, ...overrides };
|
|
441
|
+
fs.mkdirSync(dirname(settingsPath), { recursive: true });
|
|
442
|
+
const tempPath = join(dirname(settingsPath), `.${CONFIG_FILE_NAME}.${randomUUID()}.tmp`);
|
|
443
|
+
try {
|
|
444
|
+
fs.writeFileSync(tempPath, rawDocument, { encoding: "utf8", flag: "wx" });
|
|
445
|
+
fs.renameSync(tempPath, settingsPath);
|
|
446
|
+
} finally {
|
|
447
|
+
try {
|
|
448
|
+
fs.rmSync(tempPath, { force: true });
|
|
449
|
+
} catch {
|
|
450
|
+
// Best-effort cleanup must not replace the original save error.
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
...normalized,
|
|
455
|
+
source: "user",
|
|
456
|
+
settingsPath,
|
|
457
|
+
rawDocument,
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function cloneBuiltInConfig(): StarshipConfig {
|
|
462
|
+
return {
|
|
463
|
+
...BUILT_IN_CONFIG,
|
|
464
|
+
formatAst: structuredClone(BUILT_IN_CONFIG.formatAst),
|
|
465
|
+
palettes: Object.fromEntries(
|
|
466
|
+
Object.entries(BUILT_IN_CONFIG.palettes).map(([name, colors]) => [name, { ...colors }]),
|
|
467
|
+
),
|
|
468
|
+
modules: Object.fromEntries(
|
|
469
|
+
MODULE_NAMES.map((name) => [
|
|
470
|
+
name,
|
|
471
|
+
{
|
|
472
|
+
...BUILT_IN_CONFIG.modules[name],
|
|
473
|
+
formatAst: structuredClone(BUILT_IN_CONFIG.modules[name].formatAst),
|
|
474
|
+
},
|
|
475
|
+
]),
|
|
476
|
+
) as Record<ModuleName, ModuleConfig>,
|
|
477
|
+
extensionStatus: {
|
|
478
|
+
...BUILT_IN_CONFIG.extensionStatus,
|
|
479
|
+
icons: { ...BUILT_IN_CONFIG.extensionStatus.icons },
|
|
480
|
+
},
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function validateFormatVariables(
|
|
485
|
+
ast: readonly FormatNode[],
|
|
486
|
+
allowed: ReadonlySet<string>,
|
|
487
|
+
path: string,
|
|
488
|
+
diagnostics: ConfigDiagnostic[],
|
|
489
|
+
) {
|
|
490
|
+
for (const variable of formatVariables(ast)) {
|
|
491
|
+
if (allowed.has(variable)) continue;
|
|
492
|
+
diagnostics.push(
|
|
493
|
+
diagnostic(
|
|
494
|
+
"warning",
|
|
495
|
+
path,
|
|
496
|
+
`Unknown variable ${JSON.stringify(variable)} in ${path} was ignored`,
|
|
497
|
+
),
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function validateStyleVariables(
|
|
503
|
+
ast: readonly FormatNode[],
|
|
504
|
+
allowed: ReadonlySet<string>,
|
|
505
|
+
path: string,
|
|
506
|
+
diagnostics: ConfigDiagnostic[],
|
|
507
|
+
) {
|
|
508
|
+
for (const variable of styleVariables(ast)) {
|
|
509
|
+
if (allowed.has(variable)) continue;
|
|
510
|
+
diagnostics.push(
|
|
511
|
+
diagnostic(
|
|
512
|
+
"warning",
|
|
513
|
+
path,
|
|
514
|
+
`Unknown style variable ${JSON.stringify(variable)} in ${path} was ignored`,
|
|
515
|
+
),
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function setOwn<T>(record: Record<string, T>, key: string, value: T) {
|
|
521
|
+
Object.defineProperty(record, key, {
|
|
522
|
+
value,
|
|
523
|
+
writable: true,
|
|
524
|
+
enumerable: true,
|
|
525
|
+
configurable: true,
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
530
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function typeDiagnostic(path: string, type: string): ConfigDiagnostic {
|
|
534
|
+
return diagnostic("warning", path, `Expected ${type}; using the default value`);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function unknownDiagnostic(path: string): ConfigDiagnostic {
|
|
538
|
+
return diagnostic("warning", path, `Unknown setting ${JSON.stringify(path)} was ignored`);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function diagnostic(
|
|
542
|
+
severity: ConfigDiagnostic["severity"],
|
|
543
|
+
path: string,
|
|
544
|
+
message: string,
|
|
545
|
+
): ConfigDiagnostic {
|
|
546
|
+
return { severity, path, message };
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function isAlreadyExistsError(error: unknown): boolean {
|
|
550
|
+
return error instanceof Error && "code" in error && error.code === "EEXIST";
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function formatError(error: unknown): string {
|
|
554
|
+
return error instanceof Error ? error.message : String(error);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
export function activePalette(config: StarshipConfig): ColorPalette {
|
|
558
|
+
return {
|
|
559
|
+
...ownPalette(config.palettes, BUILT_IN_CONFIG.palette),
|
|
560
|
+
...ownPalette(config.palettes, config.palette),
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function ownPalette(
|
|
565
|
+
palettes: Readonly<Record<string, Record<string, string>>>,
|
|
566
|
+
name: string,
|
|
567
|
+
): Record<string, string> | undefined {
|
|
568
|
+
return Object.hasOwn(palettes, name) ? palettes[name] : undefined;
|
|
569
|
+
}
|