mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-11-04 05:13:56 +08:00
🤞 Sync 2026-09-06 22:50:31
This commit is contained in:
1 parent
bb7d626e94
commit
64c17e592d
115 files changed
+8599
-1391
No files matched your search
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { createRequire } from 'module';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
// 1. 验证受控环境 (npm_config_ignore_scripts=true)
|
||||
if (process.env.npm_config_ignore_scripts !== 'true') {
|
||||
console.error('[postinstall] 错误: npm_config_ignore_scripts 必须为 "true", 实际为: ' + process.env.npm_config_ignore_scripts);
|
||||
process.exit(98);
|
||||
}
|
||||
|
||||
// 2. 验证 cwd 为安装包根目录 (必须包含 package.json)
|
||||
const rawCwd = process.cwd();
|
||||
const cwd = rawCwd.replace(/\\/g, '/');
|
||||
const localPkgPath = path.resolve(rawCwd, 'package.json');
|
||||
if (!fs.existsSync(localPkgPath)) {
|
||||
console.error('[postinstall] 错误: cwd 必须为安装包根目录,当前未找到 package.json: ' + cwd);
|
||||
process.exit(97);
|
||||
}
|
||||
|
||||
// 3. 验证依赖可解析 (通过 Node require.resolve 机制确认当前上下文或依赖树有效)
|
||||
// 可通过环境变量 OC_TEST_DEP 指定必须可解析的依赖;未指定时解析 package.json 自身以验证解析上下文就绪
|
||||
try {
|
||||
const depToResolve = process.env.OC_TEST_DEP || './package.json';
|
||||
require.resolve(depToResolve, { paths: [cwd] });
|
||||
} catch (err) {
|
||||
console.error('[postinstall] 依赖解析失败: ' + err.message);
|
||||
process.exit(96);
|
||||
}
|
||||
|
||||
// 4. 写入可计数副作用 (文件追加写入)
|
||||
const sideEffectFile = process.env.OC_SIDE_EFFECT_FILE;
|
||||
if (sideEffectFile) {
|
||||
const record = JSON.stringify({
|
||||
script: 'postinstall',
|
||||
cwd: cwd,
|
||||
npm_config_ignore_scripts: process.env.npm_config_ignore_scripts,
|
||||
timestamp: Date.now()
|
||||
}) + '\n';
|
||||
fs.appendFileSync(sideEffectFile, record, 'utf8');
|
||||
}
|
||||
|
||||
// 5. 失败注入支持 (用于验证失败时回滚并保持现有安装不变)
|
||||
if (process.env.OC_FAIL_POSTINSTALL === '1') {
|
||||
console.error('[postinstall] 模拟 postinstall 失败 (OC_FAIL_POSTINSTALL=1)');
|
||||
process.exit(43);
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { createRequire } from 'module';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
// 1. 验证受控环境 (npm_config_ignore_scripts=true)
|
||||
if (process.env.npm_config_ignore_scripts !== 'true') {
|
||||
console.error('[preinstall] 错误: npm_config_ignore_scripts 必须为 "true", 实际为: ' + process.env.npm_config_ignore_scripts);
|
||||
process.exit(98);
|
||||
}
|
||||
|
||||
// 2. 验证 cwd 为安装包根目录 (必须包含 package.json)
|
||||
const rawCwd = process.cwd();
|
||||
const cwd = rawCwd.replace(/\\/g, '/');
|
||||
const localPkgPath = path.resolve(rawCwd, 'package.json');
|
||||
if (!fs.existsSync(localPkgPath)) {
|
||||
console.error('[preinstall] 错误: cwd 必须为安装包根目录,当前未找到 package.json: ' + cwd);
|
||||
process.exit(97);
|
||||
}
|
||||
|
||||
// 3. 验证依赖可解析 (通过 Node require.resolve 机制确认当前上下文或依赖树有效)
|
||||
// 可通过环境变量 OC_TEST_DEP 指定必须可解析的依赖;未指定时解析 package.json 自身以验证解析上下文就绪
|
||||
try {
|
||||
const depToResolve = process.env.OC_TEST_DEP || './package.json';
|
||||
require.resolve(depToResolve, { paths: [cwd] });
|
||||
} catch (err) {
|
||||
console.error('[preinstall] 依赖解析失败: ' + err.message);
|
||||
process.exit(96);
|
||||
}
|
||||
|
||||
// 4. 写入可计数副作用 (文件追加写入)
|
||||
const sideEffectFile = process.env.OC_SIDE_EFFECT_FILE;
|
||||
if (sideEffectFile) {
|
||||
const record = JSON.stringify({
|
||||
script: 'preinstall',
|
||||
cwd: cwd,
|
||||
npm_config_ignore_scripts: process.env.npm_config_ignore_scripts,
|
||||
timestamp: Date.now()
|
||||
}) + '\n';
|
||||
fs.appendFileSync(sideEffectFile, record, 'utf8');
|
||||
}
|
||||
|
||||
// 5. 失败注入支持 (用于验证失败时回滚并保持现有安装不变)
|
||||
if (process.env.OC_FAIL_PREINSTALL === '1') {
|
||||
console.error('[preinstall] 模拟 preinstall 失败 (OC_FAIL_PREINSTALL=1)');
|
||||
process.exit(42);
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
Reference in new issue
Block a user