41 lines
1.8 KiB
JavaScript
41 lines
1.8 KiB
JavaScript
import assert from 'node:assert/strict';
|
|||
|
|
import { execFileSync } from 'node:child_process';
|
||
|
|
import { createHash } from 'node:crypto';
|
||
|
|
import fs from 'node:fs';
|
||
|
|
import path from 'node:path';
|
||
|
|
import { fileURLToPath } from 'node:url';
|
||
|
|
|
||
|
|
export const app = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
||
|
|
const repo = path.dirname(app);
|
||
|
|
export const git = (...args) =>
|
||
|
|
execFileSync('git', ['-C', repo, ...args], { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 }).trimEnd();
|
||
|
|
export const reference = git('rev-parse', 'f1a4e69^{commit}');
|
||
|
|
|
||
|
|
export function fixedReference(temp, supplied) {
|
||
|
|
if (supplied) {
|
||
|
|
const root = path.resolve(supplied);
|
||
|
|
const tree = git('ls-tree', '-rz', reference, '--', path.basename(app));
|
||
|
|
for (const entry of tree.split('\0').filter(Boolean)) {
|
||
|
|
const [meta, name] = entry.split('\t');
|
||
|
|
const content = fs.readFileSync(path.join(root, path.relative(path.basename(app), name)));
|
||
|
|
const actual = createHash('sha1')
|
||
|
|
.update('blob ' + content.length + '\0')
|
||
|
|
.update(content)
|
||
|
|
.digest('hex');
|
||
|
|
assert.equal(actual, meta.split(' ')[2], '旧版检出不符合 ' + reference + ': ' + name);
|
||
|
|
}
|
||
|
|
return root;
|
||
|
|
}
|
||
|
|
const archive = path.join(temp, 'reference.tar');
|
||
|
|
git('archive', '--output=' + archive, reference, '--', path.basename(app));
|
||
|
|
execFileSync('tar', ['-xf', archive, '-C', temp]);
|
||
|
|
const root = path.join(temp, path.basename(app));
|
||
|
|
for (const folder of ['', 'e2e', 'web']) {
|
||
|
|
const modules = path.join(app, folder, 'node_modules');
|
||
|
|
if (fs.existsSync(modules)) fs.symlinkSync(modules, path.join(root, folder, 'node_modules'), 'dir');
|
||
|
|
}
|
||
|
|
return root;
|
||
|
|
}
|
||
|
|
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
||
|
|
console.log(fixedReference(process.argv[2], process.env.LEDGER_REFERENCE_APP));
|
||
|
|
}
|