1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#!/usr/bin/env bun
import { writeEnvironment } from "../shared/common.js";
import { UsageError, stdout } from "../shared/diagnostics.js";
import { defineCommand, runAsMain } from "../shared/command.js";
export const PRINTENV_LONG_OPTIONS = ["null", "help", "version"];
export function printenvMetaOption(args) {
for (const arg of args) {
if (arg === "--") return null;
if (arg === "-" || !arg.startsWith("-")) return null;
if (arg.startsWith("--")) {
const option = normalizePrintenvLongOption(arg);
const name = option.slice(2).split("=", 1)[0];
if (!PRINTENV_LONG_OPTIONS.includes(name) || option.includes("=")) return null;
if (option === "--help" || option === "--version") return option;
continue;
}
for (let j = 1; j < arg.length; j++) if (arg[j] !== "0") return null;
}
return null;
}
export async function printenv(args) {
const operands = [];
let sep = "\n";
let parsing = true;
for (const arg of args) {
if (!parsing) {
operands.push(arg);
} else if (arg === "--") {
parsing = false;
} else if (arg === "-" || !arg.startsWith("-")) {
operands.push(arg);
parsing = false;
} else if (arg.startsWith("--")) {
const option = normalizePrintenvLongOption(arg);
const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2);
if (name === "null") {
if (inlineValue !== undefined) throw new UsageError("option '--null' doesn't allow an argument", true);
sep = "\0";
} else if (name === "help" || name === "version") {
if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
} else {
throw new UsageError(`unrecognized option '${arg}'`, true);
}
} else if (/^-0+$/.test(arg)) {
sep = "\0";
} else if (arg.startsWith("-0")) {
throw new UsageError(`invalid option -- '${arg[2]}'`, true);
} else if (arg.startsWith("-")) {
throw new UsageError(`invalid option -- '${arg.slice(1, 2)}'`, true);
} else {
operands.push(arg);
}
}
if (!operands.length) {
writeEnvironment(process.env, sep);
return 0;
}
let code = 0;
for (const key of operands) {
if (process.env[key] == null) code = 1;
else stdout(`${process.env[key]}${sep}`);
}
return code;
}
export function normalizePrintenvLongOption(arg) {
const body = arg.slice(2);
const eq = body.indexOf("=");
const name = eq === -1 ? body : body.slice(0, eq);
if (!name) return arg;
const match = PRINTENV_LONG_OPTIONS.find((option) => option.startsWith(name));
if (!match) return arg;
return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`;
}
const singleCall = defineCommand("printenv", printenv, printenvMetaOption);
export default singleCall;
if (import.meta.main) await runAsMain(singleCall);
|