UNPKG

auto-request

Version:

通过Yapi JSON Schema生成接口Axios或Taro接口

87 lines (68 loc) 2.34 kB
/** * 更新快照基准文件 * 当代码有预期的改进时使用此脚本更新快照 */ const fs = require('fs'); const path = require('path'); // 自动扫描 example 目录,生成快照配置 const exampleDir = path.join(__dirname, '..', 'example'); const snapshots = []; if (fs.existsSync(exampleDir)) { const dirs = fs.readdirSync(exampleDir); dirs.forEach((dir) => { const dirPath = path.join(exampleDir, dir); if (!fs.statSync(dirPath).isDirectory()) return; const apiDir = path.join(dirPath, 'api'); if (!fs.existsSync(apiDir)) return; // 检查是否有 TypeScript 文件 const apiTs = path.join(apiDir, `${dir}.ts`); const defineTs = path.join(apiDir, `${dir}.define.ts`); const indexJs = path.join(apiDir, 'index.js'); if (fs.existsSync(apiTs)) { snapshots.push({ source: `example/${dir}/api/${dir}.ts`, target: `tests/snapshots/before/${dir}.snapshot.ts`, name: dir, }); } if (fs.existsSync(defineTs)) { snapshots.push({ source: `example/${dir}/api/${dir}.define.ts`, target: `tests/snapshots/before/${dir}.define.snapshot.ts`, name: `${dir}.define`, }); } if (fs.existsSync(indexJs)) { snapshots.push({ source: `example/${dir}/api/index.js`, target: `tests/snapshots/before/${dir}.snapshot.js`, name: dir, }); } }); } console.log('\n📸 更新快照基准...\n'); let updateCount = 0; let skipCount = 0; snapshots.forEach((snap) => { const sourcePath = path.join(__dirname, '..', snap.source); const targetPath = path.join(__dirname, '..', snap.target); if (!fs.existsSync(sourcePath)) { console.log(`⚠️ ${snap.name}: 源文件不存在,跳过`); skipCount++; return; } // 确保目标目录存在 const targetDir = path.dirname(targetPath); if (!fs.existsSync(targetDir)) { fs.mkdirSync(targetDir, { recursive: true }); } // 复制文件 fs.copyFileSync(sourcePath, targetPath); console.log(`✅ ${snap.name}: 已更新快照`); updateCount++; }); console.log(`\n${'='.repeat(50)}`); console.log(`📊 更新结果: ${updateCount} 个快照已更新, ${skipCount} 个跳过`); console.log(`${'='.repeat(50)}\n`); console.log('✅ 快照基准已更新!\n');