#!/usr/bin/env node /** * patch-p2-npmrc-release-age.js * * P2.8: 在 ~/.npmrc 追加 minimumReleaseAge=2880 * * 镜像 OpenClaw.ai pnpm-workspace.yaml 同名字段。 * npm >= 9.4.0 / pnpm >= 8 支持。 * * 协议: .bak + sentinel + 原子写 * * 注: hooks/edit-precheck-dispatcher 拦截 .npmrc 直接写, * 此补丁脚本通过 patches/ 目录获得豁免(与 P0/P1 同模式) */ 'use strict'; const fs = require('fs'); const path = require('path'); const os = require('os'); const TARGET = path.join(os.homedir(), '.npmrc'); const SENTINEL = 'BOOKWORM-NPM-RELEASE-AGE-2880'; const APPEND_BLOCK = '\n' + '# bookworm v6.6 (2026-04-25): ' + SENTINEL + ' — 反 npm 投毒\n' + '# 拒绝 < 48h 新发布的包(OpenClaw.ai 同等实践)\n' + '# npm >= 9.4.0 / pnpm >= 8 支持\n' + 'minimumReleaseAge=2880\n'; function main() { let cur = ''; if (fs.existsSync(TARGET)) { cur = fs.readFileSync(TARGET, 'utf8'); if (cur.includes(SENTINEL)) { process.stdout.write('[SKIP] already configured (sentinel found)\n'); process.exit(0); } const ts = new Date().toISOString().replace(/[:.]/g, '-'); fs.copyFileSync(TARGET, TARGET + '.bak.' + ts); process.stdout.write('[BACKUP] ' + TARGET + '.bak.' + ts + '\n'); } const newContent = cur + APPEND_BLOCK; // 原子写 const tmpPath = TARGET + '.tmp.' + process.pid; fs.writeFileSync(tmpPath, newContent); fs.renameSync(tmpPath, TARGET); process.stdout.write('[OK] minimumReleaseAge=2880 appended to ' + TARGET + '\n'); process.stdout.write(' verify: npm config get minimumReleaseAge\n'); } if (require.main === module) main();