auto-request
Version:
通过Yapi JSON Schema生成接口Axios或Taro接口
76 lines (64 loc) • 2.15 kB
text/typescript
/**
* 运行所有测试用例,生成快照并对比
*/
import * as fs from 'fs';
import * as path from 'path';
import { execSync } from 'child_process';
const examples = [
{
name: 'all-api',
script: 'yarn all-api',
outputFile: './example/all-api/api/all-api.ts',
defineFile: './example/all-api/api/all-api.define.ts',
},
{
name: 'swagger-doc',
script: 'yarn swagger-doc',
outputFile: './example/swagger-doc/api/all-api.ts',
defineFile: './example/swagger-doc/api/all-api.define.ts',
},
{
name: 'open',
script: 'yarn open',
outputFile: './example/open/api/index.js',
defineFile: null, // JS 模式没有定义文件
},
{
name: 'usercenter',
script: 'yarn usercenter',
outputFile: './example/usercenter/api/index.js',
defineFile: null,
},
];
const snapshotDir = path.join(__dirname, 'snapshots');
// 确保快照目录存在
if (!fs.existsSync(snapshotDir)) {
fs.mkdirSync(snapshotDir, { recursive: true });
}
console.log('开始生成测试快照...\n');
examples.forEach((example) => {
console.log(`处理 ${example.name}...`);
try {
// 运行生成脚本(自动选择 'y' 继续)
execSync(`echo "y" | ${example.script}`, {
stdio: 'inherit',
cwd: path.join(__dirname, '..'),
});
// 复制生成的文件到快照目录
const outputSnapshot = path.join(snapshotDir, `${example.name}.snapshot.ts`);
if (fs.existsSync(example.outputFile)) {
fs.copyFileSync(example.outputFile, outputSnapshot);
console.log(` ✓ 已保存快照: ${outputSnapshot}`);
}
// 如果有定义文件,也复制
if (example.defineFile && fs.existsSync(example.defineFile)) {
const defineSnapshot = path.join(snapshotDir, `${example.name}.define.snapshot.ts`);
fs.copyFileSync(example.defineFile, defineSnapshot);
console.log(` ✓ 已保存定义文件快照: ${defineSnapshot}`);
}
console.log(` ${example.name} 完成\n`);
} catch (error) {
console.error(` ✗ ${example.name} 失败:`, error.message);
}
});
console.log('所有快照已生成完成!');