@srobroek/pi-chezmoi-editor 0.0.0-stage → 0.7.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.
- package/CHANGELOG.md +69 -0
- package/LICENSE +202 -0
- package/README.md +71 -2
- package/extensions/chezmoi.js +920 -0
- package/package.json +31 -4
- package/skills/chezmoi-editor/SKILL.md +67 -0
- package/skills/chezmoi-editor/references/conventions.md +31 -0
|
@@ -0,0 +1,920 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// Generated by scripts/build.ts. Do not edit.
|
|
3
|
+
// plugins/chezmoi/pi-src/chezmoi.ts
|
|
4
|
+
import process from "process";
|
|
5
|
+
|
|
6
|
+
// plugins/chezmoi/shared/guard.ts
|
|
7
|
+
import { spawn as spawnProcess } from "child_process";
|
|
8
|
+
import { realpathSync } from "fs";
|
|
9
|
+
import { homedir as homedir2 } from "os";
|
|
10
|
+
import { basename as basename2 } from "path";
|
|
11
|
+
|
|
12
|
+
// lib/src/shell-write-targets.ts
|
|
13
|
+
import { statSync } from "fs";
|
|
14
|
+
import { homedir } from "os";
|
|
15
|
+
import { basename, isAbsolute, join, relative, resolve } from "path";
|
|
16
|
+
|
|
17
|
+
// lib/src/shell-tokenizer.ts
|
|
18
|
+
var SEPARATORS = new Set([";", "&", "|", "(", ")", `
|
|
19
|
+
`]);
|
|
20
|
+
function token(value, startsQuoted = false, sawQuote = false) {
|
|
21
|
+
return { value, startsQuoted, sawQuote };
|
|
22
|
+
}
|
|
23
|
+
function tokenizeShell(command, options = {}) {
|
|
24
|
+
if (typeof command !== "string")
|
|
25
|
+
return [];
|
|
26
|
+
const out = [];
|
|
27
|
+
let current = "";
|
|
28
|
+
let started = false;
|
|
29
|
+
let startsQuoted = false;
|
|
30
|
+
let sawQuote = false;
|
|
31
|
+
let quote = null;
|
|
32
|
+
const pending = [];
|
|
33
|
+
const flush = () => {
|
|
34
|
+
if (!started)
|
|
35
|
+
return;
|
|
36
|
+
out.push(token(current, startsQuoted, sawQuote));
|
|
37
|
+
current = "";
|
|
38
|
+
started = false;
|
|
39
|
+
startsQuoted = false;
|
|
40
|
+
sawQuote = false;
|
|
41
|
+
};
|
|
42
|
+
for (let i = 0;i < command.length; i++) {
|
|
43
|
+
const ch = command[i];
|
|
44
|
+
if (quote !== null) {
|
|
45
|
+
if (ch === quote) {
|
|
46
|
+
quote = null;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
current += ch;
|
|
50
|
+
started = true;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (ch === '"' || ch === "'") {
|
|
54
|
+
if (!started)
|
|
55
|
+
startsQuoted = true;
|
|
56
|
+
started = true;
|
|
57
|
+
sawQuote = true;
|
|
58
|
+
quote = ch;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (ch === "\\" && i + 1 < command.length) {
|
|
62
|
+
if (options.preserveBackslashes)
|
|
63
|
+
current += ch;
|
|
64
|
+
current += command[++i];
|
|
65
|
+
started = true;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (ch === "$" && command[i + 1] === "(") {
|
|
69
|
+
flush();
|
|
70
|
+
out.push(token("$("));
|
|
71
|
+
i++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (ch === "<" && command[i + 1] === "<" && command[i + 2] === "<") {
|
|
75
|
+
flush();
|
|
76
|
+
out.push(token("<<<"));
|
|
77
|
+
i += 2;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (ch === "<" && command[i + 1] === "<") {
|
|
81
|
+
const operator = hereDocumentOperator(command, i);
|
|
82
|
+
if (operator !== null) {
|
|
83
|
+
flush();
|
|
84
|
+
pending.push(operator);
|
|
85
|
+
i = operator.end - 1;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (ch === `
|
|
90
|
+
`) {
|
|
91
|
+
flush();
|
|
92
|
+
out.push(token(ch));
|
|
93
|
+
let bodyStart = i + 1;
|
|
94
|
+
while (pending.length > 0) {
|
|
95
|
+
const document = pending.shift();
|
|
96
|
+
if (document === undefined)
|
|
97
|
+
break;
|
|
98
|
+
const body = hereDocumentBody(command, bodyStart, document);
|
|
99
|
+
if (!document.quoted) {
|
|
100
|
+
const source = command.slice(bodyStart, body.bodyEnd);
|
|
101
|
+
if (options.hereDocumentSubstitutionsOnly)
|
|
102
|
+
out.push(...substitutions(source, options));
|
|
103
|
+
else
|
|
104
|
+
out.push(...tokenizeShell(source, options));
|
|
105
|
+
}
|
|
106
|
+
i = body.terminatorEnd;
|
|
107
|
+
bodyStart = i + 1;
|
|
108
|
+
}
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (/\s/.test(ch)) {
|
|
112
|
+
flush();
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (SEPARATORS.has(ch)) {
|
|
116
|
+
flush();
|
|
117
|
+
out.push(token(ch));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
current += ch;
|
|
121
|
+
started = true;
|
|
122
|
+
}
|
|
123
|
+
flush();
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
function hereDocumentOperator(command, start) {
|
|
127
|
+
let i = start + 2;
|
|
128
|
+
const stripTabs = command[i] === "-";
|
|
129
|
+
if (stripTabs)
|
|
130
|
+
i++;
|
|
131
|
+
while (command[i] === " " || command[i] === "\t")
|
|
132
|
+
i++;
|
|
133
|
+
let delimiter = "";
|
|
134
|
+
let quoted = false;
|
|
135
|
+
let quote = null;
|
|
136
|
+
while (i < command.length) {
|
|
137
|
+
const ch = command[i];
|
|
138
|
+
if (quote !== null) {
|
|
139
|
+
if (ch === quote) {
|
|
140
|
+
quote = null;
|
|
141
|
+
i++;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
delimiter += ch;
|
|
145
|
+
i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (ch === '"' || ch === "'") {
|
|
149
|
+
quoted = true;
|
|
150
|
+
quote = ch;
|
|
151
|
+
i++;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (ch === "\\" && i + 1 < command.length) {
|
|
155
|
+
quoted = true;
|
|
156
|
+
delimiter += command[i + 1];
|
|
157
|
+
i += 2;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (/\s|[;&|<>()]/.test(ch))
|
|
161
|
+
break;
|
|
162
|
+
delimiter += ch;
|
|
163
|
+
i++;
|
|
164
|
+
}
|
|
165
|
+
if (quote !== null || delimiter.length === 0)
|
|
166
|
+
return null;
|
|
167
|
+
return { delimiter, stripTabs, quoted, end: i };
|
|
168
|
+
}
|
|
169
|
+
function hereDocumentBody(command, from, document) {
|
|
170
|
+
let cursor = from;
|
|
171
|
+
while (cursor < command.length) {
|
|
172
|
+
let next = command.indexOf(`
|
|
173
|
+
`, cursor);
|
|
174
|
+
if (next === -1)
|
|
175
|
+
next = command.length;
|
|
176
|
+
let line = command.slice(cursor, next);
|
|
177
|
+
if (document.stripTabs)
|
|
178
|
+
line = line.replace(/^\t+/, "");
|
|
179
|
+
if (line === document.delimiter)
|
|
180
|
+
return { bodyEnd: cursor, terminatorEnd: next };
|
|
181
|
+
if (next === command.length)
|
|
182
|
+
break;
|
|
183
|
+
cursor = next + 1;
|
|
184
|
+
}
|
|
185
|
+
return { bodyEnd: command.length, terminatorEnd: command.length };
|
|
186
|
+
}
|
|
187
|
+
function substitutions(body, options) {
|
|
188
|
+
const out = [];
|
|
189
|
+
for (let i = 0;i < body.length; i++) {
|
|
190
|
+
const ch = body[i];
|
|
191
|
+
if (ch === "\\") {
|
|
192
|
+
i++;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
let inner;
|
|
196
|
+
if (ch === "$" && body[i + 1] === "(") {
|
|
197
|
+
const end = substitutionEnd(body, i + 2);
|
|
198
|
+
inner = body.slice(i + 2, end);
|
|
199
|
+
i = end;
|
|
200
|
+
if (inner.startsWith("("))
|
|
201
|
+
continue;
|
|
202
|
+
} else if (ch === "`") {
|
|
203
|
+
let end = i + 1;
|
|
204
|
+
while (end < body.length && body[end] !== "`")
|
|
205
|
+
end += body[end] === "\\" ? 2 : 1;
|
|
206
|
+
inner = body.slice(i + 1, end);
|
|
207
|
+
i = end;
|
|
208
|
+
} else {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
out.push(token("$("), ...tokenizeShell(inner, options), token(")"));
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
function substitutionEnd(body, from) {
|
|
216
|
+
let depth = 1;
|
|
217
|
+
let quote = null;
|
|
218
|
+
for (let i = from;i < body.length; i++) {
|
|
219
|
+
const ch = body[i];
|
|
220
|
+
if (quote !== null) {
|
|
221
|
+
if (ch === quote)
|
|
222
|
+
quote = null;
|
|
223
|
+
else if (ch === "\\" && quote === '"')
|
|
224
|
+
i++;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (ch === "\\")
|
|
228
|
+
i++;
|
|
229
|
+
else if (ch === '"' || ch === "'")
|
|
230
|
+
quote = ch;
|
|
231
|
+
else if (ch === "(")
|
|
232
|
+
depth++;
|
|
233
|
+
else if (ch === ")" && --depth === 0)
|
|
234
|
+
return i;
|
|
235
|
+
}
|
|
236
|
+
return body.length;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// lib/src/command-words.ts
|
|
240
|
+
var NONE = new Set;
|
|
241
|
+
var WRAPPERS = new Map([
|
|
242
|
+
[
|
|
243
|
+
"sudo",
|
|
244
|
+
{
|
|
245
|
+
valued: new Set([
|
|
246
|
+
"-C",
|
|
247
|
+
"-D",
|
|
248
|
+
"-g",
|
|
249
|
+
"-h",
|
|
250
|
+
"-p",
|
|
251
|
+
"-R",
|
|
252
|
+
"-r",
|
|
253
|
+
"-T",
|
|
254
|
+
"-t",
|
|
255
|
+
"-U",
|
|
256
|
+
"-u",
|
|
257
|
+
"--chdir",
|
|
258
|
+
"--chroot",
|
|
259
|
+
"--close-from",
|
|
260
|
+
"--command-timeout",
|
|
261
|
+
"--group",
|
|
262
|
+
"--host",
|
|
263
|
+
"--other-user",
|
|
264
|
+
"--prompt",
|
|
265
|
+
"--role",
|
|
266
|
+
"--type",
|
|
267
|
+
"--user"
|
|
268
|
+
]),
|
|
269
|
+
chdir: new Set(["-D", "--chdir"])
|
|
270
|
+
}
|
|
271
|
+
],
|
|
272
|
+
["doas", { valued: new Set(["-C", "-u"]) }],
|
|
273
|
+
[
|
|
274
|
+
"env",
|
|
275
|
+
{
|
|
276
|
+
valued: new Set(["-C", "-L", "-P", "-S", "-U", "-u", "--chdir", "--split-string", "--unset"]),
|
|
277
|
+
chdir: new Set(["-C", "--chdir"]),
|
|
278
|
+
split: new Set(["-S", "--split-string"]),
|
|
279
|
+
assignments: true
|
|
280
|
+
}
|
|
281
|
+
],
|
|
282
|
+
["nice", { valued: new Set(["-n", "--adjustment"]) }],
|
|
283
|
+
["timeout", { valued: new Set(["-k", "-s", "--kill-after", "--signal"]), operands: 1 }],
|
|
284
|
+
["stdbuf", { valued: new Set(["-e", "-i", "-o", "--error", "--input", "--output"]) }],
|
|
285
|
+
["exec", { valued: new Set(["-a"]) }],
|
|
286
|
+
["command", { valued: NONE, lookup: new Set(["-V", "-v"]) }],
|
|
287
|
+
["time", { valued: new Set(["-f", "-o", "--format", "--output"]) }],
|
|
288
|
+
["nohup", { valued: NONE }]
|
|
289
|
+
]);
|
|
290
|
+
var ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
291
|
+
function readOption(words, at, wrapper) {
|
|
292
|
+
const word = words[at];
|
|
293
|
+
if (word.startsWith("--")) {
|
|
294
|
+
const eq = word.indexOf("=");
|
|
295
|
+
const name = eq === -1 ? word : word.slice(0, eq);
|
|
296
|
+
if (!wrapper.valued.has(name))
|
|
297
|
+
return null;
|
|
298
|
+
return eq === -1 ? { name, value: words[at + 1], span: 2 } : { name, value: word.slice(eq + 1), span: 1 };
|
|
299
|
+
}
|
|
300
|
+
for (let j = 1;j < word.length; j++) {
|
|
301
|
+
const name = `-${word[j]}`;
|
|
302
|
+
if (wrapper.lookup?.has(name))
|
|
303
|
+
return "lookup";
|
|
304
|
+
if (!wrapper.valued.has(name))
|
|
305
|
+
continue;
|
|
306
|
+
const attached = word.slice(j + 1);
|
|
307
|
+
return attached ? { name, value: attached, span: 1 } : { name, value: words[at + 1], span: 2 };
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
function commandWords(input) {
|
|
312
|
+
const { argv, directories } = stripWrappers(input);
|
|
313
|
+
return { argv, directories };
|
|
314
|
+
}
|
|
315
|
+
function stripWrappers(input) {
|
|
316
|
+
const words = [...input];
|
|
317
|
+
const directories = [];
|
|
318
|
+
const wrappers = [];
|
|
319
|
+
let i = 0;
|
|
320
|
+
while (i < words.length) {
|
|
321
|
+
const word = words[i];
|
|
322
|
+
if (ASSIGNMENT.test(word) || word === "--") {
|
|
323
|
+
i++;
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const wrapper = WRAPPERS.get(word.slice(word.lastIndexOf("/") + 1));
|
|
327
|
+
if (wrapper === undefined)
|
|
328
|
+
break;
|
|
329
|
+
wrappers.push(word.slice(word.lastIndexOf("/") + 1));
|
|
330
|
+
i++;
|
|
331
|
+
while (i < words.length) {
|
|
332
|
+
const option = words[i];
|
|
333
|
+
if (option === "--") {
|
|
334
|
+
i++;
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
if (wrapper.assignments && ASSIGNMENT.test(option)) {
|
|
338
|
+
i++;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (!option.startsWith("-"))
|
|
342
|
+
break;
|
|
343
|
+
const read = readOption(words, i, wrapper);
|
|
344
|
+
if (read === "lookup")
|
|
345
|
+
return { argv: [], directories, wrappers };
|
|
346
|
+
if (read === null) {
|
|
347
|
+
i++;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (read.value === undefined)
|
|
351
|
+
return { argv: [], directories, wrappers };
|
|
352
|
+
if (wrapper.split?.has(read.name)) {
|
|
353
|
+
words.splice(i, read.span, ...tokenizeShell(read.value).map((token) => token.value));
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (wrapper.chdir?.has(read.name))
|
|
357
|
+
directories.push(read.value);
|
|
358
|
+
i += read.span;
|
|
359
|
+
}
|
|
360
|
+
i += Math.min(wrapper.operands ?? 0, words.length - i);
|
|
361
|
+
}
|
|
362
|
+
return { argv: words.slice(i), directories, wrappers };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// lib/src/shell-write-targets.ts
|
|
366
|
+
var SEPARATORS2 = {
|
|
367
|
+
";": true,
|
|
368
|
+
"&": true,
|
|
369
|
+
"|": true,
|
|
370
|
+
"\n": true,
|
|
371
|
+
"(": true,
|
|
372
|
+
")": true,
|
|
373
|
+
"$(": true
|
|
374
|
+
};
|
|
375
|
+
function lexicalAbs(target, cwd) {
|
|
376
|
+
let expanded = target.startsWith("~") ? homedir() + target.slice(1) : target;
|
|
377
|
+
if (!isAbsolute(expanded))
|
|
378
|
+
expanded = resolve(cwd, expanded);
|
|
379
|
+
const parts = [];
|
|
380
|
+
for (const segment of expanded.split(/[/\\]/)) {
|
|
381
|
+
if (segment === "" || segment === ".")
|
|
382
|
+
continue;
|
|
383
|
+
if (segment === "..") {
|
|
384
|
+
parts.pop();
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
parts.push(segment);
|
|
388
|
+
}
|
|
389
|
+
return `/${parts.join("/")}`;
|
|
390
|
+
}
|
|
391
|
+
function under(child, parent) {
|
|
392
|
+
if (!parent)
|
|
393
|
+
return false;
|
|
394
|
+
const rel = relative(parent, child);
|
|
395
|
+
return rel === "" || !rel.startsWith("..") && !isAbsolute(rel);
|
|
396
|
+
}
|
|
397
|
+
function teePaths(args) {
|
|
398
|
+
const at = args.indexOf("--");
|
|
399
|
+
const options = at === -1 ? args : args.slice(0, at);
|
|
400
|
+
const rest = at === -1 ? [] : args.slice(at + 1);
|
|
401
|
+
return [...options.filter((arg) => !arg.startsWith("-") || arg === "-"), ...rest].filter((arg) => arg !== "-");
|
|
402
|
+
}
|
|
403
|
+
function copyTargets(args) {
|
|
404
|
+
const positional = [];
|
|
405
|
+
let targetDir;
|
|
406
|
+
let noTargetDir = false;
|
|
407
|
+
for (let i = 0;i < args.length; i++) {
|
|
408
|
+
const arg = args[i];
|
|
409
|
+
if (arg === "--") {
|
|
410
|
+
positional.push(...args.slice(i + 1));
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
if (arg === "-t" || arg === "--target-directory")
|
|
414
|
+
targetDir = args[++i];
|
|
415
|
+
else if (arg.startsWith("--target-directory="))
|
|
416
|
+
targetDir = arg.slice("--target-directory=".length);
|
|
417
|
+
else if (/^-t./.test(arg))
|
|
418
|
+
targetDir = arg.slice(2);
|
|
419
|
+
else if (arg === "-T" || arg === "--no-target-directory")
|
|
420
|
+
noTargetDir = true;
|
|
421
|
+
else if (arg === "-S")
|
|
422
|
+
i++;
|
|
423
|
+
else if (!arg.startsWith("-") || arg === "-")
|
|
424
|
+
positional.push(arg);
|
|
425
|
+
}
|
|
426
|
+
if (targetDir)
|
|
427
|
+
return [{ path: targetDir, sources: positional, intoDir: true }];
|
|
428
|
+
if (positional.length < 2)
|
|
429
|
+
return [];
|
|
430
|
+
const dest = positional[positional.length - 1];
|
|
431
|
+
return [noTargetDir ? { path: dest } : { path: dest, sources: positional.slice(0, -1) }];
|
|
432
|
+
}
|
|
433
|
+
function sedPaths(args) {
|
|
434
|
+
let inplace = false;
|
|
435
|
+
let script = false;
|
|
436
|
+
const paths = [];
|
|
437
|
+
for (let i = 0;i < args.length; i++) {
|
|
438
|
+
const token = args[i];
|
|
439
|
+
if (token === "--") {
|
|
440
|
+
paths.push(...args.slice(i + 1 + (script ? 0 : 1)));
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
if (token === "-e" || token === "-f" || token === "--expression" || token === "--file") {
|
|
444
|
+
script = true;
|
|
445
|
+
i++;
|
|
446
|
+
} else if (/^--(?:expression|file)=/.test(token) || /^-[ef].+/.test(token)) {
|
|
447
|
+
script = true;
|
|
448
|
+
} else if (/^--in-place(?:=|$)/.test(token) || /^-[a-zA-Z]*i/.test(token)) {
|
|
449
|
+
inplace = true;
|
|
450
|
+
if (token === "-i" && args[i + 1] === "")
|
|
451
|
+
i++;
|
|
452
|
+
} else if (!token.startsWith("-")) {
|
|
453
|
+
if (!script)
|
|
454
|
+
script = true;
|
|
455
|
+
else
|
|
456
|
+
paths.push(token);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return inplace ? paths : [];
|
|
460
|
+
}
|
|
461
|
+
function perlPaths(args) {
|
|
462
|
+
let inplace = false;
|
|
463
|
+
let script = false;
|
|
464
|
+
let positional = false;
|
|
465
|
+
const files = [];
|
|
466
|
+
for (let i = 0;i < args.length; i++) {
|
|
467
|
+
const arg = args[i];
|
|
468
|
+
if (!positional && arg === "--") {
|
|
469
|
+
positional = true;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (!positional && arg.startsWith("-") && arg.length > 1) {
|
|
473
|
+
for (let j = 1;j < arg.length; j++) {
|
|
474
|
+
const flag = arg[j];
|
|
475
|
+
if (flag === "e" || flag === "E") {
|
|
476
|
+
script = true;
|
|
477
|
+
if (j === arg.length - 1)
|
|
478
|
+
i++;
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
if (flag === "i") {
|
|
482
|
+
inplace = true;
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
if ("MmIdDxlo0C".includes(flag))
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
positional = true;
|
|
491
|
+
if (!script)
|
|
492
|
+
script = true;
|
|
493
|
+
else
|
|
494
|
+
files.push(arg);
|
|
495
|
+
}
|
|
496
|
+
return inplace ? files : [];
|
|
497
|
+
}
|
|
498
|
+
function simpleCommandTargets(words, normalizeWords) {
|
|
499
|
+
const redirects = [];
|
|
500
|
+
const args = [];
|
|
501
|
+
for (let i = 0;i < words.length; i++) {
|
|
502
|
+
const word = words[i];
|
|
503
|
+
const value = word.value;
|
|
504
|
+
if (word.startsQuoted) {
|
|
505
|
+
args.push(value);
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (value.startsWith("<")) {
|
|
509
|
+
if (value === "<" || value === "<<<")
|
|
510
|
+
i++;
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
const at = value.indexOf(">");
|
|
514
|
+
if (at === -1) {
|
|
515
|
+
args.push(value);
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
const before = value.slice(0, at);
|
|
519
|
+
if (before && !/^\d+$/.test(before))
|
|
520
|
+
args.push(before);
|
|
521
|
+
let target = value.slice(at + 1);
|
|
522
|
+
if (target.startsWith(">") || target.startsWith("|"))
|
|
523
|
+
target = target.slice(1);
|
|
524
|
+
if (!target)
|
|
525
|
+
target = words[++i]?.value ?? "";
|
|
526
|
+
if (target)
|
|
527
|
+
redirects.push({ path: target });
|
|
528
|
+
}
|
|
529
|
+
const { argv, directories } = commandWords(normalizeWords?.(args) ?? args);
|
|
530
|
+
const name = basename(argv[0] ?? "");
|
|
531
|
+
const rest = argv.slice(1);
|
|
532
|
+
const writes = [];
|
|
533
|
+
if (name === "tee")
|
|
534
|
+
writes.push(...teePaths(rest).map((path) => ({ path })));
|
|
535
|
+
else if (name === "cp" || name === "mv")
|
|
536
|
+
writes.push(...copyTargets(rest));
|
|
537
|
+
else if (name === "sed" || name === "gsed")
|
|
538
|
+
writes.push(...sedPaths(rest).map((path) => ({ path })));
|
|
539
|
+
else if (name === "perl")
|
|
540
|
+
writes.push(...perlPaths(rest).map((path) => ({ path })));
|
|
541
|
+
return { argv, directories, redirects, writes };
|
|
542
|
+
}
|
|
543
|
+
function literalPath(raw, cwd) {
|
|
544
|
+
const path = raw.replace(/^\$\{?HOME\}?(?=\/|$)/, homedir());
|
|
545
|
+
if (/[$`]/.test(path))
|
|
546
|
+
return;
|
|
547
|
+
if (path.startsWith("~") || isAbsolute(path))
|
|
548
|
+
return lexicalAbs(path, "/");
|
|
549
|
+
return cwd === null ? undefined : lexicalAbs(path, cwd);
|
|
550
|
+
}
|
|
551
|
+
function isDirectory(path) {
|
|
552
|
+
try {
|
|
553
|
+
return statSync(path).isDirectory();
|
|
554
|
+
} catch {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function cdTarget(args, cwd) {
|
|
559
|
+
const dirs = args.filter((arg) => !/^-[LPe@]+$/.test(arg));
|
|
560
|
+
if (dirs.length === 0)
|
|
561
|
+
return homedir();
|
|
562
|
+
const dir = dirs[0];
|
|
563
|
+
if (dir === "-")
|
|
564
|
+
return null;
|
|
565
|
+
return literalPath(dir, cwd) ?? null;
|
|
566
|
+
}
|
|
567
|
+
function shellWriteTargets(command, cwd, additionalWrites, normalizeWords) {
|
|
568
|
+
const out = [];
|
|
569
|
+
let dir = cwd;
|
|
570
|
+
const saved = [];
|
|
571
|
+
let words = [];
|
|
572
|
+
const resolveInto = (targets, from) => {
|
|
573
|
+
for (const target of targets) {
|
|
574
|
+
const abs = literalPath(target.path, from);
|
|
575
|
+
if (abs === undefined)
|
|
576
|
+
continue;
|
|
577
|
+
const intoDir = target.intoDir || target.sources && (target.path.endsWith("/") || isDirectory(abs));
|
|
578
|
+
if (intoDir && target.sources) {
|
|
579
|
+
for (const source of target.sources)
|
|
580
|
+
out.push(join(abs, basename(source)));
|
|
581
|
+
} else {
|
|
582
|
+
out.push(abs);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
const finish = () => {
|
|
587
|
+
const segment = words;
|
|
588
|
+
words = [];
|
|
589
|
+
if (segment.length === 0)
|
|
590
|
+
return;
|
|
591
|
+
const { argv, directories, redirects, writes } = simpleCommandTargets(segment, normalizeWords);
|
|
592
|
+
resolveInto(redirects, dir);
|
|
593
|
+
if (argv[0] === "cd") {
|
|
594
|
+
dir = cdTarget(argv.slice(1), dir);
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
let commandDir = dir;
|
|
598
|
+
for (const directory of directories)
|
|
599
|
+
commandDir = literalPath(directory, commandDir) ?? null;
|
|
600
|
+
writes.push(...(additionalWrites?.(argv, commandDir) ?? []).map((path) => ({ path })));
|
|
601
|
+
resolveInto(writes, commandDir);
|
|
602
|
+
};
|
|
603
|
+
for (const token of tokenizeShell(command, { hereDocumentSubstitutionsOnly: true })) {
|
|
604
|
+
if (token.sawQuote || !Object.hasOwn(SEPARATORS2, token.value)) {
|
|
605
|
+
words.push(token);
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
finish();
|
|
609
|
+
if (token.value === "(" || token.value === "$(")
|
|
610
|
+
saved.push(dir);
|
|
611
|
+
else if (token.value === ")" && saved.length > 0)
|
|
612
|
+
dir = saved.pop() ?? null;
|
|
613
|
+
}
|
|
614
|
+
finish();
|
|
615
|
+
return [...new Set(out)];
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// lib/src/tool-targets.ts
|
|
619
|
+
var WRITE_TOOLS = new Set(["edit", "write", "patch"]);
|
|
620
|
+
var READ_TOOLS = new Set(["read"]);
|
|
621
|
+
var SHELL_TOOL = "shell";
|
|
622
|
+
var SHELL_ALIASES = new Set(["bash", "Bash"]);
|
|
623
|
+
function isShellTool(tool) {
|
|
624
|
+
return tool === SHELL_TOOL || SHELL_ALIASES.has(tool);
|
|
625
|
+
}
|
|
626
|
+
var SHELL_EXECUTABLES = new Set(["bash", "sh", "zsh", "dash", "ksh"]);
|
|
627
|
+
var SAFE_WORD = /^[A-Za-z0-9_@%+=:,./-]+$/;
|
|
628
|
+
function quoteWord(word) {
|
|
629
|
+
return SAFE_WORD.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
|
|
630
|
+
}
|
|
631
|
+
function commandText(value) {
|
|
632
|
+
if (typeof value === "string")
|
|
633
|
+
return value;
|
|
634
|
+
if (!Array.isArray(value) || value.length === 0 || !value.every((word) => typeof word === "string"))
|
|
635
|
+
return;
|
|
636
|
+
const argv = value;
|
|
637
|
+
const executable = argv[0]?.split("/").pop() ?? "";
|
|
638
|
+
if (argv.length === 3 && SHELL_EXECUTABLES.has(executable) && /^-l?c$/.test(argv[1] ?? ""))
|
|
639
|
+
return argv[2];
|
|
640
|
+
return argv.map(quoteWord).join(" ");
|
|
641
|
+
}
|
|
642
|
+
function flag(value) {
|
|
643
|
+
if (typeof value === "boolean")
|
|
644
|
+
return value;
|
|
645
|
+
if (value === "true")
|
|
646
|
+
return true;
|
|
647
|
+
if (value === "false")
|
|
648
|
+
return false;
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
function milliseconds(value) {
|
|
652
|
+
const parsed = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
|
|
653
|
+
return typeof parsed === "number" && Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : undefined;
|
|
654
|
+
}
|
|
655
|
+
function shellAliasInput(input) {
|
|
656
|
+
let raw = input;
|
|
657
|
+
if (typeof raw === "string") {
|
|
658
|
+
try {
|
|
659
|
+
raw = JSON.parse(raw);
|
|
660
|
+
} catch {
|
|
661
|
+
return input;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
const value = fields(raw);
|
|
665
|
+
const command = commandText(value.command ?? value.cmd);
|
|
666
|
+
if (command === undefined)
|
|
667
|
+
return input;
|
|
668
|
+
const out = { command };
|
|
669
|
+
const workdir = [value.workdir, value.cwd, value.working_directory].find((dir) => typeof dir === "string" && dir !== "");
|
|
670
|
+
if (workdir !== undefined)
|
|
671
|
+
out.workdir = workdir;
|
|
672
|
+
const timeout = milliseconds(value.timeout ?? value.timeout_ms);
|
|
673
|
+
if (timeout !== undefined)
|
|
674
|
+
out.timeout = timeout;
|
|
675
|
+
const background = flag(value.background ?? value.run_in_background);
|
|
676
|
+
if (background !== undefined)
|
|
677
|
+
out.background = background;
|
|
678
|
+
return out;
|
|
679
|
+
}
|
|
680
|
+
var PATCH_HEADER = /^\*\*\* (?:(?<verb>Add|Update|Delete) File|(?<move>Move to)):\s*(?<path>.+?)\s*$/;
|
|
681
|
+
function unquote(path) {
|
|
682
|
+
const first = path[0];
|
|
683
|
+
if (path.length > 1 && (first === '"' || first === "'") && path.endsWith(first))
|
|
684
|
+
return path.slice(1, -1);
|
|
685
|
+
return path;
|
|
686
|
+
}
|
|
687
|
+
function fields(input) {
|
|
688
|
+
return input !== null && typeof input === "object" ? input : {};
|
|
689
|
+
}
|
|
690
|
+
function patchTargets(patchText) {
|
|
691
|
+
const out = [];
|
|
692
|
+
for (const raw of patchText.split(`
|
|
693
|
+
`)) {
|
|
694
|
+
const match = PATCH_HEADER.exec(raw.replace(/\r$/, ""));
|
|
695
|
+
const path = match?.groups?.path;
|
|
696
|
+
if (match === null || path === undefined)
|
|
697
|
+
continue;
|
|
698
|
+
const operation = match.groups?.move ? "move" : match.groups?.verb?.toLowerCase();
|
|
699
|
+
const target = unquote(path);
|
|
700
|
+
if (target.length > 0)
|
|
701
|
+
out.push({ operation, path: target });
|
|
702
|
+
}
|
|
703
|
+
return out;
|
|
704
|
+
}
|
|
705
|
+
function patchPaths(patchText) {
|
|
706
|
+
return [...new Set(patchTargets(patchText).map((target) => target.path))];
|
|
707
|
+
}
|
|
708
|
+
function writeTargets(tool, input) {
|
|
709
|
+
const value = fields(input);
|
|
710
|
+
if (tool === "edit" || tool === "write")
|
|
711
|
+
return typeof value.path === "string" && value.path ? [value.path] : [];
|
|
712
|
+
if (tool === "patch")
|
|
713
|
+
return typeof value.patchText === "string" ? patchPaths(value.patchText) : [];
|
|
714
|
+
return [];
|
|
715
|
+
}
|
|
716
|
+
function shellCall(tool, input) {
|
|
717
|
+
if (!isShellTool(tool))
|
|
718
|
+
return;
|
|
719
|
+
const value = fields(tool === SHELL_TOOL ? input : shellAliasInput(input));
|
|
720
|
+
if (typeof value.command !== "string")
|
|
721
|
+
return;
|
|
722
|
+
const call = { command: value.command };
|
|
723
|
+
if (typeof value.workdir === "string" && value.workdir)
|
|
724
|
+
call.workdir = value.workdir;
|
|
725
|
+
if (typeof value.timeout === "number")
|
|
726
|
+
call.timeout = value.timeout;
|
|
727
|
+
if (typeof value.background === "boolean")
|
|
728
|
+
call.background = value.background;
|
|
729
|
+
return call;
|
|
730
|
+
}
|
|
731
|
+
function writtenPaths(tool, input, cwd) {
|
|
732
|
+
const shell = shellCall(tool, input);
|
|
733
|
+
if (shell !== undefined)
|
|
734
|
+
return shellWriteTargets(shell.command, shell.workdir ? lexicalAbs(shell.workdir, cwd) : cwd);
|
|
735
|
+
return [...new Set(writeTargets(tool, input).map((path) => lexicalAbs(path, cwd)))];
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// plugins/chezmoi/shared/guard.ts
|
|
739
|
+
var SUBPROCESS_TIMEOUT_MS = 2000;
|
|
740
|
+
var MANAGED_TTL_MS = 5000;
|
|
741
|
+
var ATTRIBUTE_PREFIXES = new Set([
|
|
742
|
+
"encrypted",
|
|
743
|
+
"private",
|
|
744
|
+
"readonly",
|
|
745
|
+
"empty",
|
|
746
|
+
"executable",
|
|
747
|
+
"exact",
|
|
748
|
+
"external",
|
|
749
|
+
"create",
|
|
750
|
+
"modify",
|
|
751
|
+
"remove",
|
|
752
|
+
"symlink",
|
|
753
|
+
"run",
|
|
754
|
+
"once",
|
|
755
|
+
"onchange",
|
|
756
|
+
"before",
|
|
757
|
+
"after"
|
|
758
|
+
]);
|
|
759
|
+
function isSeedOnceSource(source) {
|
|
760
|
+
let name = basename2(source);
|
|
761
|
+
for (;; ) {
|
|
762
|
+
const cut = name.indexOf("_");
|
|
763
|
+
if (cut <= 0 || !ATTRIBUTE_PREFIXES.has(name.slice(0, cut)))
|
|
764
|
+
return false;
|
|
765
|
+
if (name.slice(0, cut) === "create")
|
|
766
|
+
return true;
|
|
767
|
+
name = name.slice(cut + 1);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
var CHEZMOI_RUN = /(?:^|[\s;&|(/])chezmoi(?:\s|$)/;
|
|
771
|
+
var spawnChezmoi = (args, globalArgs = []) => new Promise((resolve) => {
|
|
772
|
+
let stdout = "";
|
|
773
|
+
let settled = false;
|
|
774
|
+
const child = spawnProcess("chezmoi", [...globalArgs, ...args], { stdio: ["ignore", "pipe", "ignore"] });
|
|
775
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), SUBPROCESS_TIMEOUT_MS);
|
|
776
|
+
child.stdout.setEncoding("utf8");
|
|
777
|
+
child.stdout.on("data", (chunk) => {
|
|
778
|
+
stdout += chunk;
|
|
779
|
+
});
|
|
780
|
+
child.on("error", () => {
|
|
781
|
+
if (settled)
|
|
782
|
+
return;
|
|
783
|
+
settled = true;
|
|
784
|
+
clearTimeout(timer);
|
|
785
|
+
resolve(null);
|
|
786
|
+
});
|
|
787
|
+
child.on("close", (code) => {
|
|
788
|
+
if (settled)
|
|
789
|
+
return;
|
|
790
|
+
settled = true;
|
|
791
|
+
clearTimeout(timer);
|
|
792
|
+
resolve(code === 0 ? stdout : null);
|
|
793
|
+
});
|
|
794
|
+
});
|
|
795
|
+
function normalized(path) {
|
|
796
|
+
try {
|
|
797
|
+
return realpathSync(path);
|
|
798
|
+
} catch {
|
|
799
|
+
return path;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
function refusal(target, source) {
|
|
803
|
+
const hint = source ? `'${source}'` : `the path \`chezmoi source-path ${target}\` prints`;
|
|
804
|
+
return `'${target}' is a chezmoi-managed target, not the source; the next chezmoi apply overwrites it. ` + `Edit the source at ${hint}, then run chezmoi apply. Do not write the live home-directory copy.`;
|
|
805
|
+
}
|
|
806
|
+
function createGuard(options = {}) {
|
|
807
|
+
const spawn = options.spawn ?? spawnChezmoi;
|
|
808
|
+
const now = options.now ?? Date.now;
|
|
809
|
+
const home = options.home ?? homedir2();
|
|
810
|
+
const ttl = options.ttlMs ?? MANAGED_TTL_MS;
|
|
811
|
+
let managed = null;
|
|
812
|
+
let aliases = null;
|
|
813
|
+
let managedAt = 0;
|
|
814
|
+
let sourceDir;
|
|
815
|
+
let unavailableNotified = false;
|
|
816
|
+
const notifyUnavailable = () => {
|
|
817
|
+
if (unavailableNotified)
|
|
818
|
+
return;
|
|
819
|
+
unavailableNotified = true;
|
|
820
|
+
options.onUnavailable?.();
|
|
821
|
+
};
|
|
822
|
+
const invalidate = () => {
|
|
823
|
+
managed = null;
|
|
824
|
+
aliases = null;
|
|
825
|
+
};
|
|
826
|
+
const loadSourceDir = async () => {
|
|
827
|
+
if (sourceDir !== undefined)
|
|
828
|
+
return sourceDir;
|
|
829
|
+
const out = await spawn(["source-path"], options.globalArgs);
|
|
830
|
+
if (out === null)
|
|
831
|
+
notifyUnavailable();
|
|
832
|
+
sourceDir = out?.trim() || null;
|
|
833
|
+
return sourceDir;
|
|
834
|
+
};
|
|
835
|
+
const loadManaged = async () => {
|
|
836
|
+
if (managed && now() - managedAt < ttl)
|
|
837
|
+
return managed;
|
|
838
|
+
const out = await spawn(["managed", "--path-style=absolute"], options.globalArgs);
|
|
839
|
+
if (out === null) {
|
|
840
|
+
notifyUnavailable();
|
|
841
|
+
return null;
|
|
842
|
+
}
|
|
843
|
+
managed = new Set(out.split(`
|
|
844
|
+
`).map((line) => line.trim()).filter(Boolean));
|
|
845
|
+
aliases = null;
|
|
846
|
+
managedAt = now();
|
|
847
|
+
return managed;
|
|
848
|
+
};
|
|
849
|
+
const managedTarget = (set, abs) => {
|
|
850
|
+
if (set.has(abs))
|
|
851
|
+
return abs;
|
|
852
|
+
aliases ??= new Map([...set].map((path) => [normalized(path), path]));
|
|
853
|
+
return aliases.get(normalized(abs));
|
|
854
|
+
};
|
|
855
|
+
const considerPath = async (abs) => {
|
|
856
|
+
if (!under(abs, home) && !under(normalized(abs), normalized(home)))
|
|
857
|
+
return;
|
|
858
|
+
const source = await loadSourceDir();
|
|
859
|
+
if (source && (under(abs, source) || under(normalized(abs), normalized(source)))) {
|
|
860
|
+
invalidate();
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
const set = await loadManaged();
|
|
864
|
+
if (!set)
|
|
865
|
+
return;
|
|
866
|
+
const target = managedTarget(set, abs);
|
|
867
|
+
if (!target)
|
|
868
|
+
return;
|
|
869
|
+
const targetSource = (await spawn(["source-path", target], options.globalArgs))?.trim() || undefined;
|
|
870
|
+
if (targetSource && isSeedOnceSource(targetSource))
|
|
871
|
+
return;
|
|
872
|
+
return refusal(abs, targetSource);
|
|
873
|
+
};
|
|
874
|
+
return {
|
|
875
|
+
invalidate,
|
|
876
|
+
decide: async (tool, input, cwd) => {
|
|
877
|
+
const shell = shellCall(tool, input);
|
|
878
|
+
if (shell && CHEZMOI_RUN.test(shell.command))
|
|
879
|
+
invalidate();
|
|
880
|
+
for (const abs of writtenPaths(tool, input, cwd)) {
|
|
881
|
+
const reason = await considerPath(abs);
|
|
882
|
+
if (reason)
|
|
883
|
+
return reason;
|
|
884
|
+
}
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// plugins/chezmoi/pi-src/chezmoi.ts
|
|
891
|
+
var UNAVAILABLE_NOTICE = "chezmoi: unable to read the managed file list; writes are allowed until chezmoi is available.";
|
|
892
|
+
function createExtension(pi, options = {}) {
|
|
893
|
+
let context;
|
|
894
|
+
const guard = createGuard({
|
|
895
|
+
...options,
|
|
896
|
+
onUnavailable: () => {
|
|
897
|
+
options.onUnavailable?.();
|
|
898
|
+
if (context?.hasUI)
|
|
899
|
+
context.ui.notify(UNAVAILABLE_NOTICE, "warning");
|
|
900
|
+
else
|
|
901
|
+
process.stderr.write(`${UNAVAILABLE_NOTICE}
|
|
902
|
+
`);
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
906
|
+
if (event.toolName !== "write" && event.toolName !== "edit" && event.toolName !== "bash")
|
|
907
|
+
return;
|
|
908
|
+
context = ctx;
|
|
909
|
+
const reason = await guard.decide(event.toolName, event.input, ctx.cwd);
|
|
910
|
+
if (reason)
|
|
911
|
+
return { block: true, reason };
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
function chezmoi_default(pi) {
|
|
915
|
+
createExtension(pi);
|
|
916
|
+
}
|
|
917
|
+
export {
|
|
918
|
+
createExtension,
|
|
919
|
+
chezmoi_default as default
|
|
920
|
+
};
|