forked from mantou132/browser4agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·61 lines (51 loc) · 1.85 KB
/
Copy pathcli.js
File metadata and controls
executable file
·61 lines (51 loc) · 1.85 KB
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
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import { parseToolsetJs } from './index.js';
const args = process.argv.slice(2);
if (args.length === 0) {
console.error('Usage: toolset-parser <input.js|input.ts|input-dir> [output.json|output-dir]');
process.exit(1);
}
const inputPath = path.resolve(args[0]);
const outputPath = args[1] ? path.resolve(args[1]) : undefined;
try {
const parsed = fs.statSync(inputPath).isDirectory()
? parseDir(inputPath, outputPath)
: [parseFile(inputPath, outputPath)];
if (!parsed.length) console.log(`No .js/.ts toolsets found in ${inputPath}`);
for (const { inputPath, outputPath, result } of parsed) {
fs.writeFileSync(outputPath, `${JSON.stringify(result, null, 2)}\n`);
console.log(`Parsed ${inputPath} -> ${outputPath}`);
}
} catch (err) {
if (err.name === 'ValidationError') {
console.error('Validation error:', err.message);
process.exit(1);
}
throw err;
}
function parseDir(inputDir, outputDir = inputDir) {
const entries = fs
.readdirSync(inputDir, { withFileTypes: true })
.filter((entry) => entry.isFile() && isToolsetSource(entry.name))
.map((entry) => entry.name)
.sort();
fs.mkdirSync(outputDir, { recursive: true });
return entries.map((name) =>
parseFile(path.join(inputDir, name), path.join(outputDir, name.replace(/\.[jt]s$/, '.json'))),
);
}
function parseFile(inputPath, outputPath = inputPath.replace(/\.[jt]s$/, '.json')) {
if (!isToolsetSource(inputPath)) throw new Error(`Input file must use .js or .ts extension: ${inputPath}`);
const content = fs.readFileSync(inputPath, 'utf8');
try {
return { inputPath, outputPath, result: parseToolsetJs(content, inputPath) };
} catch (err) {
err.message = `${inputPath}: ${err.message}`;
throw err;
}
}
function isToolsetSource(file) {
return /\.[jt]s$/.test(file);
}