37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
import { execFileSync } from 'node:child_process';
|
|
import { readFileSync } from 'node:fs';
|
|
import { join, resolve } from 'node:path';
|
|
import { pathToFileURL } from 'node:url';
|
|
|
|
const forbidden = [
|
|
/\b(?:prisma|tx)\s*\.\s*user\s*\.\s*delete(?:Many)?\s*\(/g,
|
|
/\bDELETE\s+FROM\s+"users"/gi,
|
|
];
|
|
|
|
export function userHardDeletes(root, files = execFileSync('git', [
|
|
'ls-files', '-z', '--cached', '--others', '--exclude-standard',
|
|
], { cwd: root, encoding: 'utf8' }).split('\0')) {
|
|
const violations = [];
|
|
for (const file of new Set(files)) {
|
|
if (file.startsWith('scripts/archive/') ||
|
|
file.startsWith('资料/') || file.startsWith('.private/') ||
|
|
/(?:^|\/)__tests__\/|\.test\.[^.]+$/.test(file) ||
|
|
!/\.(?:[cm]?[jt]sx?|sql|ps1|sh|py)$/i.test(file)) continue;
|
|
const source = readFileSync(join(root, file), 'utf8');
|
|
for (const pattern of forbidden) {
|
|
for (const { index } of source.matchAll(pattern)) {
|
|
violations.push(file + ':' + source.slice(0, index).split('\n').length);
|
|
}
|
|
}
|
|
}
|
|
return violations;
|
|
}
|
|
|
|
if (process.argv[1] && pathToFileURL(resolve(process.argv[1])).href === import.meta.url) {
|
|
const violations = userHardDeletes(process.cwd());
|
|
if (violations.length) {
|
|
console.error('禁止物理删除用户:\n' + violations.join('\n'));
|
|
process.exitCode = 1;
|
|
}
|
|
}
|