30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
// 每次运行把样例仓库复制一份,初始化成 git 仓库,方便看 diff。
|
|
import { cpSync, mkdirSync, rmSync } from 'node:fs';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { join } from 'node:path';
|
|
import { PROJECT_ROOT } from './config.ts';
|
|
|
|
export const FIXTURE = join(PROJECT_ROOT, 'fixture', 'order-service');
|
|
export const RUNS_DIR = join(PROJECT_ROOT, '.runs');
|
|
|
|
export function prepareWorkspace(runId: string): string {
|
|
const dir = join(RUNS_DIR, runId, 'order-service');
|
|
rmSync(dir, { recursive: true, force: true });
|
|
mkdirSync(dir, { recursive: true });
|
|
cpSync(FIXTURE, dir, { recursive: true });
|
|
const git = (...args: string[]) => execFileSync('git', args, { cwd: dir, stdio: 'ignore' });
|
|
git('init', '-q', '-b', 'main');
|
|
git('add', '-A');
|
|
git('-c', 'user.name=demo', '-c', 'user.email=demo@example.com', 'commit', '-qm', 'baseline');
|
|
return dir;
|
|
}
|
|
|
|
export function workspaceDiff(dir: string): string {
|
|
try {
|
|
execFileSync('git', ['add', '-A', '-N'], { cwd: dir, stdio: 'ignore' });
|
|
return execFileSync('git', ['diff', '--no-color'], { cwd: dir, encoding: 'utf8' });
|
|
} catch {
|
|
return '';
|
|
}
|
|
}
|