55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
|
|
#!/usr/bin/env node
|
|||
|
|
/**
|
|||
|
|
* patch-p2-npmrc-fix-kebab.js
|
|||
|
|
*
|
|||
|
|
* 修正 .npmrc 中 minimumReleaseAge → minimum-release-age (pnpm 标准 kebab-case)
|
|||
|
|
*
|
|||
|
|
* npm CLI 不识别此字段(会报 Unknown user config 警告),
|
|||
|
|
* pnpm CLI 用 kebab-case 才能正确读取。
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
'use strict';
|
|||
|
|
const fs = require('fs');
|
|||
|
|
const path = require('path');
|
|||
|
|
const os = require('os');
|
|||
|
|
|
|||
|
|
const TARGET = path.join(os.homedir(), '.npmrc');
|
|||
|
|
|
|||
|
|
function main() {
|
|||
|
|
if (!fs.existsSync(TARGET)) {
|
|||
|
|
process.stderr.write('[ERROR] .npmrc not found\n');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
let cur = fs.readFileSync(TARGET, 'utf8');
|
|||
|
|
|
|||
|
|
if (cur.includes('minimum-release-age=')) {
|
|||
|
|
process.stdout.write('[SKIP] already in kebab-case\n');
|
|||
|
|
process.exit(0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!cur.includes('minimumReleaseAge=')) {
|
|||
|
|
process.stderr.write('[ERROR] base patch not applied (no minimumReleaseAge found)\n');
|
|||
|
|
process.exit(1);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const ts = new Date().toISOString().replace(/[:.]/g, '-');
|
|||
|
|
fs.copyFileSync(TARGET, TARGET + '.bak.kebab.' + ts);
|
|||
|
|
process.stdout.write('[BACKUP] ' + TARGET + '.bak.kebab.' + ts + '\n');
|
|||
|
|
|
|||
|
|
// 替换 + 同时保留 camelCase 一份给文档参考
|
|||
|
|
const updated = cur.replace(
|
|||
|
|
/minimumReleaseAge=(\d+)/,
|
|||
|
|
'minimum-release-age=$1 # pnpm canonical field (kebab-case)\n# minimumReleaseAge=$1 # camelCase alias (npm reports Unknown — informational only)'
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
const tmpPath = TARGET + '.tmp.' + process.pid;
|
|||
|
|
fs.writeFileSync(tmpPath, updated);
|
|||
|
|
fs.renameSync(tmpPath, TARGET);
|
|||
|
|
|
|||
|
|
process.stdout.write('[OK] field renamed to kebab-case\n');
|
|||
|
|
process.stdout.write(' verify (pnpm): pnpm config get minimum-release-age\n');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (require.main === module) main();
|