UNPKG

auto-request

Version:

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

53 lines (41 loc) 1.75 kB
/** * 对比快照文件,确保重构后生成的代码一致 */ import * as fs from 'fs'; import * as path from 'path'; const comparisons = [ { before: 'all-api.snapshot.ts', after: '../example/all-api/api/all-api.ts', name: 'all-api' }, { before: 'all-api.define.snapshot.ts', after: '../example/all-api/api/all-api.define.ts', name: 'all-api.define' }, { before: 'open.snapshot.js', after: '../example/open/api/index.js', name: 'open' }, { before: 'usercenter.snapshot.js', after: '../example/usercenter/api/index.js', name: 'usercenter' }, ]; const snapshotDir = path.join(__dirname, 'snapshots/before'); let allPassed = true; console.log('开始对比快照...\n'); comparisons.forEach((comp) => { const beforePath = path.join(snapshotDir, comp.before); const afterPath = path.join(__dirname, '..', comp.after); if (!fs.existsSync(beforePath)) { console.log(`⚠ ${comp.name}: 快照文件不存在,跳过`); return; } if (!fs.existsSync(afterPath)) { console.log(`✗ ${comp.name}: 生成文件不存在`); allPassed = false; return; } const beforeContent = fs.readFileSync(beforePath, 'utf-8'); const afterContent = fs.readFileSync(afterPath, 'utf-8'); if (beforeContent === afterContent) { console.log(`✓ ${comp.name}: 一致`); } else { console.log(`✗ ${comp.name}: 不一致`); allPassed = false; // 显示差异的行数 const beforeLines = beforeContent.split('\n').length; const afterLines = afterContent.split('\n').length; console.log(` 重构前: ${beforeLines} 行,重构后: ${afterLines} 行`); } }); console.log(`\n${allPassed ? '✓ 所有测试通过!' : '✗ 有测试失败'}`); process.exit(allPassed ? 0 : 1);