UNPKG

@n1k1t/unit-generator

Version:

Coverage based unit tests AI generator

154 lines 6.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Rg = void 0; const path_1 = __importDefault(require("path")); const promises_1 = __importDefault(require("fs/promises")); const v3_1 = __importDefault(require("zod/v3")); const bash_1 = require("./bash"); const platforms = { 'arm64-darwin': { platform: 'aarch64-apple-darwin', extension: 'tar.gz' }, 'arm64-linux': { platform: 'aarch64-unknown-linux-gnu', extension: 'tar.gz' }, 'arm64-win32': { platform: 'aarch64-pc-windows-msvc', extension: 'zip' }, 'x64-darwin': { platform: 'x86_64-apple-darwin', extension: 'tar.gz' }, 'x64-linux': { platform: 'x86_64-unknown-linux-musl', extension: 'tar.gz' }, 'x64-win32': { platform: 'x86_64-pc-windows-msvc', extension: 'zip' }, }; const schemas = (() => { const match = v3_1.default.object({ type: v3_1.default.literal('match'), data: v3_1.default.object({ line_number: v3_1.default.number(), absolute_offset: v3_1.default.number(), path: v3_1.default.object({ text: v3_1.default.string(), }), lines: v3_1.default.object({ text: v3_1.default.string(), }), submatches: v3_1.default.array(v3_1.default.object({ match: v3_1.default.object({ text: v3_1.default.string(), }), start: v3_1.default.number(), end: v3_1.default.number(), })), }), }); return { match, result: v3_1.default.union([ match, v3_1.default.object({ type: v3_1.default.literal('begin') }), v3_1.default.object({ type: v3_1.default.literal('end') }), v3_1.default.object({ type: v3_1.default.literal('summary') }), ]), }; })(); class Rg { constructor(provided) { this.provided = provided; this.argv0 = null; } async exec(pattern, options) { const argv0 = await this.provide(); const bash = bash_1.Bash.build({ argv0, cwd: this.provided?.cwd }); const args = ['--json', '--hidden']; if (options?.include) { for (const pattern of options.include) { args.push(`--glob='${pattern}'`); } } if (options?.exclude) { for (const pattern of options.exclude.concat(['.git/**'])) { args.push(`--glob=!'${pattern}'`); } } if (options?.limit) { args.push(`--max-count=${options.limit}`); } const result = await bash.exec(args.concat([`'${pattern}'`], options?.path ? [options.path] : []).join(' ')); if (result.status === 'ERROR') { return []; } return result.stdout .trim() .split(/\r?\n/) .filter(Boolean) .map((line) => JSON.parse(line)) .map((parsed) => schemas.result.parse(parsed)) .filter((record) => record.type === 'match') .slice(0, options?.limit) .map((record) => record.data); } /** Installs and returns argv0 for `rg` */ async provide() { if (this.argv0) { return this.argv0; } const existent = await this.which('rg'); if (existent) { this.argv0 = existent; return existent; } const dir = path_1.default.join(__dirname, '../../', '.bin'); const bin = path_1.default.join(dir, `rg${process.platform === 'win32' ? '.exe' : ''}`); if ((await promises_1.default.stat(bin).catch(() => null))) { this.argv0 = bin; return bin; } await promises_1.default.mkdir(dir, { recursive: true }); const platform = `${process.arch}-${process.platform}`; const config = platforms[platform]; if (!config) { throw new Error(`Unsupported platform: ${platform}`); } const version = '14.1.1'; const filename = `ripgrep-${version}-${config.platform}.${config.extension}`; const url = `https://github.com/BurntSushi/ripgrep/releases/download/${version}/${filename}`; const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to download ripgrep: ${response.statusText}`); } const buffer = await response.arrayBuffer(); const archive = path_1.default.join(dir, filename); await promises_1.default.writeFile(archive, Buffer.from(buffer)); if (config.extension === 'tar.gz') { const bash = new bash_1.Bash({ cwd: dir }); const tarArgs = ['-xzf', filename, '--strip-components=1']; if (process.platform === 'darwin') { tarArgs.push('--include=*/rg'); } else { tarArgs.push('--wildcards', '*/rg'); } await bash.exec(`tar ${tarArgs.join(' ')}`); } else { // Basic zip extraction for windows if needed, but here we assume tar is available or use a simple approach // For simplicity in this environment, we'll try to use powershell or similar if on windows, // but the example used a zip library which is not in dependencies. // Given the environment, we'll stick to tar which is usually available on modern windows too. const bash = new bash_1.Bash({ cwd: dir }); await bash.exec(`tar -xf ${filename} --strip-components=1`); } await promises_1.default.unlink(archive); if (process.platform !== 'win32') { await promises_1.default.chmod(bin, 0o755); } this.argv0 = bin; return bin; } async which(cmd) { const bash = new bash_1.Bash({ argv0: 'which' }); const result = await bash.exec(cmd); return result.status === 'OK' ? result.stdout.trim() : null; } static build(provided) { return new Rg(provided); } } exports.Rg = Rg; //# sourceMappingURL=rg.js.map