maven-proxy 1.0.1 → 1.0.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.
@@ -1,328 +1,328 @@
1
- import fs from "node:fs";
2
- import os from "node:os";
3
- import path from "node:path";
4
- import { spawnSync } from "node:child_process";
5
-
6
- function runCommandCapture(command, args) {
7
- const result = spawnSync(command, args, {
8
- shell: false,
9
- encoding: "utf8",
10
- });
11
-
12
- if (result.error || result.status !== 0) {
13
- return "";
14
- }
15
-
16
- return String(result.stdout || "").trim();
17
- }
18
-
19
- function isFile(filePath) {
20
- try {
21
- return fs.statSync(filePath).isFile();
22
- } catch {
23
- return false;
24
- }
25
- }
26
-
27
- function isDirectory(dirPath) {
28
- try {
29
- return fs.statSync(dirPath).isDirectory();
30
- } catch {
31
- return false;
32
- }
33
- }
34
-
35
- function isLikelyJavaHome(homePath, platform) {
36
- const javaName = platform === "win32" ? "java.exe" : "java";
37
-
38
- if (!isFile(path.join(homePath, "bin", javaName))) {
39
- return false;
40
- }
41
-
42
- if (isFile(path.join(homePath, "release"))) {
43
- return true;
44
- }
45
-
46
- if (isFile(path.join(homePath, "lib", "security", "cacerts"))) {
47
- return true;
48
- }
49
-
50
- if (isFile(path.join(homePath, "jre", "lib", "security", "cacerts"))) {
51
- return true;
52
- }
53
-
54
- return false;
55
- }
56
-
57
- function safeRealpath(targetPath) {
58
- try {
59
- return fs.realpathSync(targetPath);
60
- } catch {
61
- return targetPath;
62
- }
63
- }
64
-
65
- function deriveJavaHomeFromJavaBin(javaBinPath) {
66
- const resolved = safeRealpath(javaBinPath);
67
- const executableName = path.basename(resolved).toLowerCase();
68
-
69
- if (executableName !== "java" && executableName !== "java.exe") {
70
- return "";
71
- }
72
-
73
- const binDir = path.dirname(resolved);
74
- if (path.basename(binDir).toLowerCase() !== "bin") {
75
- return "";
76
- }
77
-
78
- const javaHome = path.dirname(binDir);
79
- if (!isDirectory(javaHome)) {
80
- return "";
81
- }
82
-
83
- const platform = os.platform();
84
- return isLikelyJavaHome(javaHome, platform) ? javaHome : "";
85
- }
86
-
87
- function normalizeConfiguredJavaHome(configured, platform) {
88
- if (!configured) {
89
- return "";
90
- }
91
-
92
- const resolved = safeRealpath(configured);
93
- if (isDirectory(resolved)) {
94
- if (isLikelyJavaHome(resolved, platform)) {
95
- return resolved;
96
- }
97
-
98
- if (path.basename(resolved).toLowerCase() === "bin") {
99
- const base = path.dirname(resolved);
100
- if (isLikelyJavaHome(base, platform)) {
101
- return base;
102
- }
103
- }
104
-
105
- return "";
106
- }
107
-
108
- if (isFile(resolved)) {
109
- return deriveJavaHomeFromJavaBin(resolved);
110
- }
111
-
112
- return "";
113
- }
114
-
115
- function detectFromJavaCommand(platform) {
116
- const command = platform === "win32" ? "where" : "which";
117
- const output = runCommandCapture(command, ["java"]);
118
- if (!output) {
119
- return "";
120
- }
121
-
122
- const firstPath = output
123
- .split(/\r?\n/g)
124
- .map((line) => line.trim())
125
- .find(Boolean);
126
-
127
- if (!firstPath) {
128
- return "";
129
- }
130
-
131
- return deriveJavaHomeFromJavaBin(firstPath);
132
- }
133
-
134
- function detectFromLinuxCommonPaths() {
135
- const directCandidates = [
136
- "/usr/bin/java",
137
- "/usr/local/bin/java",
138
- ];
139
-
140
- for (const javaPath of directCandidates) {
141
- if (!isFile(javaPath)) {
142
- continue;
143
- }
144
-
145
- const home = deriveJavaHomeFromJavaBin(javaPath);
146
- if (home) {
147
- return home;
148
- }
149
- }
150
-
151
- const roots = [
152
- "/usr/lib/jvm",
153
- "/usr/java",
154
- "/opt/java",
155
- "/opt/jdk",
156
- ];
157
-
158
- for (const root of roots) {
159
- if (!isDirectory(root)) {
160
- continue;
161
- }
162
-
163
- let children = [];
164
- try {
165
- children = fs.readdirSync(root, { withFileTypes: true })
166
- .filter((entry) => entry.isDirectory())
167
- .map((entry) => entry.name)
168
- .sort((a, b) => b.localeCompare(a));
169
- } catch {
170
- continue;
171
- }
172
-
173
- for (const child of children) {
174
- const candidate = path.join(root, child);
175
- if (isFile(path.join(candidate, "bin", "java"))) {
176
- return candidate;
177
- }
178
- }
179
- }
180
-
181
- return "";
182
- }
183
-
184
- function detectFromMacJavaHomeCommand() {
185
- const output = runCommandCapture("/usr/libexec/java_home", []);
186
- if (!output) {
187
- return "";
188
- }
189
-
190
- return isDirectory(output) ? output : "";
191
- }
192
-
193
- function detectFromWindowsCommonPaths() {
194
- const roots = [
195
- "C:\\Program Files\\Java",
196
- "C:\\Program Files (x86)\\Java",
197
- "C:\\Program Files\\Eclipse Adoptium",
198
- "C:\\Program Files\\Microsoft",
199
- "C:\\Program Files\\Amazon Corretto",
200
- "C:\\Program Files\\Zulu",
201
- ];
202
-
203
- for (const root of roots) {
204
- if (!isDirectory(root)) {
205
- continue;
206
- }
207
-
208
- let children = [];
209
- try {
210
- children = fs.readdirSync(root, { withFileTypes: true })
211
- .filter((entry) => entry.isDirectory())
212
- .map((entry) => entry.name)
213
- .sort((a, b) => b.localeCompare(a));
214
- } catch {
215
- continue;
216
- }
217
-
218
- for (const child of children) {
219
- const base = path.join(root, child);
220
- const candidates = [base, path.join(base, "jre")];
221
- for (const candidate of candidates) {
222
- if (isFile(path.join(candidate, "bin", "java.exe"))) {
223
- return candidate;
224
- }
225
- }
226
- }
227
- }
228
-
229
- return "";
230
- }
231
-
232
- function detectAutoJavaHome(platform) {
233
- if (platform === "darwin") {
234
- const fromMacCommand = detectFromMacJavaHomeCommand();
235
- if (fromMacCommand) {
236
- return {
237
- javaHome: fromMacCommand,
238
- source: "auto-macos-java-home",
239
- };
240
- }
241
-
242
- const fromCommand = detectFromJavaCommand(platform);
243
- if (fromCommand) {
244
- return {
245
- javaHome: fromCommand,
246
- source: "auto-java-command",
247
- };
248
- }
249
-
250
- return { javaHome: "", source: "none" };
251
- }
252
-
253
- if (platform === "win32") {
254
- const fromCommand = detectFromJavaCommand(platform);
255
- if (fromCommand) {
256
- return {
257
- javaHome: fromCommand,
258
- source: "auto-java-command",
259
- };
260
- }
261
-
262
- const fromWindowsPath = detectFromWindowsCommonPaths();
263
- if (fromWindowsPath) {
264
- return {
265
- javaHome: fromWindowsPath,
266
- source: "auto-windows-common-path",
267
- };
268
- }
269
-
270
- return { javaHome: "", source: "none" };
271
- }
272
-
273
- const fromCommand = detectFromJavaCommand(platform);
274
- if (fromCommand) {
275
- return {
276
- javaHome: fromCommand,
277
- source: "auto-java-command",
278
- };
279
- }
280
-
281
- if (platform === "linux") {
282
- const fromLinuxPath = detectFromLinuxCommonPaths();
283
- if (fromLinuxPath) {
284
- return {
285
- javaHome: fromLinuxPath,
286
- source: "auto-linux-common-path",
287
- };
288
- }
289
- }
290
-
291
- return { javaHome: "", source: "none" };
292
- }
293
-
294
- export function detectJavaHome(configuredJavaHome = "", platform = os.platform()) {
295
- const configured = String(configuredJavaHome || "").trim();
296
- const normalizedConfigured = normalizeConfiguredJavaHome(configured, platform);
297
-
298
- if (normalizedConfigured) {
299
- return {
300
- javaHome: normalizedConfigured,
301
- source: "env",
302
- configuredJavaHome: configured,
303
- };
304
- }
305
-
306
- const autoDetected = detectAutoJavaHome(platform);
307
- if (autoDetected.javaHome) {
308
- return {
309
- javaHome: autoDetected.javaHome,
310
- source: configured ? "auto-fallback" : autoDetected.source,
311
- configuredJavaHome: configured,
312
- };
313
- }
314
-
315
- if (configured) {
316
- return {
317
- javaHome: configured,
318
- source: "env-missing",
319
- configuredJavaHome: configured,
320
- };
321
- }
322
-
323
- return {
324
- javaHome: "",
325
- source: "none",
326
- configuredJavaHome: "",
327
- };
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { spawnSync } from "node:child_process";
5
+
6
+ function runCommandCapture(command, args) {
7
+ const result = spawnSync(command, args, {
8
+ shell: false,
9
+ encoding: "utf8",
10
+ });
11
+
12
+ if (result.error || result.status !== 0) {
13
+ return "";
14
+ }
15
+
16
+ return String(result.stdout || "").trim();
17
+ }
18
+
19
+ function isFile(filePath) {
20
+ try {
21
+ return fs.statSync(filePath).isFile();
22
+ } catch {
23
+ return false;
24
+ }
25
+ }
26
+
27
+ function isDirectory(dirPath) {
28
+ try {
29
+ return fs.statSync(dirPath).isDirectory();
30
+ } catch {
31
+ return false;
32
+ }
33
+ }
34
+
35
+ function isLikelyJavaHome(homePath, platform) {
36
+ const javaName = platform === "win32" ? "java.exe" : "java";
37
+
38
+ if (!isFile(path.join(homePath, "bin", javaName))) {
39
+ return false;
40
+ }
41
+
42
+ if (isFile(path.join(homePath, "release"))) {
43
+ return true;
44
+ }
45
+
46
+ if (isFile(path.join(homePath, "lib", "security", "cacerts"))) {
47
+ return true;
48
+ }
49
+
50
+ if (isFile(path.join(homePath, "jre", "lib", "security", "cacerts"))) {
51
+ return true;
52
+ }
53
+
54
+ return false;
55
+ }
56
+
57
+ function safeRealpath(targetPath) {
58
+ try {
59
+ return fs.realpathSync(targetPath);
60
+ } catch {
61
+ return targetPath;
62
+ }
63
+ }
64
+
65
+ function deriveJavaHomeFromJavaBin(javaBinPath) {
66
+ const resolved = safeRealpath(javaBinPath);
67
+ const executableName = path.basename(resolved).toLowerCase();
68
+
69
+ if (executableName !== "java" && executableName !== "java.exe") {
70
+ return "";
71
+ }
72
+
73
+ const binDir = path.dirname(resolved);
74
+ if (path.basename(binDir).toLowerCase() !== "bin") {
75
+ return "";
76
+ }
77
+
78
+ const javaHome = path.dirname(binDir);
79
+ if (!isDirectory(javaHome)) {
80
+ return "";
81
+ }
82
+
83
+ const platform = os.platform();
84
+ return isLikelyJavaHome(javaHome, platform) ? javaHome : "";
85
+ }
86
+
87
+ function normalizeConfiguredJavaHome(configured, platform) {
88
+ if (!configured) {
89
+ return "";
90
+ }
91
+
92
+ const resolved = safeRealpath(configured);
93
+ if (isDirectory(resolved)) {
94
+ if (isLikelyJavaHome(resolved, platform)) {
95
+ return resolved;
96
+ }
97
+
98
+ if (path.basename(resolved).toLowerCase() === "bin") {
99
+ const base = path.dirname(resolved);
100
+ if (isLikelyJavaHome(base, platform)) {
101
+ return base;
102
+ }
103
+ }
104
+
105
+ return "";
106
+ }
107
+
108
+ if (isFile(resolved)) {
109
+ return deriveJavaHomeFromJavaBin(resolved);
110
+ }
111
+
112
+ return "";
113
+ }
114
+
115
+ function detectFromJavaCommand(platform) {
116
+ const command = platform === "win32" ? "where" : "which";
117
+ const output = runCommandCapture(command, ["java"]);
118
+ if (!output) {
119
+ return "";
120
+ }
121
+
122
+ const firstPath = output
123
+ .split(/\r?\n/g)
124
+ .map((line) => line.trim())
125
+ .find(Boolean);
126
+
127
+ if (!firstPath) {
128
+ return "";
129
+ }
130
+
131
+ return deriveJavaHomeFromJavaBin(firstPath);
132
+ }
133
+
134
+ function detectFromLinuxCommonPaths() {
135
+ const directCandidates = [
136
+ "/usr/bin/java",
137
+ "/usr/local/bin/java",
138
+ ];
139
+
140
+ for (const javaPath of directCandidates) {
141
+ if (!isFile(javaPath)) {
142
+ continue;
143
+ }
144
+
145
+ const home = deriveJavaHomeFromJavaBin(javaPath);
146
+ if (home) {
147
+ return home;
148
+ }
149
+ }
150
+
151
+ const roots = [
152
+ "/usr/lib/jvm",
153
+ "/usr/java",
154
+ "/opt/java",
155
+ "/opt/jdk",
156
+ ];
157
+
158
+ for (const root of roots) {
159
+ if (!isDirectory(root)) {
160
+ continue;
161
+ }
162
+
163
+ let children = [];
164
+ try {
165
+ children = fs.readdirSync(root, { withFileTypes: true })
166
+ .filter((entry) => entry.isDirectory())
167
+ .map((entry) => entry.name)
168
+ .sort((a, b) => b.localeCompare(a));
169
+ } catch {
170
+ continue;
171
+ }
172
+
173
+ for (const child of children) {
174
+ const candidate = path.join(root, child);
175
+ if (isFile(path.join(candidate, "bin", "java"))) {
176
+ return candidate;
177
+ }
178
+ }
179
+ }
180
+
181
+ return "";
182
+ }
183
+
184
+ function detectFromMacJavaHomeCommand() {
185
+ const output = runCommandCapture("/usr/libexec/java_home", []);
186
+ if (!output) {
187
+ return "";
188
+ }
189
+
190
+ return isDirectory(output) ? output : "";
191
+ }
192
+
193
+ function detectFromWindowsCommonPaths() {
194
+ const roots = [
195
+ "C:\\Program Files\\Java",
196
+ "C:\\Program Files (x86)\\Java",
197
+ "C:\\Program Files\\Eclipse Adoptium",
198
+ "C:\\Program Files\\Microsoft",
199
+ "C:\\Program Files\\Amazon Corretto",
200
+ "C:\\Program Files\\Zulu",
201
+ ];
202
+
203
+ for (const root of roots) {
204
+ if (!isDirectory(root)) {
205
+ continue;
206
+ }
207
+
208
+ let children = [];
209
+ try {
210
+ children = fs.readdirSync(root, { withFileTypes: true })
211
+ .filter((entry) => entry.isDirectory())
212
+ .map((entry) => entry.name)
213
+ .sort((a, b) => b.localeCompare(a));
214
+ } catch {
215
+ continue;
216
+ }
217
+
218
+ for (const child of children) {
219
+ const base = path.join(root, child);
220
+ const candidates = [base, path.join(base, "jre")];
221
+ for (const candidate of candidates) {
222
+ if (isFile(path.join(candidate, "bin", "java.exe"))) {
223
+ return candidate;
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ return "";
230
+ }
231
+
232
+ function detectAutoJavaHome(platform) {
233
+ if (platform === "darwin") {
234
+ const fromMacCommand = detectFromMacJavaHomeCommand();
235
+ if (fromMacCommand) {
236
+ return {
237
+ javaHome: fromMacCommand,
238
+ source: "auto-macos-java-home",
239
+ };
240
+ }
241
+
242
+ const fromCommand = detectFromJavaCommand(platform);
243
+ if (fromCommand) {
244
+ return {
245
+ javaHome: fromCommand,
246
+ source: "auto-java-command",
247
+ };
248
+ }
249
+
250
+ return { javaHome: "", source: "none" };
251
+ }
252
+
253
+ if (platform === "win32") {
254
+ const fromCommand = detectFromJavaCommand(platform);
255
+ if (fromCommand) {
256
+ return {
257
+ javaHome: fromCommand,
258
+ source: "auto-java-command",
259
+ };
260
+ }
261
+
262
+ const fromWindowsPath = detectFromWindowsCommonPaths();
263
+ if (fromWindowsPath) {
264
+ return {
265
+ javaHome: fromWindowsPath,
266
+ source: "auto-windows-common-path",
267
+ };
268
+ }
269
+
270
+ return { javaHome: "", source: "none" };
271
+ }
272
+
273
+ const fromCommand = detectFromJavaCommand(platform);
274
+ if (fromCommand) {
275
+ return {
276
+ javaHome: fromCommand,
277
+ source: "auto-java-command",
278
+ };
279
+ }
280
+
281
+ if (platform === "linux") {
282
+ const fromLinuxPath = detectFromLinuxCommonPaths();
283
+ if (fromLinuxPath) {
284
+ return {
285
+ javaHome: fromLinuxPath,
286
+ source: "auto-linux-common-path",
287
+ };
288
+ }
289
+ }
290
+
291
+ return { javaHome: "", source: "none" };
292
+ }
293
+
294
+ export function detectJavaHome(configuredJavaHome = "", platform = os.platform()) {
295
+ const configured = String(configuredJavaHome || "").trim();
296
+ const normalizedConfigured = normalizeConfiguredJavaHome(configured, platform);
297
+
298
+ if (normalizedConfigured) {
299
+ return {
300
+ javaHome: normalizedConfigured,
301
+ source: "env",
302
+ configuredJavaHome: configured,
303
+ };
304
+ }
305
+
306
+ const autoDetected = detectAutoJavaHome(platform);
307
+ if (autoDetected.javaHome) {
308
+ return {
309
+ javaHome: autoDetected.javaHome,
310
+ source: configured ? "auto-fallback" : autoDetected.source,
311
+ configuredJavaHome: configured,
312
+ };
313
+ }
314
+
315
+ if (configured) {
316
+ return {
317
+ javaHome: configured,
318
+ source: "env-missing",
319
+ configuredJavaHome: configured,
320
+ };
321
+ }
322
+
323
+ return {
324
+ javaHome: "",
325
+ source: "none",
326
+ configuredJavaHome: "",
327
+ };
328
328
  }