@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
@@ -1,205 +1,174 @@
1
1
  import { delimiter, join } from "node:path";
2
2
  import {
3
- directMatch,
4
- formatVersion,
5
- MAX_METADATA_FILE_BYTES,
6
- optionString,
7
- optionStrings,
8
- pathName,
9
- runBounded,
10
- safeMetadata,
3
+ directMatch,
4
+ formatVersion,
5
+ MAX_METADATA_FILE_BYTES,
6
+ optionString,
7
+ optionStrings,
8
+ pathName,
9
+ runBounded,
10
+ safeMetadata,
11
11
  } from "./helpers.js";
12
12
  import type { CollectorContext, MutableModuleSnapshot } from "./types.js";
13
13
 
14
14
  const LANGUAGE_NAMES = ["nodejs", "python", "rust", "golang", "bun", "deno"] as const;
15
15
  type LanguageName = (typeof LANGUAGE_NAMES)[number];
16
16
 
17
- const DEFAULT_DETECTION: Record<
18
- LanguageName,
19
- { files: string[]; extensions: string[]; folders: string[] }
20
- > = {
21
- nodejs: {
22
- files: [
23
- "package.json",
24
- ".node-version",
25
- ".nvmrc",
26
- "!bun.lock",
27
- "!bun.lockb",
28
- "!deno.json",
29
- "!deno.jsonc",
30
- ],
31
- extensions: ["js", "mjs", "cjs", "ts", "mts", "cts"],
32
- folders: ["node_modules"],
33
- },
34
- python: {
35
- files: ["pyproject.toml", "requirements.txt", "Pipfile", "poetry.lock", ".python-version"],
36
- extensions: ["py"],
37
- folders: [".venv", "venv"],
38
- },
39
- rust: { files: ["Cargo.toml"], extensions: ["rs"], folders: [] },
40
- golang: { files: ["go.mod", "go.sum", "go.work"], extensions: ["go"], folders: [] },
41
- bun: { files: ["bun.lock", "bun.lockb", "bunfig.toml"], extensions: [], folders: [] },
42
- deno: { files: ["deno.json", "deno.jsonc", "deno.lock"], extensions: [], folders: [] },
17
+ const DEFAULT_DETECTION: Record<LanguageName, { files: string[]; extensions: string[]; folders: string[] }> = {
18
+ nodejs: {
19
+ files: ["package.json", ".node-version", ".nvmrc", "!bun.lock", "!bun.lockb", "!deno.json", "!deno.jsonc"],
20
+ extensions: ["js", "mjs", "cjs", "ts", "mts", "cts"],
21
+ folders: ["node_modules"],
22
+ },
23
+ python: {
24
+ files: ["pyproject.toml", "requirements.txt", "Pipfile", "poetry.lock", ".python-version"],
25
+ extensions: ["py"],
26
+ folders: [".venv", "venv"],
27
+ },
28
+ rust: { files: ["Cargo.toml"], extensions: ["rs"], folders: [] },
29
+ golang: { files: ["go.mod", "go.sum", "go.work"], extensions: ["go"], folders: [] },
30
+ bun: { files: ["bun.lock", "bun.lockb", "bunfig.toml"], extensions: [], folders: [] },
31
+ deno: { files: ["deno.json", "deno.jsonc", "deno.lock"], extensions: [], folders: [] },
43
32
  };
44
33
 
45
34
  export async function collectLanguages(context: CollectorContext): Promise<MutableModuleSnapshot> {
46
- const result: MutableModuleSnapshot = {};
47
- if (!LANGUAGE_NAMES.some((name) => context.needs(name))) return result;
48
- const entries = await context.entries();
49
- for (const name of LANGUAGE_NAMES) {
50
- if (!context.needs(name)) continue;
51
- const defaults = DEFAULT_DETECTION[name];
52
- const files = optionStrings(context, name, "detect_files");
53
- const extensions = optionStrings(context, name, "detect_extensions");
54
- const folders = optionStrings(context, name, "detect_folders");
55
- if (
56
- !directMatch(
57
- entries,
58
- files.length > 0 ? files : defaults.files,
59
- extensions.length > 0 ? extensions : defaults.extensions,
60
- folders.length > 0 ? folders : defaults.folders,
61
- )
62
- ) {
63
- continue;
64
- }
65
- const values: Record<string, string> = {};
66
- addEnvironmentValues(context, name, values);
67
- if (context.needs(name, "engines_version")) {
68
- const engine = await readNodeEngine(context);
69
- if (engine) values.engines_version = engine;
70
- }
71
- const needsVersion = context.needs(name, "version");
72
- const needsNumver = name === "rust" && context.needs(name, "numver");
73
- if (needsVersion || needsNumver) {
74
- const command = await versionCommand(context, name);
75
- if (command) {
76
- const output = await runBounded(context, command[0], command[1]);
77
- const version = output ? parseRuntimeVersion(name, output) : undefined;
78
- if (version) {
79
- if (needsVersion) {
80
- values.version = formatVersion(version, optionString(context, name, "version_format"));
81
- }
82
- if (needsNumver) values.numver = version.replace(/^v/u, "");
83
- }
84
- }
85
- }
86
- result[name] = values;
87
- }
88
- return result;
35
+ const result: MutableModuleSnapshot = {};
36
+ if (!LANGUAGE_NAMES.some((name) => context.needs(name))) return result;
37
+ const entries = await context.entries();
38
+ for (const name of LANGUAGE_NAMES) {
39
+ if (!context.needs(name)) continue;
40
+ const defaults = DEFAULT_DETECTION[name];
41
+ const files = optionStrings(context, name, "detect_files");
42
+ const extensions = optionStrings(context, name, "detect_extensions");
43
+ const folders = optionStrings(context, name, "detect_folders");
44
+ if (
45
+ !directMatch(
46
+ entries,
47
+ files.length > 0 ? files : defaults.files,
48
+ extensions.length > 0 ? extensions : defaults.extensions,
49
+ folders.length > 0 ? folders : defaults.folders,
50
+ )
51
+ ) {
52
+ continue;
53
+ }
54
+ const values: Record<string, string> = {};
55
+ addEnvironmentValues(context, name, values);
56
+ if (context.needs(name, "engines_version")) {
57
+ const engine = await readNodeEngine(context);
58
+ if (engine) values.engines_version = engine;
59
+ }
60
+ const needsVersion = context.needs(name, "version");
61
+ const needsNumver = name === "rust" && context.needs(name, "numver");
62
+ if (needsVersion || needsNumver) {
63
+ const command = await versionCommand(context, name);
64
+ if (command) {
65
+ const output = await runBounded(context, command[0], command[1]);
66
+ const version = output ? parseRuntimeVersion(name, output) : undefined;
67
+ if (version) {
68
+ if (needsVersion) {
69
+ values.version = formatVersion(version, optionString(context, name, "version_format"));
70
+ }
71
+ if (needsNumver) values.numver = version.replace(/^v/u, "");
72
+ }
73
+ }
74
+ }
75
+ result[name] = values;
76
+ }
77
+ return result;
89
78
  }
90
79
 
91
- function addEnvironmentValues(
92
- context: CollectorContext,
93
- name: LanguageName,
94
- values: Record<string, string>,
95
- ): void {
96
- const env = context.input.environment;
97
- if (name === "python") {
98
- const virtualenv = safeMetadata(env.VIRTUAL_ENV) ?? safeMetadata(env.CONDA_DEFAULT_ENV);
99
- if (virtualenv && context.needs(name, "virtualenv")) values.virtualenv = pathName(virtualenv);
100
- const pyenv = safeMetadata(env.PYENV_VERSION);
101
- if (pyenv && context.needs(name, "pyenv_prefix")) values.pyenv_prefix = pyenv;
102
- }
103
- if (name === "rust") {
104
- const toolchain = safeMetadata(env.RUSTUP_TOOLCHAIN);
105
- if (toolchain && context.needs(name, "toolchain")) values.toolchain = toolchain;
106
- }
80
+ function addEnvironmentValues(context: CollectorContext, name: LanguageName, values: Record<string, string>): void {
81
+ const env = context.input.environment;
82
+ if (name === "python") {
83
+ const virtualenv = safeMetadata(env.VIRTUAL_ENV) ?? safeMetadata(env.CONDA_DEFAULT_ENV);
84
+ if (virtualenv && context.needs(name, "virtualenv")) values.virtualenv = pathName(virtualenv);
85
+ const pyenv = safeMetadata(env.PYENV_VERSION);
86
+ if (pyenv && context.needs(name, "pyenv_prefix")) values.pyenv_prefix = pyenv;
87
+ }
88
+ if (name === "rust") {
89
+ const toolchain = safeMetadata(env.RUSTUP_TOOLCHAIN);
90
+ if (toolchain && context.needs(name, "toolchain")) values.toolchain = toolchain;
91
+ }
107
92
  }
108
93
 
109
94
  async function readNodeEngine(context: CollectorContext): Promise<string | undefined> {
110
- const source = await context.fs.readFile(
111
- join(context.input.cwd, "package.json"),
112
- MAX_METADATA_FILE_BYTES,
113
- );
114
- if (!source) return undefined;
115
- try {
116
- const document = JSON.parse(source) as { engines?: { node?: unknown } };
117
- return safeMetadata(document.engines?.node, 80);
118
- } catch {
119
- return undefined;
120
- }
95
+ const source = await context.fs.readFile(join(context.input.cwd, "package.json"), MAX_METADATA_FILE_BYTES);
96
+ if (!source) return undefined;
97
+ try {
98
+ const document = JSON.parse(source) as { engines?: { node?: unknown } };
99
+ return safeMetadata(document.engines?.node, 80);
100
+ } catch {
101
+ return undefined;
102
+ }
121
103
  }
122
104
 
123
- async function versionCommand(
124
- context: CollectorContext,
125
- name: LanguageName,
126
- ): Promise<[string, string[]] | undefined> {
127
- switch (name) {
128
- case "nodejs":
129
- return ["node", ["--version"]];
130
- case "python": {
131
- const virtualenv = safeMetadata(context.input.environment.VIRTUAL_ENV);
132
- if (virtualenv) {
133
- const candidate = join(
134
- virtualenv,
135
- context.input.platform === "win32" ? "Scripts/python.exe" : "bin/python",
136
- );
137
- if (await context.fs.fileExists(candidate)) return [candidate, ["--version"]];
138
- }
139
- return ["python", ["--version"]];
140
- }
141
- case "rust": {
142
- const rustc = await safeRustCompiler(context);
143
- return rustc ? [rustc, ["--version"]] : undefined;
144
- }
145
- case "golang":
146
- return ["go", ["version"]];
147
- case "bun":
148
- return ["bun", ["--version"]];
149
- case "deno":
150
- return ["deno", ["-V"]];
151
- }
105
+ async function versionCommand(context: CollectorContext, name: LanguageName): Promise<[string, string[]] | undefined> {
106
+ switch (name) {
107
+ case "nodejs":
108
+ return ["node", ["--version"]];
109
+ case "python": {
110
+ const virtualenv = safeMetadata(context.input.environment.VIRTUAL_ENV);
111
+ if (virtualenv) {
112
+ const candidate = join(virtualenv, context.input.platform === "win32" ? "Scripts/python.exe" : "bin/python");
113
+ if (await context.fs.fileExists(candidate)) return [candidate, ["--version"]];
114
+ }
115
+ return ["python", ["--version"]];
116
+ }
117
+ case "rust": {
118
+ const rustc = await safeRustCompiler(context);
119
+ return rustc ? [rustc, ["--version"]] : undefined;
120
+ }
121
+ case "golang":
122
+ return ["go", ["version"]];
123
+ case "bun":
124
+ return ["bun", ["--version"]];
125
+ case "deno":
126
+ return ["deno", ["-V"]];
127
+ }
152
128
  }
153
129
 
154
130
  async function safeRustCompiler(context: CollectorContext): Promise<string | undefined> {
155
- const candidates = [
156
- context.input.environment.RUSTC,
157
- ...(context.input.environment.PATH ?? "")
158
- .split(delimiter)
159
- .filter(Boolean)
160
- .map((directory) =>
161
- join(directory, context.input.platform === "win32" ? "rustc.exe" : "rustc"),
162
- ),
163
- ];
164
- for (const rawCandidate of candidates) {
165
- const candidate = safeMetadata(rawCandidate, 1_024);
166
- if (!candidate || /(?:^|[\\/])\.(?:cargo|rustup)(?:[\\/]|$)/iu.test(candidate)) continue;
167
- if (await context.fs.fileExists(candidate)) return candidate;
168
- }
169
- return undefined;
131
+ const candidates = [
132
+ context.input.environment.RUSTC,
133
+ ...(context.input.environment.PATH ?? "")
134
+ .split(delimiter)
135
+ .filter(Boolean)
136
+ .map((directory) => join(directory, context.input.platform === "win32" ? "rustc.exe" : "rustc")),
137
+ ];
138
+ for (const rawCandidate of candidates) {
139
+ const candidate = safeMetadata(rawCandidate, 1_024);
140
+ if (!candidate || /(?:^|[\\/])\.(?:cargo|rustup)(?:[\\/]|$)/iu.test(candidate)) continue;
141
+ if (await context.fs.fileExists(candidate)) return candidate;
142
+ }
143
+ return undefined;
170
144
  }
171
145
 
172
146
  export function parseRuntimeVersion(name: LanguageName, output: string): string | undefined {
173
- const normalized = output.trim();
174
- if (
175
- !normalized ||
176
- normalized.includes("\n") ||
177
- normalized.includes("\r") ||
178
- normalized.includes("�")
179
- ) {
180
- return undefined;
181
- }
182
- let match: RegExpExecArray | null;
183
- switch (name) {
184
- case "nodejs":
185
- match = /^v(\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)$/u.exec(normalized);
186
- break;
187
- case "python":
188
- match = /^Python (\d+\.\d+(?:\.\d+)?(?:[0-9A-Za-z.+-]*)?)$/u.exec(normalized);
189
- break;
190
- case "rust":
191
- match = /^rustc (\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)(?: .*)?$/u.exec(normalized);
192
- break;
193
- case "golang":
194
- match = /^go version go(\d+\.\d+(?:\.\d+)?(?:[0-9A-Za-z.+-]*))(?:\s+.*)?$/u.exec(normalized);
195
- break;
196
- case "bun":
197
- match = /^(\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)$/u.exec(normalized);
198
- break;
199
- case "deno":
200
- match = /^(?:deno\s+)?(\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)$/u.exec(normalized);
201
- break;
202
- }
203
- const version = match?.[1];
204
- return version ? `v${version}` : undefined;
147
+ const normalized = output.trim();
148
+ if (!normalized || normalized.includes("\n") || normalized.includes("\r") || normalized.includes("�")) {
149
+ return undefined;
150
+ }
151
+ let match: RegExpExecArray | null;
152
+ switch (name) {
153
+ case "nodejs":
154
+ match = /^v(\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)$/u.exec(normalized);
155
+ break;
156
+ case "python":
157
+ match = /^Python (\d+\.\d+(?:\.\d+)?(?:[0-9A-Za-z.+-]*)?)$/u.exec(normalized);
158
+ break;
159
+ case "rust":
160
+ match = /^rustc (\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)(?: .*)?$/u.exec(normalized);
161
+ break;
162
+ case "golang":
163
+ match = /^go version go(\d+\.\d+(?:\.\d+)?(?:[0-9A-Za-z.+-]*))(?:\s+.*)?$/u.exec(normalized);
164
+ break;
165
+ case "bun":
166
+ match = /^(\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)$/u.exec(normalized);
167
+ break;
168
+ case "deno":
169
+ match = /^(?:deno\s+)?(\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?)$/u.exec(normalized);
170
+ break;
171
+ }
172
+ const version = match?.[1];
173
+ return version ? `v${version}` : undefined;
205
174
  }
@@ -1,112 +1,99 @@
1
1
  import { join } from "node:path";
2
2
  import { parse } from "smol-toml";
3
3
  import {
4
- formatVersion,
5
- isRecord,
6
- MAX_METADATA_FILE_BYTES,
7
- optionString,
8
- parentDirectories,
9
- safeMetadata,
4
+ formatVersion,
5
+ isRecord,
6
+ MAX_METADATA_FILE_BYTES,
7
+ optionString,
8
+ parentDirectories,
9
+ safeMetadata,
10
10
  } from "./helpers.js";
11
11
  import type { CollectorContext } from "./types.js";
12
12
 
13
- export async function collectPackage(
14
- context: CollectorContext,
15
- ): Promise<Record<string, string> | undefined> {
16
- if (!context.needs("package")) return undefined;
17
- const versionFormat = optionString(context, "package", "version_format");
18
- for (const reader of [readPackageJson, readCargo, readPyproject]) {
19
- const result = await reader(context);
20
- if (!result) continue;
21
- return {
22
- source: result.source,
23
- version: formatVersion(result.version, versionFormat),
24
- };
25
- }
26
- return undefined;
13
+ export async function collectPackage(context: CollectorContext): Promise<Record<string, string> | undefined> {
14
+ if (!context.needs("package")) return undefined;
15
+ const versionFormat = optionString(context, "package", "version_format");
16
+ for (const reader of [readPackageJson, readCargo, readPyproject]) {
17
+ const result = await reader(context);
18
+ if (!result) continue;
19
+ return {
20
+ source: result.source,
21
+ version: formatVersion(result.version, versionFormat),
22
+ };
23
+ }
24
+ return undefined;
27
25
  }
28
26
 
29
27
  interface PackageVersion {
30
- version: string;
31
- source: string;
28
+ version: string;
29
+ source: string;
32
30
  }
33
31
 
34
32
  async function readPackageJson(context: CollectorContext): Promise<PackageVersion | undefined> {
35
- const source = await context.fs.readFile(
36
- join(context.input.cwd, "package.json"),
37
- MAX_METADATA_FILE_BYTES,
38
- );
39
- if (!source) return undefined;
40
- try {
41
- const document = JSON.parse(source) as unknown;
42
- const version = isRecord(document) ? safeVersion(document.version) : undefined;
43
- return version ? { version, source: "package.json" } : undefined;
44
- } catch {
45
- return undefined;
46
- }
33
+ const source = await context.fs.readFile(join(context.input.cwd, "package.json"), MAX_METADATA_FILE_BYTES);
34
+ if (!source) return undefined;
35
+ try {
36
+ const document = JSON.parse(source) as unknown;
37
+ const version = isRecord(document) ? safeVersion(document.version) : undefined;
38
+ return version ? { version, source: "package.json" } : undefined;
39
+ } catch {
40
+ return undefined;
41
+ }
47
42
  }
48
43
 
49
44
  async function readCargo(context: CollectorContext): Promise<PackageVersion | undefined> {
50
- const path = join(context.input.cwd, "Cargo.toml");
51
- const source = await context.fs.readFile(path, MAX_METADATA_FILE_BYTES);
52
- if (!source) return undefined;
53
- try {
54
- const document = parse(source);
55
- const packageTable = isRecord(document.package) ? document.package : undefined;
56
- const direct = safeVersion(packageTable?.version);
57
- if (direct) return { version: direct, source: "Cargo.toml" };
58
- const inherited = isRecord(packageTable?.version) && packageTable.version.workspace === true;
59
- if (!inherited) return undefined;
60
- const localWorkspaceVersion = workspacePackageVersion(document);
61
- if (localWorkspaceVersion) {
62
- return { version: localWorkspaceVersion, source: "Cargo.toml workspace" };
63
- }
64
- for (const parent of parentDirectories(context.input.cwd, 8)) {
65
- const parentSource = await context.fs.readFile(
66
- join(parent, "Cargo.toml"),
67
- MAX_METADATA_FILE_BYTES,
68
- );
69
- if (!parentSource) continue;
70
- const parentDocument = parse(parentSource);
71
- const version = workspacePackageVersion(parentDocument);
72
- if (version) return { version, source: "Cargo.toml workspace" };
73
- }
74
- return undefined;
75
- } catch {
76
- return undefined;
77
- }
45
+ const path = join(context.input.cwd, "Cargo.toml");
46
+ const source = await context.fs.readFile(path, MAX_METADATA_FILE_BYTES);
47
+ if (!source) return undefined;
48
+ try {
49
+ const document = parse(source);
50
+ const packageTable = isRecord(document.package) ? document.package : undefined;
51
+ const direct = safeVersion(packageTable?.version);
52
+ if (direct) return { version: direct, source: "Cargo.toml" };
53
+ const inherited = isRecord(packageTable?.version) && packageTable.version.workspace === true;
54
+ if (!inherited) return undefined;
55
+ const localWorkspaceVersion = workspacePackageVersion(document);
56
+ if (localWorkspaceVersion) {
57
+ return { version: localWorkspaceVersion, source: "Cargo.toml workspace" };
58
+ }
59
+ for (const parent of parentDirectories(context.input.cwd, 8)) {
60
+ const parentSource = await context.fs.readFile(join(parent, "Cargo.toml"), MAX_METADATA_FILE_BYTES);
61
+ if (!parentSource) continue;
62
+ const parentDocument = parse(parentSource);
63
+ const version = workspacePackageVersion(parentDocument);
64
+ if (version) return { version, source: "Cargo.toml workspace" };
65
+ }
66
+ return undefined;
67
+ } catch {
68
+ return undefined;
69
+ }
78
70
  }
79
71
 
80
72
  function workspacePackageVersion(document: Record<string, unknown>): string | undefined {
81
- const workspace = isRecord(document.workspace) ? document.workspace : undefined;
82
- const workspacePackage = isRecord(workspace?.package) ? workspace.package : undefined;
83
- return safeVersion(workspacePackage?.version);
73
+ const workspace = isRecord(document.workspace) ? document.workspace : undefined;
74
+ const workspacePackage = isRecord(workspace?.package) ? workspace.package : undefined;
75
+ return safeVersion(workspacePackage?.version);
84
76
  }
85
77
 
86
78
  async function readPyproject(context: CollectorContext): Promise<PackageVersion | undefined> {
87
- const source = await context.fs.readFile(
88
- join(context.input.cwd, "pyproject.toml"),
89
- MAX_METADATA_FILE_BYTES,
90
- );
91
- if (!source) return undefined;
92
- try {
93
- const document = parse(source);
94
- const project = isRecord(document.project) ? document.project : undefined;
95
- if (Array.isArray(project?.dynamic) && project.dynamic.includes("version")) return undefined;
96
- const pepVersion = safeVersion(project?.version);
97
- if (pepVersion) return { version: pepVersion, source: "pyproject.toml (PEP 621)" };
98
- const tool = isRecord(document.tool) ? document.tool : undefined;
99
- const poetry = isRecord(tool?.poetry) ? tool.poetry : undefined;
100
- const poetryVersion = safeVersion(poetry?.version);
101
- return poetryVersion
102
- ? { version: poetryVersion, source: "pyproject.toml (Poetry)" }
103
- : undefined;
104
- } catch {
105
- return undefined;
106
- }
79
+ const source = await context.fs.readFile(join(context.input.cwd, "pyproject.toml"), MAX_METADATA_FILE_BYTES);
80
+ if (!source) return undefined;
81
+ try {
82
+ const document = parse(source);
83
+ const project = isRecord(document.project) ? document.project : undefined;
84
+ if (Array.isArray(project?.dynamic) && project.dynamic.includes("version")) return undefined;
85
+ const pepVersion = safeVersion(project?.version);
86
+ if (pepVersion) return { version: pepVersion, source: "pyproject.toml (PEP 621)" };
87
+ const tool = isRecord(document.tool) ? document.tool : undefined;
88
+ const poetry = isRecord(tool?.poetry) ? tool.poetry : undefined;
89
+ const poetryVersion = safeVersion(poetry?.version);
90
+ return poetryVersion ? { version: poetryVersion, source: "pyproject.toml (Poetry)" } : undefined;
91
+ } catch {
92
+ return undefined;
93
+ }
107
94
  }
108
95
 
109
96
  function safeVersion(value: unknown): string | undefined {
110
- const version = safeMetadata(value, 80);
111
- return version && /^[0-9A-Za-z][0-9A-Za-z.+_-]*$/u.test(version) ? version : undefined;
97
+ const version = safeMetadata(value, 80);
98
+ return version && /^[0-9A-Za-z][0-9A-Za-z.+_-]*$/u.test(version) ? version : undefined;
112
99
  }