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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
#!/usr/bin/env bun
import { mkdir, open, rm } from "node:fs/promises";
import { isAbsolute, join, basename as pathBasename, dirname as pathDirname } from "node:path";
import { isWriteError, localeQuotedEscapedDiagnostic, parseOptions, systemErrorMessage } from "../shared/common.js";
import { UsageError, stdout } from "../shared/diagnostics.js";
import { defineCommand, runAsMain } from "../shared/command.js";
export const MKTEMP_LONG_OPTIONS = ["directory", "dry-run", "quiet", "suffix", "tmpdir", "help", "version"];
export function mktempMetaOption(args) {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === "--") return null;
if (!arg.startsWith("-") || arg === "-") {
if (process.env.POSIXLY_CORRECT) return null;
continue;
}
if (arg.startsWith("--")) {
const option = normalizeMktempLongOption(arg);
const name = option.slice(2).split("=", 1)[0];
if (!MKTEMP_LONG_OPTIONS.includes(name)) return null;
if (option.includes("=")) {
if (name === "directory" || name === "dry-run" || name === "quiet" || name === "help" || name === "version") return null;
continue;
}
if (option === "--help" || option === "--version") return option;
if (name === "suffix") i++;
continue;
}
for (let j = 1; j < arg.length; j++) {
const ch = arg[j];
if (ch === "d" || ch === "q" || ch === "t" || ch === "u") continue;
if (ch !== "p") return null;
if (!arg.slice(j + 1)) i++;
break;
}
}
return null;
}
export async function mktempCmd(args) {
if (process.env.POSIXLY_CORRECT && args.findIndex((arg) => arg !== "--" && !arg.startsWith("-")) !== -1) {
const firstOperand = args.findIndex((arg) => arg !== "--" && !arg.startsWith("-"));
if (args.slice(firstOperand + 1).some((arg) => arg.startsWith("-"))) throw new UsageError("too many templates", true);
}
args = normalizeMktempLongOptions(args);
const { opts, operands } = parseOptions(args, { short: { d: false, p: "value", q: false, t: false, u: false }, long: { directory: false, quiet: false, tmpdir: "optional-value", suffix: "value", "dry-run": false, help: false, version: false } });
if (operands.length > 1) throw new UsageError("too many templates", true);
const template = operands[0] ?? "tmp.XXXXXXXXXX";
const useTmpdir = opts.t || opts.tmpdir !== undefined || opts.p !== undefined || !operands.length;
const envTmpdir = process.env.TMPDIR || null;
const defaultTmpdir = envTmpdir ?? "/tmp";
const optionTmpdir = opts.tmpdir === true || opts.tmpdir === "" ? defaultTmpdir : opts.tmpdir ?? (opts.p === "" ? defaultTmpdir : opts.p);
const tmpdir = opts.t ? (envTmpdir ?? optionTmpdir ?? defaultTmpdir) : optionTmpdir;
if (opts.t && template.includes("/")) throw new UsageError(`invalid template, ${localeQuotedEscapedDiagnostic(template)}, contains directory separator`);
if (useTmpdir && isAbsolute(template)) throw new UsageError(`invalid template, ${localeQuotedEscapedDiagnostic(template)}; with --tmpdir, it may not be absolute`);
const dir = useTmpdir ? tmpdir ?? defaultTmpdir : template.includes("/") ? pathDirname(template) : "";
const name = template.includes("/") ? pathBasename(template) : template;
if (opts.suffix !== undefined && !name.endsWith("X")) throw new UsageError(`with --suffix, template ${localeQuotedEscapedDiagnostic(template)} must end in X`);
const wholeXMatch = [...template.matchAll(/X{3,}/g)].at(-1);
if (opts.suffix === undefined && wholeXMatch) {
const implicitSuffix = template.slice(wholeXMatch.index + wholeXMatch[0].length);
const slash = implicitSuffix.indexOf("/");
if (slash !== -1 && !implicitSuffix.slice(slash + 1).includes("X")) {
throw new UsageError(`invalid suffix ${localeQuotedEscapedDiagnostic(implicitSuffix)}, contains directory separator`);
}
}
const xMatch = [...name.matchAll(/X{3,}/g)].at(-1);
const x = xMatch?.[0];
if (!x || x.length < 3) throw new UsageError(`too few X's in template ${localeQuotedEscapedDiagnostic(template)}`);
if (opts.suffix === undefined && wholeXMatch && template.slice(wholeXMatch.index + wholeXMatch[0].length).includes("/")) {
throw new UsageError(`invalid suffix ${localeQuotedEscapedDiagnostic(template.slice(wholeXMatch.index + wholeXMatch[0].length))}, contains directory separator`);
}
const suffix = opts.suffix ?? name.slice(xMatch.index + x.length);
if (suffix.includes("/")) throw new UsageError(`invalid suffix ${localeQuotedEscapedDiagnostic(suffix)}, contains directory separator`);
const templateDir = template.includes("/") && useTmpdir ? pathDirname(template) : "";
const parent = mktempJoin(dir, templateDir);
const namePrefix = name.slice(0, xMatch.index);
const prefix = mktempPrefix(parent, namePrefix);
const displayTemplate = `${prefix}${x}${suffix}`;
const candidateName = () => `${prefix}${mktempRandom(x.length)}${suffix}`;
if (opts.u || opts["dry-run"]) {
stdout(`${candidateName()}\n`);
return 0;
}
for (let i = 0; i < 100; i++) {
const candidate = candidateName();
try {
if (opts.d || opts.directory) await mkdir(candidate, { mode: 0o700 });
else {
const fh = await open(candidate, "wx", 0o600);
await fh.close();
}
try {
stdout(`${candidate}\n`);
} catch (error) {
if (isWriteError(error)) await rm(candidate, { recursive: true, force: true });
throw error;
}
return 0;
} catch (error) {
if (error.code === "ENOENT") {
if (opts.q || opts.quiet) return 1;
throw new UsageError(`failed to create file via template ${localeQuotedEscapedDiagnostic(displayTemplate)}: ${systemErrorMessage(error)}`);
}
if (error.code !== "EEXIST") {
if (opts.q || opts.quiet) return 1;
throw new UsageError(`failed to create file via template ${localeQuotedEscapedDiagnostic(displayTemplate)}: ${systemErrorMessage(error)}`);
}
}
}
if (opts.q || opts.quiet) return 1;
throw new UsageError(`failed to create file via template ${localeQuotedEscapedDiagnostic(displayTemplate)}`);
}
export function normalizeMktempLongOptions(args) {
const out = [];
let end = false;
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (end || arg === "--" || !arg.startsWith("--")) {
out.push(arg);
if (arg === "--") end = true;
continue;
}
const option = normalizeMktempLongOption(arg);
out.push(option);
if (option === "--suffix" && i + 1 < args.length) out.push(args[++i]);
}
return out;
}
export function normalizeMktempLongOption(arg) {
const body = arg.slice(2);
const eq = body.indexOf("=");
const name = eq === -1 ? body : body.slice(0, eq);
if (!name) return arg;
const matches = MKTEMP_LONG_OPTIONS.filter((option) => option.startsWith(name));
if (matches.length === 0) return arg;
if (matches.length > 1) {
throw new UsageError(`option '--${name}${eq === -1 ? "" : `=${body.slice(eq + 1)}`}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true);
}
return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`;
}
export function mktempJoin(...parts) {
const filtered = parts.filter((part) => part !== "");
if (filtered.length === 0) return "";
if (filtered[0] === "." && filtered.length > 1) return `./${join(...filtered.slice(1))}`;
if (filtered[0].startsWith("./") && filtered.length > 1) return `${filtered[0]}/${join(...filtered.slice(1))}`;
return join(...filtered);
}
export function mktempPrefix(parent, namePrefix) {
if (!parent) return namePrefix;
if (namePrefix) return mktempJoin(parent, namePrefix);
return parent.endsWith("/") ? parent : `${parent}/`;
}
export function mktempRandom(length) {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let out = "";
for (let i = 0; i < length; i++) out += alphabet[Math.floor(Math.random() * alphabet.length)];
return out;
}
const singleCall = defineCommand("mktemp", mktempCmd, mktempMetaOption);
export default singleCall;
if (import.meta.main) await runAsMain(singleCall);
|