yinji/tools/ai/yinji-cli.mjs

184 lines
4.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
import path from 'node:path';
import process from 'node:process';
import { YinjiDriver } from './lib/yinji-driver.mjs';
function printHelp() {
console.log(`印迹 AI CLI
用法:
node tools/ai/yinji-cli.mjs smoke --pdf ./test-fixture.pdf [--seal demo]
node tools/ai/yinji-cli.mjs state --pdf ./test-fixture.pdf [--seal demo] [--add-seal] [--add-signature-demo] [--batch-pdf ./test-fixture.pdf] [--batch-pdf ./test-multi.pdf] [--process-batch] [--tab edit|export] [--collapse-sidebar]
node tools/ai/yinji-cli.mjs capture --output ./shot.png [--pdf ./test-fixture.pdf] [--seal demo] [--add-seal] [--add-signature-demo] [--batch-pdf ./test-fixture.pdf] [--batch-pdf ./test-multi.pdf] [--process-batch] [--tab edit|export] [--collapse-sidebar] [--modal settings|help]
可选参数:
--app-url 页面地址,默认 http://127.0.0.1:8123/index.html
--cdp-url Chrome DevTools 地址,默认 http://127.0.0.1:9222
--width 视口宽度,默认 1440
--height 视口高度,默认 960
--pdf 文件路径,支持 PDF、PNG、JPG
--seal 印章文件路径,或 demo
--add-seal 自动点击“添加普通章”
--add-signature-demo 自动写入一笔演示签名并确认
--batch-pdf 加入一个批量 PDF可重复传多次
--batch-pdfs 加入多个批量 PDF使用逗号分隔
--process-batch 自动开始批量处理,并等待界面流程结束
--tab 切到 edit 或 export
--collapse-sidebar 折叠左侧页面预览
--modal 打开 settings 或 help
--output 截图输出路径
`);
}
function parseArgs(argv) {
const [, , command, ...rest] = argv;
const options = {};
for (let i = 0; i < rest.length; i += 1) {
const arg = rest[i];
if (!arg.startsWith('--')) {
continue;
}
const key = arg.slice(2);
if (
[
'add-seal',
'add-signature-demo',
'process-batch',
'collapse-sidebar',
].includes(key)
) {
options[key] = true;
continue;
}
if (key === 'batch-pdf') {
if (!options[key]) {
options[key] = [];
}
options[key].push(rest[i + 1]);
i += 1;
continue;
}
options[key] = rest[i + 1];
i += 1;
}
return { command, options };
}
function resolveOptionPath(filePath) {
if (!filePath || filePath === 'demo') {
return filePath;
}
return path.resolve(process.cwd(), filePath);
}
function resolveOptionPathList(values = []) {
return values.map((value) => path.resolve(process.cwd(), value));
}
function resolveCommaSeparatedPathList(value) {
if (!value) {
return [];
}
return value
.split(',')
.map((item) => item.trim())
.filter(Boolean)
.map((item) => path.resolve(process.cwd(), item));
}
async function main() {
const { command, options } = parseArgs(process.argv);
if (!command || command === '--help' || command === 'help') {
printHelp();
return;
}
const driver = new YinjiDriver({
appUrl: options['app-url'],
cdpBaseUrl: options['cdp-url'],
width: options.width ? Number(options.width) : undefined,
height: options.height ? Number(options.height) : undefined,
});
try {
await driver.open();
const pdf = resolveOptionPath(options.pdf);
const seal = resolveOptionPath(options.seal);
const batchPdfs = [
...resolveOptionPathList(options['batch-pdf'] || []),
...resolveCommaSeparatedPathList(options['batch-pdfs']),
];
if (command === 'smoke') {
if (!pdf) {
throw new Error('smoke 需要 --pdf');
}
const result = await driver.runSmoke({
pdf,
seal: seal || 'demo',
});
console.log(JSON.stringify(result, null, 2));
return;
}
if (command === 'state' || command === 'capture') {
await driver.prepare({
pdf,
seal,
addSeal: Boolean(options['add-seal']),
addSignatureDemo: Boolean(options['add-signature-demo']),
batchPdfs,
processBatch: Boolean(options['process-batch']),
tab: options.tab || '',
collapseSidebar: Boolean(options['collapse-sidebar']),
modal: options.modal || '',
});
const snapshot = await driver.snapshotState();
if (command === 'capture') {
const output = options.output
? path.resolve(process.cwd(), options.output)
: path.resolve(process.cwd(), 'yinji-capture.png');
await driver.captureScreenshot(output);
console.log(
JSON.stringify(
{
output,
snapshot,
},
null,
2
)
);
return;
}
console.log(JSON.stringify(snapshot, null, 2));
return;
}
throw new Error(`未知命令: ${command}`);
} finally {
await driver.close();
}
}
main().catch((error) => {
console.error(error.message || error);
process.exitCode = 1;
});