@narumitw/pi-starship 0.55.0 → 0.55.2

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.
Files changed (101) hide show
  1. package/README.md +21 -36
  2. package/dist/chunks/{chunk-UX6PBXVK.js → chunk-JO5PH6FN.js} +28 -110
  3. package/dist/chunks/chunk-JO5PH6FN.js.map +7 -0
  4. package/dist/chunks/{chunk-P4XMFSQW.js → chunk-YWY5RIDQ.js} +3 -7
  5. package/dist/chunks/chunk-YWY5RIDQ.js.map +7 -0
  6. package/dist/chunks/{chunk-EOGXAMTA.js → chunk-ZQZSXXPI.js} +1 -1
  7. package/dist/chunks/chunk-ZQZSXXPI.js.map +7 -0
  8. package/dist/chunks/{cloud-BKHYWCTA.js → cloud-C4VU3TX3.js} +7 -14
  9. package/dist/chunks/cloud-C4VU3TX3.js.map +7 -0
  10. package/dist/chunks/{commands-53ZO2JUK.js → commands-ATILSUVK.js} +13 -42
  11. package/dist/chunks/commands-ATILSUVK.js.map +7 -0
  12. package/dist/chunks/{deployment-RKKOEQQ2.js → deployment-FMNTYRBW.js} +6 -21
  13. package/dist/chunks/deployment-FMNTYRBW.js.map +7 -0
  14. package/dist/chunks/{development-BERD42HW.js → development-OUYXDPBS.js} +4 -8
  15. package/dist/chunks/development-OUYXDPBS.js.map +7 -0
  16. package/dist/chunks/{execution-42JTEY3N.js → execution-HJOGLMWD.js} +3 -3
  17. package/dist/chunks/execution-HJOGLMWD.js.map +7 -0
  18. package/dist/chunks/{languages-RMP6DW7H.js → languages-WXMF4GOF.js} +6 -22
  19. package/dist/chunks/languages-WXMF4GOF.js.map +7 -0
  20. package/dist/chunks/{package-LG5H7BBM.js → package-YXRZQAOV.js} +5 -14
  21. package/dist/chunks/package-YXRZQAOV.js.map +7 -0
  22. package/dist/index.ts +38 -53
  23. package/dist/index.ts.map +2 -2
  24. package/package.json +60 -61
  25. package/skills/configuring-pi-starship/references/runtime-and-security.md +5 -1
  26. package/skills/configuring-pi-starship/scripts/apply.mjs +43 -47
  27. package/skills/configuring-pi-starship/scripts/config-path.mjs +3 -3
  28. package/skills/configuring-pi-starship/scripts/script-support.mjs +6 -6
  29. package/skills/configuring-pi-starship/scripts/validate.mjs +18 -18
  30. package/src/command-configuration.ts +267 -286
  31. package/src/command-contract.ts +6 -6
  32. package/src/command-inspector.ts +27 -27
  33. package/src/command-preset-picker.ts +45 -50
  34. package/src/command-preview.ts +153 -166
  35. package/src/commands.ts +588 -618
  36. package/src/config.ts +580 -660
  37. package/src/effective-config.ts +46 -48
  38. package/src/format/formatter.ts +211 -227
  39. package/src/format/style.ts +205 -216
  40. package/src/modules/activity.ts +74 -74
  41. package/src/modules/brand.ts +9 -9
  42. package/src/modules/cache.ts +18 -18
  43. package/src/modules/catalog.ts +79 -83
  44. package/src/modules/cloud.ts +54 -54
  45. package/src/modules/context.ts +27 -27
  46. package/src/modules/cost.ts +23 -23
  47. package/src/modules/deployment.ts +45 -45
  48. package/src/modules/development.ts +80 -81
  49. package/src/modules/directory.ts +89 -113
  50. package/src/modules/display.ts +9 -9
  51. package/src/modules/execution.ts +62 -69
  52. package/src/modules/extension-status.ts +59 -64
  53. package/src/modules/fill.ts +10 -10
  54. package/src/modules/git/branch.ts +26 -26
  55. package/src/modules/git/commit.ts +21 -23
  56. package/src/modules/git/metrics.ts +22 -22
  57. package/src/modules/git/runtime.ts +299 -334
  58. package/src/modules/git/state.ts +17 -17
  59. package/src/modules/git/status.ts +77 -79
  60. package/src/modules/git/worktree.ts +10 -12
  61. package/src/modules/github-pr.ts +20 -20
  62. package/src/modules/helpers.ts +3 -3
  63. package/src/modules/index.ts +21 -21
  64. package/src/modules/inspection.ts +101 -108
  65. package/src/modules/languages.ts +52 -59
  66. package/src/modules/model.ts +57 -67
  67. package/src/modules/package.ts +12 -12
  68. package/src/modules/provider.ts +22 -24
  69. package/src/modules/render.ts +124 -140
  70. package/src/modules/thinking.ts +27 -26
  71. package/src/modules/time.ts +10 -13
  72. package/src/modules/tokens.ts +13 -13
  73. package/src/modules/truncation.ts +14 -17
  74. package/src/modules/turn.ts +9 -9
  75. package/src/modules/types.ts +125 -132
  76. package/src/modules/workspace-helpers.ts +4 -7
  77. package/src/pi-starship.ts +567 -571
  78. package/src/presets/catalog.ts +113 -113
  79. package/src/runtime/cloud.ts +122 -150
  80. package/src/runtime/command.ts +106 -108
  81. package/src/runtime/deployment.ts +150 -179
  82. package/src/runtime/development.ts +107 -124
  83. package/src/runtime/execution.ts +110 -124
  84. package/src/runtime/github-pr.ts +223 -252
  85. package/src/runtime/helpers.ts +186 -199
  86. package/src/runtime/languages.ts +149 -180
  87. package/src/runtime/package.ts +74 -87
  88. package/src/runtime/refresh-controller.ts +82 -86
  89. package/src/runtime/types.ts +38 -38
  90. package/src/runtime/workspace.ts +108 -116
  91. package/src/usage.ts +40 -40
  92. package/dist/chunks/chunk-EOGXAMTA.js.map +0 -7
  93. package/dist/chunks/chunk-P4XMFSQW.js.map +0 -7
  94. package/dist/chunks/chunk-UX6PBXVK.js.map +0 -7
  95. package/dist/chunks/cloud-BKHYWCTA.js.map +0 -7
  96. package/dist/chunks/commands-53ZO2JUK.js.map +0 -7
  97. package/dist/chunks/deployment-RKKOEQQ2.js.map +0 -7
  98. package/dist/chunks/development-BERD42HW.js.map +0 -7
  99. package/dist/chunks/execution-42JTEY3N.js.map +0 -7
  100. package/dist/chunks/languages-RMP6DW7H.js.map +0 -7
  101. package/dist/chunks/package-LG5H7BBM.js.map +0 -7
@@ -4,120 +4,118 @@ import type { WorkspaceExec } from "./types.js";
4
4
  const FORCE_KILL_DELAY_MS = 250;
5
5
 
6
6
  export const execWorkspaceCommand: WorkspaceExec = async (command, args, options) =>
7
- new Promise((resolve) => {
8
- if (options.signal?.aborted) {
9
- resolve({ stdout: "", stderr: "", code: 1, killed: true });
10
- return;
11
- }
7
+ new Promise((resolve) => {
8
+ if (options.signal?.aborted) {
9
+ resolve({ stdout: "", stderr: "", code: 1, killed: true });
10
+ return;
11
+ }
12
12
 
13
- let child: ReturnType<typeof spawn>;
14
- try {
15
- child = spawn(command, args, {
16
- cwd: options.cwd,
17
- detached: process.platform !== "win32",
18
- env: explicitEnvironment(options.environment),
19
- shell: false,
20
- stdio: ["ignore", "pipe", "pipe"],
21
- windowsHide: true,
22
- });
23
- } catch {
24
- resolve({ stdout: "", stderr: "", code: 1, killed: false });
25
- return;
26
- }
13
+ let child: ReturnType<typeof spawn>;
14
+ try {
15
+ child = spawn(command, args, {
16
+ cwd: options.cwd,
17
+ detached: process.platform !== "win32",
18
+ env: explicitEnvironment(options.environment),
19
+ shell: false,
20
+ stdio: ["ignore", "pipe", "pipe"],
21
+ windowsHide: true,
22
+ });
23
+ } catch {
24
+ resolve({ stdout: "", stderr: "", code: 1, killed: false });
25
+ return;
26
+ }
27
27
 
28
- const stdout: Buffer[] = [];
29
- const stderr: Buffer[] = [];
30
- let outputBytes = 0;
31
- let killed = false;
32
- let settled = false;
33
- let forceKillTimer: NodeJS.Timeout | undefined;
34
- let timeoutTimer: NodeJS.Timeout | undefined;
28
+ const stdout: Buffer[] = [];
29
+ const stderr: Buffer[] = [];
30
+ let outputBytes = 0;
31
+ let killed = false;
32
+ let settled = false;
33
+ let forceKillTimer: NodeJS.Timeout | undefined;
34
+ let timeoutTimer: NodeJS.Timeout | undefined;
35
35
 
36
- const finish = (code: number) => {
37
- if (settled) return;
38
- settled = true;
39
- if (timeoutTimer) clearTimeout(timeoutTimer);
40
- if (forceKillTimer) clearTimeout(forceKillTimer);
41
- options.signal?.removeEventListener("abort", terminate);
42
- resolve({
43
- stdout: Buffer.concat(stdout).toString("utf8"),
44
- stderr: Buffer.concat(stderr).toString("utf8"),
45
- code,
46
- killed,
47
- });
48
- };
36
+ const finish = (code: number) => {
37
+ if (settled) return;
38
+ settled = true;
39
+ if (timeoutTimer) clearTimeout(timeoutTimer);
40
+ if (forceKillTimer) clearTimeout(forceKillTimer);
41
+ options.signal?.removeEventListener("abort", terminate);
42
+ resolve({
43
+ stdout: Buffer.concat(stdout).toString("utf8"),
44
+ stderr: Buffer.concat(stderr).toString("utf8"),
45
+ code,
46
+ killed,
47
+ });
48
+ };
49
49
 
50
- const signalProcessTree = (signal: NodeJS.Signals) => {
51
- const pid = child.pid;
52
- if (pid === undefined) return;
53
- if (process.platform !== "win32") {
54
- try {
55
- process.kill(-pid, signal);
56
- return;
57
- } catch {
58
- child.kill(signal);
59
- return;
60
- }
61
- }
62
- try {
63
- const killer = spawn("taskkill.exe", ["/pid", String(pid), "/t", "/f"], {
64
- stdio: "ignore",
65
- windowsHide: true,
66
- });
67
- killer.once("error", () => child.kill(signal));
68
- killer.unref();
69
- } catch {
70
- child.kill(signal);
71
- }
72
- };
50
+ const signalProcessTree = (signal: NodeJS.Signals) => {
51
+ const pid = child.pid;
52
+ if (pid === undefined) return;
53
+ if (process.platform !== "win32") {
54
+ try {
55
+ process.kill(-pid, signal);
56
+ return;
57
+ } catch {
58
+ child.kill(signal);
59
+ return;
60
+ }
61
+ }
62
+ try {
63
+ const killer = spawn("taskkill.exe", ["/pid", String(pid), "/t", "/f"], {
64
+ stdio: "ignore",
65
+ windowsHide: true,
66
+ });
67
+ killer.once("error", () => child.kill(signal));
68
+ killer.unref();
69
+ } catch {
70
+ child.kill(signal);
71
+ }
72
+ };
73
73
 
74
- function terminate() {
75
- if (killed || settled) return;
76
- killed = true;
77
- signalProcessTree("SIGTERM");
78
- forceKillTimer = setTimeout(() => {
79
- signalProcessTree("SIGKILL");
80
- child.stdout?.destroy();
81
- child.stderr?.destroy();
82
- finish(1);
83
- }, FORCE_KILL_DELAY_MS);
84
- }
74
+ function terminate() {
75
+ if (killed || settled) return;
76
+ killed = true;
77
+ signalProcessTree("SIGTERM");
78
+ forceKillTimer = setTimeout(() => {
79
+ signalProcessTree("SIGKILL");
80
+ child.stdout?.destroy();
81
+ child.stderr?.destroy();
82
+ finish(1);
83
+ }, FORCE_KILL_DELAY_MS);
84
+ }
85
85
 
86
- const collect = (target: Buffer[]) => (data: Buffer | string) => {
87
- const chunk = Buffer.isBuffer(data) ? data : Buffer.from(data);
88
- const remaining = Math.max(0, options.maxOutputBytes - outputBytes);
89
- if (remaining > 0) target.push(chunk.subarray(0, remaining));
90
- outputBytes += chunk.byteLength;
91
- if (outputBytes > options.maxOutputBytes) terminate();
92
- };
86
+ const collect = (target: Buffer[]) => (data: Buffer | string) => {
87
+ const chunk = Buffer.isBuffer(data) ? data : Buffer.from(data);
88
+ const remaining = Math.max(0, options.maxOutputBytes - outputBytes);
89
+ if (remaining > 0) target.push(chunk.subarray(0, remaining));
90
+ outputBytes += chunk.byteLength;
91
+ if (outputBytes > options.maxOutputBytes) terminate();
92
+ };
93
93
 
94
- child.stdout?.on("data", collect(stdout));
95
- child.stderr?.on("data", collect(stderr));
96
- child.once("error", () => {
97
- if (!killed) finish(1);
98
- });
99
- child.once("close", (code) => {
100
- if (!killed) finish(code ?? 1);
101
- });
102
- options.signal?.addEventListener("abort", terminate, { once: true });
103
- if (options.signal?.aborted) terminate();
104
- timeoutTimer = setTimeout(terminate, options.timeout);
105
- });
94
+ child.stdout?.on("data", collect(stdout));
95
+ child.stderr?.on("data", collect(stderr));
96
+ child.once("error", () => {
97
+ if (!killed) finish(1);
98
+ });
99
+ child.once("close", (code) => {
100
+ if (!killed) finish(code ?? 1);
101
+ });
102
+ options.signal?.addEventListener("abort", terminate, { once: true });
103
+ if (options.signal?.aborted) terminate();
104
+ timeoutTimer = setTimeout(terminate, options.timeout);
105
+ });
106
106
 
107
- function explicitEnvironment(
108
- source: Readonly<Record<string, string | undefined>>,
109
- ): NodeJS.ProcessEnv {
110
- const result: NodeJS.ProcessEnv = Object.create(null);
111
- for (const [name, value] of Object.entries(source)) {
112
- if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name) || value === undefined || value.includes("\0")) {
113
- continue;
114
- }
115
- Object.defineProperty(result, name, {
116
- value,
117
- writable: true,
118
- enumerable: true,
119
- configurable: true,
120
- });
121
- }
122
- return result;
107
+ function explicitEnvironment(source: Readonly<Record<string, string | undefined>>): NodeJS.ProcessEnv {
108
+ const result: NodeJS.ProcessEnv = Object.create(null);
109
+ for (const [name, value] of Object.entries(source)) {
110
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name) || value === undefined || value.includes("\0")) {
111
+ continue;
112
+ }
113
+ Object.defineProperty(result, name, {
114
+ value,
115
+ writable: true,
116
+ enumerable: true,
117
+ configurable: true,
118
+ });
119
+ }
120
+ return result;
123
121
  }
@@ -1,201 +1,172 @@
1
1
  import { extname, isAbsolute, join, resolve } from "node:path";
2
2
  import { parse as parseYaml } from "yaml";
3
3
  import {
4
- directMatch,
5
- exactAlias,
6
- formatVersion,
7
- isRecord,
8
- joinHome,
9
- MAX_METADATA_FILE_BYTES,
10
- optionBoolean,
11
- optionMap,
12
- optionNumber,
13
- optionString,
14
- optionStrings,
15
- runBounded,
16
- safeMetadata,
4
+ directMatch,
5
+ exactAlias,
6
+ formatVersion,
7
+ isRecord,
8
+ joinHome,
9
+ MAX_METADATA_FILE_BYTES,
10
+ optionBoolean,
11
+ optionMap,
12
+ optionNumber,
13
+ optionString,
14
+ optionStrings,
15
+ runBounded,
16
+ safeMetadata,
17
17
  } from "./helpers.js";
18
18
  import type { CollectorContext, MutableModuleSnapshot } from "./types.js";
19
19
 
20
20
  export async function collectDeployment(context: CollectorContext): Promise<MutableModuleSnapshot> {
21
- const result: MutableModuleSnapshot = {};
22
- if (context.needs("docker_context")) {
23
- const docker = await collectDocker(context);
24
- if (docker) result.docker_context = docker;
25
- }
26
- if (context.needs("kubernetes")) {
27
- const kubernetes = await collectKubernetes(context);
28
- if (kubernetes) result.kubernetes = kubernetes;
29
- }
30
- if (context.needs("terraform")) {
31
- const terraform = await collectTerraform(context);
32
- if (terraform) result.terraform = terraform;
33
- }
34
- return result;
21
+ const result: MutableModuleSnapshot = {};
22
+ if (context.needs("docker_context")) {
23
+ const docker = await collectDocker(context);
24
+ if (docker) result.docker_context = docker;
25
+ }
26
+ if (context.needs("kubernetes")) {
27
+ const kubernetes = await collectKubernetes(context);
28
+ if (kubernetes) result.kubernetes = kubernetes;
29
+ }
30
+ if (context.needs("terraform")) {
31
+ const terraform = await collectTerraform(context);
32
+ if (terraform) result.terraform = terraform;
33
+ }
34
+ return result;
35
35
  }
36
36
 
37
- async function collectDocker(
38
- context: CollectorContext,
39
- ): Promise<Record<string, string> | undefined> {
40
- const entries = await context.entries();
41
- if (optionBoolean(context, "docker_context", "only_with_files")) {
42
- const configuredFiles = optionStrings(context, "docker_context", "detect_files");
43
- if (
44
- !directMatch(
45
- entries,
46
- configuredFiles.length > 0
47
- ? configuredFiles
48
- : [
49
- "Dockerfile",
50
- "docker-compose.yml",
51
- "docker-compose.yaml",
52
- "compose.yml",
53
- "compose.yaml",
54
- ],
55
- optionStrings(context, "docker_context", "detect_extensions"),
56
- optionStrings(context, "docker_context", "detect_folders"),
57
- )
58
- ) {
59
- return undefined;
60
- }
61
- }
62
- let active = safeMetadata(context.input.environment.DOCKER_CONTEXT);
63
- if (!active) {
64
- const configDirectory =
65
- safeMetadata(context.input.environment.DOCKER_CONFIG, 1_024) ??
66
- joinHome(context.input, ".docker");
67
- const source = await context.fs.readFile(
68
- join(configDirectory, "config.json"),
69
- MAX_METADATA_FILE_BYTES,
70
- );
71
- if (source) {
72
- try {
73
- const document = JSON.parse(source) as unknown;
74
- active = isRecord(document) ? safeMetadata(document.currentContext) : undefined;
75
- } catch {
76
- // Malformed metadata degrades to an empty module.
77
- }
78
- }
79
- }
80
- return active && active !== "default" ? { context: active } : undefined;
37
+ async function collectDocker(context: CollectorContext): Promise<Record<string, string> | undefined> {
38
+ const entries = await context.entries();
39
+ if (optionBoolean(context, "docker_context", "only_with_files")) {
40
+ const configuredFiles = optionStrings(context, "docker_context", "detect_files");
41
+ if (
42
+ !directMatch(
43
+ entries,
44
+ configuredFiles.length > 0
45
+ ? configuredFiles
46
+ : ["Dockerfile", "docker-compose.yml", "docker-compose.yaml", "compose.yml", "compose.yaml"],
47
+ optionStrings(context, "docker_context", "detect_extensions"),
48
+ optionStrings(context, "docker_context", "detect_folders"),
49
+ )
50
+ ) {
51
+ return undefined;
52
+ }
53
+ }
54
+ let active = safeMetadata(context.input.environment.DOCKER_CONTEXT);
55
+ if (!active) {
56
+ const configDirectory =
57
+ safeMetadata(context.input.environment.DOCKER_CONFIG, 1_024) ?? joinHome(context.input, ".docker");
58
+ const source = await context.fs.readFile(join(configDirectory, "config.json"), MAX_METADATA_FILE_BYTES);
59
+ if (source) {
60
+ try {
61
+ const document = JSON.parse(source) as unknown;
62
+ active = isRecord(document) ? safeMetadata(document.currentContext) : undefined;
63
+ } catch {
64
+ // Malformed metadata degrades to an empty module.
65
+ }
66
+ }
67
+ }
68
+ return active && active !== "default" ? { context: active } : undefined;
81
69
  }
82
70
 
83
- async function collectKubernetes(
84
- context: CollectorContext,
85
- ): Promise<Record<string, string> | undefined> {
86
- const selected = safeMetadata(context.input.environment.KUBECONFIG, 4_096);
87
- const candidates = selected
88
- ? selected.split(context.input.platform === "win32" ? ";" : ":").filter(Boolean)
89
- : [joinHome(context.input, ".kube", "config")];
90
- const limit = optionNumber(context, "kubernetes", "max_config_files") ?? 8;
91
- const contexts = new Map<string, Record<string, unknown>>();
92
- const clusters = new Set<string>();
93
- const users = new Set<string>();
94
- let currentContext: string | undefined;
95
- for (const path of candidates.slice(0, limit)) {
96
- const source = await context.fs.readFile(path, MAX_METADATA_FILE_BYTES);
97
- if (!source) continue;
98
- try {
99
- const document = parseYaml(source) as unknown;
100
- if (!isRecord(document)) continue;
101
- currentContext ??= safeMetadata(document["current-context"]);
102
- for (const item of arrayRecords(document.contexts)) {
103
- const name = safeMetadata(item.name);
104
- if (name && !contexts.has(name) && isRecord(item.context)) contexts.set(name, item.context);
105
- }
106
- for (const item of arrayRecords(document.clusters)) {
107
- const name = safeMetadata(item.name);
108
- if (name) clusters.add(name);
109
- }
110
- for (const item of arrayRecords(document.users)) {
111
- const name = safeMetadata(item.name);
112
- if (name) users.add(name);
113
- }
114
- } catch {
115
- // Ignore malformed files independently.
116
- }
117
- }
118
- if (!currentContext) return undefined;
119
- const selectedContext = contexts.get(currentContext);
120
- const cluster = safeMetadata(selectedContext?.cluster);
121
- const user = safeMetadata(selectedContext?.user);
122
- const namespace = safeMetadata(selectedContext?.namespace) ?? "default";
123
- const values: Record<string, string> = {
124
- context:
125
- exactAlias(currentContext, optionMap(context, "kubernetes", "context_aliases")) ??
126
- currentContext,
127
- namespace:
128
- exactAlias(namespace, optionMap(context, "kubernetes", "namespace_aliases")) ?? namespace,
129
- };
130
- if (cluster && clusters.has(cluster)) {
131
- values.cluster =
132
- exactAlias(cluster, optionMap(context, "kubernetes", "cluster_aliases")) ?? cluster;
133
- }
134
- if (user && users.has(user)) {
135
- values.user = exactAlias(user, optionMap(context, "kubernetes", "user_aliases")) ?? user;
136
- }
137
- return values;
71
+ async function collectKubernetes(context: CollectorContext): Promise<Record<string, string> | undefined> {
72
+ const selected = safeMetadata(context.input.environment.KUBECONFIG, 4_096);
73
+ const candidates = selected
74
+ ? selected.split(context.input.platform === "win32" ? ";" : ":").filter(Boolean)
75
+ : [joinHome(context.input, ".kube", "config")];
76
+ const limit = optionNumber(context, "kubernetes", "max_config_files") ?? 8;
77
+ const contexts = new Map<string, Record<string, unknown>>();
78
+ const clusters = new Set<string>();
79
+ const users = new Set<string>();
80
+ let currentContext: string | undefined;
81
+ for (const path of candidates.slice(0, limit)) {
82
+ const source = await context.fs.readFile(path, MAX_METADATA_FILE_BYTES);
83
+ if (!source) continue;
84
+ try {
85
+ const document = parseYaml(source) as unknown;
86
+ if (!isRecord(document)) continue;
87
+ currentContext ??= safeMetadata(document["current-context"]);
88
+ for (const item of arrayRecords(document.contexts)) {
89
+ const name = safeMetadata(item.name);
90
+ if (name && !contexts.has(name) && isRecord(item.context)) contexts.set(name, item.context);
91
+ }
92
+ for (const item of arrayRecords(document.clusters)) {
93
+ const name = safeMetadata(item.name);
94
+ if (name) clusters.add(name);
95
+ }
96
+ for (const item of arrayRecords(document.users)) {
97
+ const name = safeMetadata(item.name);
98
+ if (name) users.add(name);
99
+ }
100
+ } catch {
101
+ // Ignore malformed files independently.
102
+ }
103
+ }
104
+ if (!currentContext) return undefined;
105
+ const selectedContext = contexts.get(currentContext);
106
+ const cluster = safeMetadata(selectedContext?.cluster);
107
+ const user = safeMetadata(selectedContext?.user);
108
+ const namespace = safeMetadata(selectedContext?.namespace) ?? "default";
109
+ const values: Record<string, string> = {
110
+ context: exactAlias(currentContext, optionMap(context, "kubernetes", "context_aliases")) ?? currentContext,
111
+ namespace: exactAlias(namespace, optionMap(context, "kubernetes", "namespace_aliases")) ?? namespace,
112
+ };
113
+ if (cluster && clusters.has(cluster)) {
114
+ values.cluster = exactAlias(cluster, optionMap(context, "kubernetes", "cluster_aliases")) ?? cluster;
115
+ }
116
+ if (user && users.has(user)) {
117
+ values.user = exactAlias(user, optionMap(context, "kubernetes", "user_aliases")) ?? user;
118
+ }
119
+ return values;
138
120
  }
139
121
 
140
- async function collectTerraform(
141
- context: CollectorContext,
142
- ): Promise<Record<string, string> | undefined> {
143
- const entries = await context.entries();
144
- const configuredFiles = optionStrings(context, "terraform", "detect_files");
145
- const configuredExtensions = optionStrings(context, "terraform", "detect_extensions");
146
- const configuredFolders = optionStrings(context, "terraform", "detect_folders");
147
- const detected =
148
- configuredFiles.length + configuredExtensions.length + configuredFolders.length > 0
149
- ? directMatch(entries, configuredFiles, configuredExtensions, configuredFolders)
150
- : entries.some(
151
- (entry) =>
152
- (entry.isFile && [".tf", ".tfplan", ".tfstate"].includes(extname(entry.name))) ||
153
- (entry.isDirectory && entry.name === ".terraform"),
154
- );
155
- if (!detected) return undefined;
156
- const values: Record<string, string> = {};
157
- const workspace = await terraformWorkspace(context);
158
- if (workspace) values.workspace = workspace;
159
- if (context.needs("terraform", "version")) {
160
- for (const command of ["terraform", "tofu"]) {
161
- const output = await runBounded(context, command, ["version"]);
162
- const parsed = output ? parseTerraformVersion(output, command) : undefined;
163
- if (parsed) {
164
- values.version = formatVersion(
165
- parsed,
166
- optionString(context, "terraform", "version_format"),
167
- );
168
- break;
169
- }
170
- }
171
- }
172
- return values;
122
+ async function collectTerraform(context: CollectorContext): Promise<Record<string, string> | undefined> {
123
+ const entries = await context.entries();
124
+ const configuredFiles = optionStrings(context, "terraform", "detect_files");
125
+ const configuredExtensions = optionStrings(context, "terraform", "detect_extensions");
126
+ const configuredFolders = optionStrings(context, "terraform", "detect_folders");
127
+ const detected =
128
+ configuredFiles.length + configuredExtensions.length + configuredFolders.length > 0
129
+ ? directMatch(entries, configuredFiles, configuredExtensions, configuredFolders)
130
+ : entries.some(
131
+ (entry) =>
132
+ (entry.isFile && [".tf", ".tfplan", ".tfstate"].includes(extname(entry.name))) ||
133
+ (entry.isDirectory && entry.name === ".terraform"),
134
+ );
135
+ if (!detected) return undefined;
136
+ const values: Record<string, string> = {};
137
+ const workspace = await terraformWorkspace(context);
138
+ if (workspace) values.workspace = workspace;
139
+ if (context.needs("terraform", "version")) {
140
+ for (const command of ["terraform", "tofu"]) {
141
+ const output = await runBounded(context, command, ["version"]);
142
+ const parsed = output ? parseTerraformVersion(output, command) : undefined;
143
+ if (parsed) {
144
+ values.version = formatVersion(parsed, optionString(context, "terraform", "version_format"));
145
+ break;
146
+ }
147
+ }
148
+ }
149
+ return values;
173
150
  }
174
151
 
175
152
  async function terraformWorkspace(context: CollectorContext): Promise<string | undefined> {
176
- const direct = safeMetadata(context.input.environment.TF_WORKSPACE);
177
- if (direct) return direct;
178
- const dataDirectory = safeMetadata(context.input.environment.TF_DATA_DIR, 1_024);
179
- const candidate = dataDirectory
180
- ? join(
181
- isAbsolute(dataDirectory) ? dataDirectory : resolve(context.input.cwd, dataDirectory),
182
- "environment",
183
- )
184
- : join(context.input.cwd, ".terraform", "environment");
185
- return safeMetadata(await context.fs.readFile(candidate, 1_024));
153
+ const direct = safeMetadata(context.input.environment.TF_WORKSPACE);
154
+ if (direct) return direct;
155
+ const dataDirectory = safeMetadata(context.input.environment.TF_DATA_DIR, 1_024);
156
+ const candidate = dataDirectory
157
+ ? join(isAbsolute(dataDirectory) ? dataDirectory : resolve(context.input.cwd, dataDirectory), "environment")
158
+ : join(context.input.cwd, ".terraform", "environment");
159
+ return safeMetadata(await context.fs.readFile(candidate, 1_024));
186
160
  }
187
161
 
188
- export function parseTerraformVersion(
189
- output: string,
190
- command: "terraform" | "tofu" | string,
191
- ): string | undefined {
192
- const escaped = command === "tofu" ? "OpenTofu" : "Terraform";
193
- const match = new RegExp(`^${escaped} v?(\\d+\\.\\d+\\.\\d+(?:[-+][0-9A-Za-z.-]+)?)$`, "u").exec(
194
- output.trim().split(/\r?\n/u)[0] ?? "",
195
- );
196
- return match?.[1] ? `v${match[1]}` : undefined;
162
+ export function parseTerraformVersion(output: string, command: "terraform" | "tofu" | string): string | undefined {
163
+ const escaped = command === "tofu" ? "OpenTofu" : "Terraform";
164
+ const match = new RegExp(`^${escaped} v?(\\d+\\.\\d+\\.\\d+(?:[-+][0-9A-Za-z.-]+)?)$`, "u").exec(
165
+ output.trim().split(/\r?\n/u)[0] ?? "",
166
+ );
167
+ return match?.[1] ? `v${match[1]}` : undefined;
197
168
  }
198
169
 
199
170
  function arrayRecords(value: unknown): Record<string, unknown>[] {
200
- return Array.isArray(value) ? value.filter(isRecord) : [];
171
+ return Array.isArray(value) ? value.filter(isRecord) : [];
201
172
  }