posimai-root/scripts/run-pc-audit.js

47 lines
1.4 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
import { readFileSync, writeFileSync } from 'fs';
import { spawnSync } from 'child_process';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const rootDir = join(__dirname, '..');
const psScript = join(rootDir, 'tools', 'pc-audit', 'Invoke-PcAudit.ps1');
function ensureUtf8Bom(filePath) {
let data;
try {
data = readFileSync(filePath);
} catch {
console.error('[pc-audit] Invoke-PcAudit.ps1 が見つかりません:', filePath);
process.exit(1);
}
const hasBom = data.length >= 3 && data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf;
if (hasBom) return false;
writeFileSync(filePath, Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), data]));
return true;
}
function run() {
const shareSafe = process.argv.includes('--share-safe');
const bomAdded = ensureUtf8Bom(psScript);
if (bomAdded) {
console.log('[pc-audit] Invoke-PcAudit.ps1 に UTF-8 BOM を自動付与しました。');
}
const args = ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', psScript];
if (shareSafe) args.push('-ShareSafe');
const result = spawnSync('powershell', args, {
cwd: rootDir,
stdio: 'inherit',
});
if (result.error) {
console.error('[pc-audit] PowerShell 起動失敗:', result.error.message);
}
process.exit(result.status ?? 1);
}
run();