@zgeoff/skill-gate 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +85 -2
- package/dist/build-bash-skill-gate-output.d.ts +4 -0
- package/dist/build-skill-gate-output.d.ts +2 -0
- package/dist/collect-gate-skills.d.ts +2 -0
- package/dist/collect-loaded-skills.d.ts +2 -0
- package/dist/collect-written-paths.d.ts +2 -0
- package/dist/format-skill-list.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/parse-shell-commands.d.ts +2 -0
- package/dist/parse-skill-gate-rules.d.ts +2 -0
- package/dist/plan-bash-skill-gate.d.ts +2 -0
- package/dist/plan-skill-gate.d.ts +2 -0
- package/dist/run-skill-gate.d.ts +1 -0
- package/dist/types.d.ts +79 -0
- package/package.json +47 -4
- package/src/build-bash-skill-gate-output.ts +41 -0
- package/src/build-skill-gate-output.ts +54 -0
- package/src/collect-gate-skills.ts +19 -0
- package/src/collect-loaded-skills.ts +98 -0
- package/src/collect-written-paths.ts +451 -0
- package/src/format-skill-list.ts +10 -0
- package/src/index.ts +1 -0
- package/src/parse-shell-commands.ts +429 -0
- package/src/parse-skill-gate-rules.ts +72 -0
- package/src/plan-bash-skill-gate.ts +21 -0
- package/src/plan-skill-gate.ts +21 -0
- package/src/run-skill-gate.ts +235 -0
- package/src/types.ts +96 -0
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { parseShellCommands } from './parse-shell-commands.ts';
|
|
3
|
+
import type { ShellCommand, ShellWord, WrittenPath } from './types.ts';
|
|
4
|
+
|
|
5
|
+
// Collects the paths a Bash command writes through a redirect, `tee`, `sed -i`, `perl -i`, `cp`,
|
|
6
|
+
// `mv`, `bash -c` or an inline script. A path built from a variable or a glob is left out, and a
|
|
7
|
+
// command that does not parse writes nothing: the gate guides an agent and is not a sandbox.
|
|
8
|
+
export function collectWrittenPaths(command: string): readonly WrittenPath[] {
|
|
9
|
+
return collectTextPaths(command, '');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// `dir` is the working directory relative to the command's start, or null after a `cd` the parser
|
|
13
|
+
// cannot follow, which leaves only absolute paths.
|
|
14
|
+
function collectTextPaths(text: string, dir: string | null): WrittenPath[] {
|
|
15
|
+
const commands = parseShellCommands(text) ?? [];
|
|
16
|
+
const paths: WrittenPath[] = [];
|
|
17
|
+
const parentDirs: (string | null)[] = [];
|
|
18
|
+
let current = dir;
|
|
19
|
+
|
|
20
|
+
for (const command of commands) {
|
|
21
|
+
const enteredFrom = current;
|
|
22
|
+
|
|
23
|
+
parentDirs.push(...Array.from({ length: command.enters }, () => enteredFrom));
|
|
24
|
+
paths.push(...collectCommandPaths(command, current));
|
|
25
|
+
|
|
26
|
+
current = resolveNextDir(command, current);
|
|
27
|
+
|
|
28
|
+
// Leaving a subshell drops its `cd`, so the parent's directory comes back.
|
|
29
|
+
for (let left = 0; left < command.leaves && parentDirs.length > 0; left += 1) {
|
|
30
|
+
current = parentDirs.pop() ?? null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return paths;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function collectCommandPaths(command: ShellCommand, dir: string | null): WrittenPath[] {
|
|
38
|
+
const redirected = command.redirects
|
|
39
|
+
.filter((redirect) => isWriteRedirect(redirect.operator, redirect.target))
|
|
40
|
+
.flatMap((redirect) => resolveWordPath(redirect.target, dir));
|
|
41
|
+
|
|
42
|
+
const words = getProgramWords(command.words);
|
|
43
|
+
const [program] = words;
|
|
44
|
+
|
|
45
|
+
if (program === undefined || !program.literal) {
|
|
46
|
+
return redirected;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const name = path.basename(program.text);
|
|
50
|
+
const args = words.slice(1);
|
|
51
|
+
|
|
52
|
+
return [...redirected, ...collectProgramPaths(name, args, command.stdin, dir)];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const WRITE_REDIRECTS = new Set(['>', '>>', '>|', '&>', '&>>', '<>']);
|
|
56
|
+
|
|
57
|
+
// `>&` sends output to a file unless its target is a file descriptor, as in `2>&1`.
|
|
58
|
+
function isWriteRedirect(operator: string, target: ShellWord): boolean {
|
|
59
|
+
return WRITE_REDIRECTS.has(operator) || (operator === '>&' && !/^(?:\d+|-)$/u.test(target.text));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function resolveWordPath(word: ShellWord | undefined, dir: string | null): WrittenPath[] {
|
|
63
|
+
if (word === undefined || !word.literal || word.text === '' || word.text === '-') {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (path.isAbsolute(word.text)) {
|
|
68
|
+
return [{ path: word.text }];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return dir === null ? [] : [{ path: path.join(dir, word.text) }];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const ASSIGNMENT = /^[A-Za-z_]\w*=/u;
|
|
75
|
+
|
|
76
|
+
// Each wrapper names the options that take the next argument as their value, such as `sudo -u`.
|
|
77
|
+
const WRAPPER_VALUE_OPTIONS: ReadonlyMap<string, ReadonlySet<string>> = new Map([
|
|
78
|
+
['sudo', new Set(['-u', '-g', '-C', '-D', '-p', '-r', '-t', '-T', '-U'])],
|
|
79
|
+
['env', new Set(['-u', '-C', '-S', '--unset', '--chdir', '--split-string'])],
|
|
80
|
+
['nice', new Set(['-n', '--adjustment'])],
|
|
81
|
+
['time', new Set(['-f', '-o', '--format', '--output'])],
|
|
82
|
+
['exec', new Set(['-a'])],
|
|
83
|
+
['command', new Set<string>()],
|
|
84
|
+
['builtin', new Set<string>()],
|
|
85
|
+
['nohup', new Set<string>()],
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
// Drops the variable assignments and wrappers such as `sudo` or `env` in front of the program.
|
|
89
|
+
function getProgramWords(words: readonly ShellWord[]): readonly ShellWord[] {
|
|
90
|
+
let index = 0;
|
|
91
|
+
|
|
92
|
+
while (index < words.length) {
|
|
93
|
+
const word = words[index];
|
|
94
|
+
|
|
95
|
+
if (word === undefined) {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (ASSIGNMENT.test(word.text)) {
|
|
100
|
+
index += 1;
|
|
101
|
+
} else if (word.literal && WRAPPER_VALUE_OPTIONS.has(word.text)) {
|
|
102
|
+
const valueOptions = WRAPPER_VALUE_OPTIONS.get(word.text) ?? new Set<string>();
|
|
103
|
+
|
|
104
|
+
index += 1;
|
|
105
|
+
|
|
106
|
+
while (words[index]?.text.startsWith('-') === true) {
|
|
107
|
+
index += valueOptions.has(words[index]?.text ?? '') ? 2 : 1;
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return words.slice(index);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const SHELLS = new Set(['bash', 'sh', 'zsh', 'dash']);
|
|
118
|
+
const INTERPRETERS = new Set(['python', 'python3', 'node', 'bun', 'deno', 'ruby']);
|
|
119
|
+
|
|
120
|
+
// Python, Ruby and Perl start a comment with `#`, where JavaScript has `//`, and Python's `//` is
|
|
121
|
+
// floor division.
|
|
122
|
+
const JS_RUNTIMES = new Set(['node', 'bun', 'deno']);
|
|
123
|
+
|
|
124
|
+
function collectProgramPaths(
|
|
125
|
+
name: string,
|
|
126
|
+
args: readonly ShellWord[],
|
|
127
|
+
stdin: string | null,
|
|
128
|
+
dir: string | null,
|
|
129
|
+
): WrittenPath[] {
|
|
130
|
+
if (name === 'sed' || name === 'gsed') {
|
|
131
|
+
return collectInPlacePaths(args, SED_SCRIPT_FLAGS).flatMap((word) =>
|
|
132
|
+
resolveWordPath(word, dir),
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (name === 'perl') {
|
|
137
|
+
return collectPerlPaths(args, stdin, dir);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (name === 'tee') {
|
|
141
|
+
return collectOperands(args).flatMap((word) => resolveWordPath(word, dir));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (name === 'cp' || name === 'mv') {
|
|
145
|
+
return collectCopyPaths(args, dir);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (SHELLS.has(name)) {
|
|
149
|
+
const script = findShellScript(args, stdin);
|
|
150
|
+
|
|
151
|
+
return script === null ? [] : collectTextPaths(script, dir);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (INTERPRETERS.has(name)) {
|
|
155
|
+
const script = findInlineScript(args, stdin);
|
|
156
|
+
const commentStart = JS_RUNTIMES.has(name) ? '//' : '#';
|
|
157
|
+
|
|
158
|
+
return script === null ? [] : collectScriptPaths(script, commentStart, dir);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const SED_SCRIPT_FLAGS = new Set(['e', 'f']);
|
|
165
|
+
|
|
166
|
+
// Collects the files that `sed -i` or `perl -i` rewrites: every operand after the script, or every
|
|
167
|
+
// operand when an `-e` or `-f` flag gives the script. Without `-i` (or `--in-place`), it writes
|
|
168
|
+
// nothing.
|
|
169
|
+
function collectInPlacePaths(
|
|
170
|
+
args: readonly ShellWord[],
|
|
171
|
+
scriptFlags: ReadonlySet<string>,
|
|
172
|
+
): readonly ShellWord[] {
|
|
173
|
+
let inPlace = false;
|
|
174
|
+
let hasScriptFlag = false;
|
|
175
|
+
let optionsEnded = false;
|
|
176
|
+
const operands: ShellWord[] = [];
|
|
177
|
+
|
|
178
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
179
|
+
const arg = args[index];
|
|
180
|
+
|
|
181
|
+
if (arg === undefined) {
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (optionsEnded || !arg.text.startsWith('-') || arg.text === '-') {
|
|
186
|
+
operands.push(arg);
|
|
187
|
+
} else if (arg.text === '--') {
|
|
188
|
+
optionsEnded = true;
|
|
189
|
+
} else if (arg.text.startsWith('--')) {
|
|
190
|
+
inPlace ||= arg.text === '--in-place' || arg.text.startsWith('--in-place=');
|
|
191
|
+
|
|
192
|
+
if (/^--(?:expression|file)(?:=|$)/u.test(arg.text)) {
|
|
193
|
+
hasScriptFlag = true;
|
|
194
|
+
index += arg.text.includes('=') ? 0 : 1;
|
|
195
|
+
}
|
|
196
|
+
} else {
|
|
197
|
+
const flags = parseShortFlags(arg.text, scriptFlags);
|
|
198
|
+
|
|
199
|
+
inPlace ||= flags.inPlace;
|
|
200
|
+
hasScriptFlag ||= flags.script;
|
|
201
|
+
index += flags.takesNext ? 1 : 0;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (!inPlace) {
|
|
206
|
+
return [];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return hasScriptFlag ? operands : operands.slice(1);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Reads a bundle of short flags such as `-ni` or `-pi.bak`. An `i` takes the rest of the bundle as
|
|
213
|
+
// its backup suffix. A script flag takes the rest as its value, or the next argument when it ends
|
|
214
|
+
// the bundle.
|
|
215
|
+
function parseShortFlags(
|
|
216
|
+
bundle: string,
|
|
217
|
+
scriptFlags: ReadonlySet<string>,
|
|
218
|
+
): { readonly inPlace: boolean; readonly script: boolean; readonly takesNext: boolean } {
|
|
219
|
+
for (let index = 1; index < bundle.length; index += 1) {
|
|
220
|
+
const flag = bundle.charAt(index);
|
|
221
|
+
|
|
222
|
+
if (flag === 'i') {
|
|
223
|
+
return { inPlace: true, script: false, takesNext: false };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (scriptFlags.has(flag)) {
|
|
227
|
+
return { inPlace: false, script: true, takesNext: index === bundle.length - 1 };
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return { inPlace: false, script: false, takesNext: false };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const PERL_SCRIPT_FLAGS = new Set(['e', 'E']);
|
|
235
|
+
|
|
236
|
+
function collectPerlPaths(
|
|
237
|
+
args: readonly ShellWord[],
|
|
238
|
+
stdin: string | null,
|
|
239
|
+
dir: string | null,
|
|
240
|
+
): WrittenPath[] {
|
|
241
|
+
const inPlace = collectInPlacePaths(args, PERL_SCRIPT_FLAGS);
|
|
242
|
+
|
|
243
|
+
if (inPlace.length > 0) {
|
|
244
|
+
return inPlace.flatMap((word) => resolveWordPath(word, dir));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const script = findInlineScript(args, stdin);
|
|
248
|
+
|
|
249
|
+
return script === null ? [] : collectScriptPaths(script, '#', dir);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function collectOperands(args: readonly ShellWord[]): readonly ShellWord[] {
|
|
253
|
+
return args.filter((arg) => !arg.text.startsWith('-'));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Collects where `cp` or `mv` writes: into the `-t` directory, into the destination when several
|
|
257
|
+
// sources or a trailing slash make it a directory, and otherwise onto the destination, or into it
|
|
258
|
+
// when it turns out to be a directory.
|
|
259
|
+
function collectCopyPaths(args: readonly ShellWord[], dir: string | null): WrittenPath[] {
|
|
260
|
+
const operands: ShellWord[] = [];
|
|
261
|
+
let targetDir: ShellWord | undefined;
|
|
262
|
+
let optionsEnded = false;
|
|
263
|
+
|
|
264
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
265
|
+
const arg = args[index];
|
|
266
|
+
|
|
267
|
+
if (arg === undefined) {
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (optionsEnded || !arg.text.startsWith('-') || arg.text === '-') {
|
|
272
|
+
operands.push(arg);
|
|
273
|
+
} else if (arg.text === '--') {
|
|
274
|
+
optionsEnded = true;
|
|
275
|
+
} else if (arg.text.startsWith('--target-directory=')) {
|
|
276
|
+
targetDir = { ...arg, text: arg.text.slice('--target-directory='.length) };
|
|
277
|
+
} else if (arg.text === '--target-directory' || /^-[^-]*t$/u.test(arg.text)) {
|
|
278
|
+
targetDir = args[index + 1];
|
|
279
|
+
index += 1;
|
|
280
|
+
} else if (arg.text === '-S' || arg.text === '--suffix') {
|
|
281
|
+
index += 1;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (targetDir !== undefined) {
|
|
286
|
+
return collectIntoDirPaths(targetDir, operands, dir);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const destination = operands.at(-1);
|
|
290
|
+
const sources = operands.slice(0, -1);
|
|
291
|
+
const [source] = sources;
|
|
292
|
+
|
|
293
|
+
if (destination === undefined || source === undefined) {
|
|
294
|
+
return [];
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (sources.length > 1 || destination.text.endsWith('/')) {
|
|
298
|
+
return collectIntoDirPaths(destination, sources, dir);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const fileName = source.literal ? path.basename(source.text) : undefined;
|
|
302
|
+
|
|
303
|
+
return resolveWordPath(destination, dir).map((written) =>
|
|
304
|
+
fileName === undefined ? written : { path: written.path, fileName },
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function collectIntoDirPaths(
|
|
309
|
+
target: ShellWord,
|
|
310
|
+
sources: readonly ShellWord[],
|
|
311
|
+
dir: string | null,
|
|
312
|
+
): WrittenPath[] {
|
|
313
|
+
return resolveWordPath(target, dir).flatMap((written) =>
|
|
314
|
+
sources
|
|
315
|
+
.filter((source) => source.literal)
|
|
316
|
+
.map((source) => ({ path: path.join(written.path, path.basename(source.text)) })),
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Finds the script a shell runs: the operand after `-c`, or the here document when the shell has
|
|
321
|
+
// no script file.
|
|
322
|
+
function findShellScript(args: readonly ShellWord[], stdin: string | null): string | null {
|
|
323
|
+
const hasCommandFlag = args.some((arg) => /^-[A-Za-z]*c[A-Za-z]*$/u.test(arg.text));
|
|
324
|
+
const [script] = collectOperands(args);
|
|
325
|
+
|
|
326
|
+
if (hasCommandFlag) {
|
|
327
|
+
return script?.literal === true ? script.text : null;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return script === undefined ? stdin : null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const INLINE_FLAGS = new Set(['-c', '-e', '-E', '--eval', '-p', '--print', 'eval']);
|
|
334
|
+
|
|
335
|
+
// Finds the code an interpreter runs from its arguments, as in `python3 -c`, `node -e` or
|
|
336
|
+
// `deno eval`, or from a here document when it reads its script from stdin.
|
|
337
|
+
function findInlineScript(args: readonly ShellWord[], stdin: string | null): string | null {
|
|
338
|
+
const flagIndex = args.findIndex((arg) => INLINE_FLAGS.has(arg.text));
|
|
339
|
+
const code = flagIndex === -1 ? undefined : args[flagIndex + 1];
|
|
340
|
+
|
|
341
|
+
if (code !== undefined) {
|
|
342
|
+
return code.literal ? code.text : null;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const readsStdin = args.length === 0 || args.some((arg) => arg.text === '-');
|
|
346
|
+
|
|
347
|
+
return readsStdin ? stdin : null;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Each pattern names the quoted path that a common file-writing call in a script takes.
|
|
351
|
+
const SCRIPT_WRITE_PATTERNS = [
|
|
352
|
+
/\b(?:writeFile|writeFileSync|appendFile|appendFileSync|createWriteStream)\(\s*(?<quote>['"`])(?<path>[^'"`]+)\k<quote>/gu,
|
|
353
|
+
/\bBun\.write\(\s*(?<quote>['"`])(?<path>[^'"`]+)\k<quote>/gu,
|
|
354
|
+
/\bopen\(\s*(?<quote>['"])(?<path>[^'"]+)\k<quote>\s*,\s*(?:mode\s*=\s*)?(?<modeQuote>['"])[^'"]*[wax+][^'"]*\k<modeQuote>/gu,
|
|
355
|
+
/\bPath\(\s*(?<quote>['"])(?<path>[^'"]+)\k<quote>\s*\)\.write_(?:text|bytes)\(/gu,
|
|
356
|
+
/\bFile\.write\(\s*(?<quote>['"])(?<path>[^'"]+)\k<quote>/gu,
|
|
357
|
+
/\bopen\s*\(?\s*(?:my\s+)?\$?\w+\s*,\s*['"](?:>|>>|\+<)['"]\s*,\s*(?<quote>['"])(?<path>[^'"]+)\k<quote>/gu,
|
|
358
|
+
];
|
|
359
|
+
|
|
360
|
+
function collectScriptPaths(
|
|
361
|
+
script: string,
|
|
362
|
+
commentStart: string,
|
|
363
|
+
dir: string | null,
|
|
364
|
+
): WrittenPath[] {
|
|
365
|
+
const textRanges = collectScriptTextRanges(script, commentStart);
|
|
366
|
+
|
|
367
|
+
return SCRIPT_WRITE_PATTERNS.flatMap((pattern) =>
|
|
368
|
+
[...script.matchAll(pattern)].flatMap((match) => {
|
|
369
|
+
const written = match.groups?.['path'] ?? '';
|
|
370
|
+
|
|
371
|
+
const isText = textRanges.some(
|
|
372
|
+
(range) => match.index > range.start && match.index < range.end,
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
return isText || /[$*?{]/u.test(written)
|
|
376
|
+
? []
|
|
377
|
+
: resolveWordPath({ text: written, literal: true }, dir);
|
|
378
|
+
}),
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Collects the spans of a script's string literals and comments, so a write call that only appears
|
|
383
|
+
// in text, such as `print("open('a.ts', 'w')")`, is not read as code.
|
|
384
|
+
function collectScriptTextRanges(
|
|
385
|
+
script: string,
|
|
386
|
+
commentStart: string,
|
|
387
|
+
): readonly { readonly start: number; readonly end: number }[] {
|
|
388
|
+
const ranges: { start: number; end: number }[] = [];
|
|
389
|
+
let index = 0;
|
|
390
|
+
|
|
391
|
+
while (index < script.length) {
|
|
392
|
+
const char = script.charAt(index);
|
|
393
|
+
|
|
394
|
+
if (commentStart === '//' && script.startsWith('/*', index)) {
|
|
395
|
+
const close = script.indexOf('*/', index + 2);
|
|
396
|
+
const end = close === -1 ? script.length : close + 2;
|
|
397
|
+
|
|
398
|
+
ranges.push({ start: index, end });
|
|
399
|
+
|
|
400
|
+
index = end;
|
|
401
|
+
} else if (script.startsWith(commentStart, index)) {
|
|
402
|
+
const newline = script.indexOf('\n', index);
|
|
403
|
+
const end = newline === -1 ? script.length : newline;
|
|
404
|
+
|
|
405
|
+
ranges.push({ start: index, end });
|
|
406
|
+
|
|
407
|
+
index = end;
|
|
408
|
+
} else if (char === "'" || char === '"' || char === '`') {
|
|
409
|
+
const end = getQuoteEndOrScriptEnd(script, index);
|
|
410
|
+
|
|
411
|
+
ranges.push({ start: index, end });
|
|
412
|
+
|
|
413
|
+
index = end;
|
|
414
|
+
} else {
|
|
415
|
+
index += 1;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return ranges;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function getQuoteEndOrScriptEnd(script: string, start: number): number {
|
|
423
|
+
const quote = script.charAt(start);
|
|
424
|
+
let index = start + 1;
|
|
425
|
+
|
|
426
|
+
while (index < script.length && script.charAt(index) !== quote) {
|
|
427
|
+
index += script.charAt(index) === '\\' ? 2 : 1;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return Math.min(index + 1, script.length);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function resolveNextDir(command: ShellCommand, dir: string | null): string | null {
|
|
434
|
+
const [program, ...args] = command.words;
|
|
435
|
+
|
|
436
|
+
if (program?.text !== 'cd') {
|
|
437
|
+
return dir;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const [target] = collectOperands(args);
|
|
441
|
+
|
|
442
|
+
if (target === undefined || !target.literal) {
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (path.isAbsolute(target.text)) {
|
|
447
|
+
return target.text;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return dir === null ? null : path.join(dir, target.text);
|
|
451
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function formatSkillList(skills: readonly string[]): string {
|
|
2
|
+
const names = skills.map((skill) => `\`${skill}\``);
|
|
3
|
+
const noun = names.length === 1 ? 'skill' : 'skills';
|
|
4
|
+
|
|
5
|
+
if (names.length === 1) {
|
|
6
|
+
return `${names[0]} ${noun}`;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return `${names.slice(0, -1).join(', ')} and ${names.at(-1)} ${noun}`;
|
|
10
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { runSkillGate } from './run-skill-gate.ts';
|