固定构建工具链并禁止依赖安装脚本

This commit is contained in:
xuans committed 2026-09-27 18:24:53 +08:00
1 parent 5fdb5e4fa1
commit 410ce751ba
12 files changed
+445 -22

No files matched your search

+16 -1
View File
@@ -56,7 +56,22 @@ powershell -ExecutionPolicy Bypass -File .\build\build-release.ps1 -OutputDirect
脚本仅创建新的发布目录。发布目录或对应归档已存在时,在构建前拒绝执行,不覆盖旧包。前端直接构建到本次发布目录中的 `web-build/`,不清空或覆盖已有 `frontend/dist/`。失败产生的目录保留供排查,再次构建请使用新的输出目录。
`manifest.json` 记录源码提交、已跟踪文件是否未提交、实际 Go 工具链、目标平台和文件及归档的 SHA256。`delivery_scope` 固定为 `web`、`server`,兼容字段 `desktop_requested` 固定为 `false`。`source_dirty=true` 的包仅用于开发,正式发布必须从已复审提交构建。未跟踪本地产物不计入源码脏状态,也不会自动打包。
`manifest.json` 记录源码提交、源码是否未提交、实际 Go 工具链、目标平台和文件及归档的 SHA256。`delivery_scope` 固定为 `web`、`server`,兼容字段 `desktop_requested` 固定为 `false`。`source_dirty=true` 的包仅用于开发,正式发布必须从已复审提交构建。未忽略的未跟踪文件也计入源码脏状态;已忽略的本地产物不计入,也不会自动打包。
## 固定工具链与安装策略
`toolchain.mjs` 是正式发布入口调用的构建检查器,`toolchain.test.mjs` 是纳入前端测试命令的正式回归,不是临时验收脚本。
1. Go 精确版本以 `backend/go.mod` 为唯一来源;实际工具链不符则失败。`go.sum`、`go.mod` 固定 Go 依赖和数据库驱动,构建及归档使用 `-mod=readonly`;禁用外部 Go 工作区及 `GOFLAGS` 覆盖。
2. Node/npm 精确版本由 `frontend/package.json` 的 `engines` 声明,`devEngines`、`packageManager` 和锁文件必须一致。直接依赖禁止范围版本,锁文件负责传递依赖。
3. `frontend/.npmrc` 明确配置 `ignore-scripts=true`、`engine-strict=true`、`save-exact=true`。不使用脚本白名单或全量放行开关;当前 npm 的未审查警告不能证明脚本被禁止。
4. 发布脚本在创建输出目录前校验版本、执行 `go mod verify`,并自动执行 `npm ci --ignore-scripts --engine-strict --include=dev --include=optional --no-audit --no-fund`。这会按锁文件重建前端依赖,不复用未知的旧安装,也不删除数据库、证据或既有发布包。
5. esbuild 使用锁文件中对应构建机的平台可选包,不运行其安装脚本,不允许 `ESBUILD_BINARY_PATH` 外部覆盖。缺少平台包或二进制不可用必须失败,不允许另行下载绕过锁文件。
6. Vite 构建仍显式执行,但禁用隐式前后置脚本。最终再校验版本和输入 SHA256;构建中改变版本文件不生成发布清单。
`build_environment` 记录 Node/npm/Go/PowerShell、直接前端依赖、安装策略、构建机系统名称/版本/架构,以及五份依赖和策略文件的 SHA256;不记录用户名、主机名或本地绝对路径。构建机目前允许 Windows/Linux amd64,实际已验证系统和未验证边界见版本矩阵;Windows 交叉编译 Linux 包不等于 Linux 原生构建或运行验收。
从 `npm test` 等父进程启动检查器时,npm 可能将用户级 `allow-scripts` 导出到环境变量,而项目级安装拒绝这种传递。本入口仅从子进程环境移除该白名单变量,不改用户配置;`--ignore-scripts` 仍强制生效。正式测试覆盖嵌套调用和主动尝试放行脚本的环境设置,不能通过开启安装脚本解决此兼容问题。
## 本机路径保护
+30 -5
View File
@@ -62,7 +62,7 @@ function Invoke-GoBuild {
$env:GOOS = $OperatingSystem
$env:GOARCH = $Architecture
$env:CGO_ENABLED = "0"
& go build -trimpath -o $Destination $Package
& go build -trimpath -mod=readonly -o $Destination $Package
if ($LASTEXITCODE -ne 0) {
throw "构建 $OperatingSystem/$Architecture 的 $Package 失败。"
}
@@ -136,6 +136,7 @@ function Invoke-ReleaseArchive {
$arguments = @(
"run",
"-trimpath",
"-mod=readonly",
"./cmd/scert-release",
"--source", $Source,
"--output", $Destination,
@@ -174,7 +175,7 @@ function Test-ReleasePaths {
[string[]]$ForbiddenPaths
)
$arguments = @("run", "-trimpath", "./cmd/scert-release", "--verify-only", "--source", $Source)
$arguments = @("run", "-trimpath", "-mod=readonly", "./cmd/scert-release", "--verify-only", "--source", $Source)
foreach ($binary in $Binaries) {
$arguments += @("--binary", $binary)
}
@@ -215,6 +216,9 @@ $npmCommand = if ($env:OS -eq "Windows_NT") { "npm.cmd" } else { "npm" }
if (-not (Get-Command $npmCommand -ErrorAction SilentlyContinue)) {
throw "未找到 npm 工具链,请先安装 Node.js 并加入 PATH。"
}
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
throw "未找到 Node.js 工具链,请先安装 Node.js 并加入 PATH。"
}
if (-not (Test-Path -LiteralPath (Join-Path $frontend "package.json") -PathType Leaf)) {
throw "前端目录中没有找到 package.json:$frontend"
}
@@ -258,21 +262,29 @@ $sourceCommit = (& git -C $backend rev-parse HEAD)
if ($LASTEXITCODE -ne 0) {
throw "无法取得源码提交,不能生成可追溯发布包。"
}
$sourceChanges = @(& git -C $backend status --porcelain --untracked-files=no)
$sourceChanges = @(& git -C $backend status --porcelain --untracked-files=normal)
if ($LASTEXITCODE -ne 0) {
throw "无法检查源码工作区状态。"
}
$sourceDirty = $sourceChanges.Count -gt 0
if ($sourceDirty) {
Write-Warning "存在未提交的已跟踪文件改动,清单将标记 source_dirty=true,不能作为正式发布包。"
Write-Warning "存在未提交的源码改动,清单将标记 source_dirty=true,不能作为正式发布包。"
}
Write-Host "正在校验构建版本并按锁文件安装前端依赖……"
$toolchainJSON = & node (Join-Path $scriptDirectory "toolchain.mjs") --backend $backend --frontend $frontend --install
if ($LASTEXITCODE -ne 0) {
throw "构建版本或依赖安装检查失败,尚未创建发布目录。"
}
$toolchain = ($toolchainJSON -join "`n") | ConvertFrom-Json
$toolchain | Add-Member -NotePropertyName powershell -NotePropertyValue $PSVersionTable.PSVersion.ToString()
New-Item -ItemType Directory -Path $releaseDirectory | Out-Null
$frontendDistribution = Join-Path $releaseDirectory "web-build"
Write-Host "正在构建 Scert Web 前端……"
Push-Location $frontend
try {
& $npmCommand run build -- --outDir $frontendDistribution --emptyOutDir false
& $npmCommand --ignore-scripts run build -- --outDir $frontendDistribution --emptyOutDir false
if ($LASTEXITCODE -ne 0) {
throw "构建 Scert Web 前端失败。"
}
@@ -287,6 +299,8 @@ if (-not (Test-Path -LiteralPath (Join-Path $frontendDistribution "index.html")
$oldGoOS = $env:GOOS
$oldGoArch = $env:GOARCH
$oldCGOEnabled = $env:CGO_ENABLED
$oldGoWork = $env:GOWORK
$env:GOWORK = "off"
$manifestArtifacts = [System.Collections.Generic.List[object]]::new()
Push-Location $backend
@@ -385,6 +399,16 @@ finally {
$env:GOOS = $oldGoOS
$env:GOARCH = $oldGoArch
$env:CGO_ENABLED = $oldCGOEnabled
$env:GOWORK = $oldGoWork
}
$verifiedToolchainJSON = & node (Join-Path $scriptDirectory "toolchain.mjs") --backend $backend --frontend $frontend
if ($LASTEXITCODE -ne 0) {
throw "构建后的工具链复核失败,不生成发布清单。"
}
$verifiedToolchain = ($verifiedToolchainJSON -join "`n") | ConvertFrom-Json
if (($toolchain.input_sha256 | ConvertTo-Json -Compress) -ne ($verifiedToolchain.input_sha256 | ConvertTo-Json -Compress)) {
throw "构建期间版本文件发生变化,不生成发布清单。"
}
$manifest = [ordered]@{
@@ -394,6 +418,7 @@ $manifest = [ordered]@{
source_commit = $sourceCommit.Trim()
source_dirty = $sourceDirty
go_toolchain = $goVersion.Trim()
build_environment = $toolchain
trimpath = $true
local_path_check = "passed"
targets = @($Target)
+179
View File
@@ -0,0 +1,179 @@
import { createHash } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { arch, platform, release, version } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';
const exactVersion = /^\d+\.\d+\.\d+$/;
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
function requireCondition(condition, message) {
if (!condition) {
throw new Error(message);
}
}
export function validateFrontend(pkg, lock, npmrc, actual) {
for (const name of ['node', 'npm']) {
const expected = pkg.engines?.[name];
requireCondition(exactVersion.test(expected ?? ''), `${name} 必须固定精确版本。`);
requireCondition(actual[name] === expected, `${name} 版本不匹配,需要 ${expected}。`);
const engine = pkg.devEngines?.[name === 'node' ? 'runtime' : 'packageManager'];
requireCondition(
engine?.name === name && engine.version === expected && engine.onFail === 'error',
`${name} 的开发工具链拒绝策略不一致。`,
);
requireCondition(lock.packages?.['']?.engines?.[name] === expected, `${name} 锁文件版本声明不一致。`);
}
requireCondition(pkg.packageManager === `npm@${pkg.engines.npm}`, '包管理器声明不一致。');
requireCondition(lock.lockfileVersion === 3, '前端必须使用第三版锁文件。');
requireCondition(pkg.name === lock.name && pkg.version === lock.version, '前端锁文件项目标识不一致。');
for (const section of ['dependencies', 'devDependencies']) {
const declared = pkg[section] ?? {};
const locked = lock.packages[''][section] ?? {};
requireCondition(
JSON.stringify(Object.keys(declared).sort()) === JSON.stringify(Object.keys(locked).sort()),
`前端 ${section} 与锁文件不一致。`,
);
for (const [name, expected] of Object.entries(declared)) {
requireCondition(exactVersion.test(expected), `依赖 ${name} 必须固定精确版本。`);
requireCondition(
locked[name] === expected && lock.packages[`node_modules/${name}`]?.version === expected,
`依赖 ${name} 与锁文件不一致。`,
);
}
}
const settings = new Map();
for (const line of npmrc.split(/\r?\n/)) {
const value = line.trim();
if (!value || value.startsWith('#') || value.startsWith(';')) continue;
const match = /^([a-z-]+)=(.*)$/.exec(value);
requireCondition(match && !settings.has(match[1]), '前端 npm 配置包含无效项或重复项。');
settings.set(match[1], match[2]);
}
for (const name of ['engine-strict', 'ignore-scripts', 'save-exact']) {
requireCondition(settings.get(name) === 'true', `前端 npm 配置必须启用 ${name}。`);
}
requireCondition(settings.size === 3, '前端 npm 配置存在未审查的额外设置。');
requireCondition(!pkg.allowScripts && !pkg.dangerouslyAllowAllScripts, '禁止额外放行依赖安装脚本。');
}
export function validateGoVersion(goMod, actual) {
const expected = /^go (\d+\.\d+\.\d+)\s*$/m.exec(goMod)?.[1];
requireCondition(expected, '后端模块必须声明精确 Go 版本。');
requireCondition(actual === `go${expected}`, `实际 Go 工具链不匹配,需要 go${expected}。`);
return expected;
}
export function validateEnvironment(env, host) {
requireCondition(['win32', 'linux'].includes(host.platform) && host.arch === 'x64', '构建机仅支持 Windows/Linux amd64。');
requireCondition(!env.ESBUILD_BINARY_PATH, '禁止使用锁文件之外的 esbuild 可执行文件。');
requireCondition(!env.GOWORK || env.GOWORK === 'off', '正式构建禁止使用外部 Go 工作区。');
requireCondition(!env.GOFLAGS, '正式构建禁止通过 GOFLAGS 覆盖依赖或编译策略。');
}
export function npmEnvironment(env = process.env) {
// npm run 会把用户级白名单导出为环境变量;子项目安装却拒绝该变量。
// 仅移除这项不适用于项目安装的继承策略,安装命令仍强制禁止全部脚本。
return Object.fromEntries(Object.entries(env).filter(([name]) => name.toLowerCase() !== 'npm_config_allow_scripts'));
}
function execute(command, args, cwd, env = process.env) {
const useShell = process.platform === 'win32' && command === npmCommand;
if (useShell) {
requireCondition(args.every((arg) => /^[a-zA-Z0-9=_./-]+$/.test(arg)), 'npm 参数含有不允许的命令解释器字符。');
}
const options = {
cwd,
env: command === npmCommand ? npmEnvironment(env) : env,
encoding: 'utf8',
windowsHide: true,
// Windows 的 npm.cmd 需要命令解释器;参数仅来自本文件的固定值。
shell: useShell,
maxBuffer: 16 * 1024 * 1024,
};
const result = useShell
? spawnSync(`${command} ${args.join(' ')}`, options)
: spawnSync(command, args, options);
if (result.error || result.status !== 0) {
throw new Error(`${command} 执行失败。\n${result.stderr ?? ''}`);
}
return result;
}
export function inputHashes(backend, frontend) {
const files = {
'backend/go.mod': join(backend, 'go.mod'),
'backend/go.sum': join(backend, 'go.sum'),
'frontend/package.json': join(frontend, 'package.json'),
'frontend/package-lock.json': join(frontend, 'package-lock.json'),
'frontend/.npmrc': join(frontend, '.npmrc'),
};
return Object.fromEntries(Object.entries(files).map(([name, path]) => [
name,
createHash('sha256').update(readFileSync(path)).digest('hex'),
]));
}
export function installDependencies(frontend) {
// 不信任使用者的 npm 全局/用户设置,命令行参数显式覆盖;保留构建所需的可选平台包。
const result = execute(npmCommand, [
'ci', '--ignore-scripts', '--engine-strict', '--include=dev', '--include=optional',
'--no-audit', '--no-fund',
], frontend);
process.stderr.write(result.stdout);
process.stderr.write(result.stderr);
}
export function inspectToolchain(backend, frontend, install = false) {
const host = { platform: platform(), arch: arch(), release: release(), version: version() };
validateEnvironment(process.env, host);
const pkg = JSON.parse(readFileSync(join(frontend, 'package.json'), 'utf8'));
const lock = JSON.parse(readFileSync(join(frontend, 'package-lock.json'), 'utf8'));
const npmrc = readFileSync(join(frontend, '.npmrc'), 'utf8');
const actual = { node: process.versions.node, npm: execute(npmCommand, ['--version'], frontend).stdout.trim() };
validateFrontend(pkg, lock, npmrc, actual);
const goEnv = { ...process.env, GOWORK: 'off' };
const goVersion = execute('go', ['env', 'GOVERSION'], backend, goEnv).stdout.trim();
validateGoVersion(readFileSync(join(backend, 'go.mod'), 'utf8'), goVersion);
const hashes = inputHashes(backend, frontend);
execute('go', ['mod', 'verify'], backend, goEnv);
if (install) installDependencies(frontend);
requireCondition(JSON.stringify(hashes) === JSON.stringify(inputHashes(backend, frontend)), '依赖安装改变了锁文件或版本策略,拒绝构建。');
return {
node: actual.node,
npm: actual.npm,
go: goVersion,
host,
frontend_dependencies: { ...pkg.dependencies, ...pkg.devDependencies },
dependency_scripts: 'disabled',
dependency_install: install ? 'npm-ci' : 'not-run',
input_sha256: hashes,
};
}
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
try {
const { values } = parseArgs({
options: {
backend: { type: 'string' },
frontend: { type: 'string' },
install: { type: 'boolean', default: false },
},
});
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const report = inspectToolchain(
resolve(values.backend ?? join(root, 'backend')),
resolve(values.frontend ?? join(root, 'frontend')),
values.install,
);
process.stdout.write(`${JSON.stringify(report)}\n`);
} catch (error) {
console.error(`构建工具链检查失败:${error.message}`);
process.exitCode = 1;
}
}
+164
View File
@@ -0,0 +1,164 @@
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'node:test';
import { installDependencies, npmEnvironment, validateEnvironment, validateFrontend, validateGoVersion } from './toolchain.mjs';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
function fixture() {
const frontend = join(root, 'frontend');
const pkg = JSON.parse(readFileSync(join(frontend, 'package.json'), 'utf8'));
return {
pkg,
lock: JSON.parse(readFileSync(join(frontend, 'package-lock.json'), 'utf8')),
npmrc: readFileSync(join(frontend, '.npmrc'), 'utf8'),
actual: { ...pkg.engines },
};
}
function validate(value) {
validateFrontend(value.pkg, value.lock, value.npmrc, value.actual);
}
test('仓库版本声明、锁文件和安装策略一致', () => {
validate(fixture());
});
for (const [name, mutate] of Object.entries({
'拒绝错误 Node 版本': (f) => { f.actual.node = '24.0.0'; },
'拒绝错误 npm 版本': (f) => { f.actual.npm = '10.0.0'; },
'拒绝工具链范围版本': (f) => { f.pkg.engines.node = '^24.19.0'; },
'拒绝开发工具链警告降级': (f) => { f.pkg.devEngines.runtime.onFail = 'warn'; },
'拒绝包管理器声明漂移': (f) => { f.pkg.packageManager = 'npm@10.0.0'; },
'拒绝旧版锁文件': (f) => { f.lock.lockfileVersion = 2; },
'拒绝锁文件工具链漂移': (f) => { f.lock.packages[''].engines.node = '24.0.0'; },
'拒绝依赖范围版本': (f) => { f.pkg.dependencies.vue = '^3.5.41'; },
'拒绝依赖锁定漂移': (f) => { f.lock.packages['node_modules/vue'].version = '3.5.0'; },
'拒绝锁文件多余直接依赖': (f) => { f.lock.packages[''].dependencies.extra = '1.0.0'; },
'拒绝前端标识漂移': (f) => { f.lock.name = 'other-project'; },
'拒绝放行安装脚本': (f) => { f.npmrc = f.npmrc.replace('ignore-scripts=true', 'ignore-scripts=false'); },
'拒绝重复安装策略': (f) => { f.npmrc += '\nignore-scripts=false\n'; },
'拒绝关闭版本限制': (f) => { f.npmrc = f.npmrc.replace('engine-strict=true', 'engine-strict=false'); },
'拒绝额外 npm 配置': (f) => { f.npmrc += '\nforce=true\n'; },
'拒绝脚本白名单绕行': (f) => { f.pkg.allowScripts = { esbuild: true }; },
'拒绝全量脚本放行': (f) => { f.pkg.dangerouslyAllowAllScripts = true; },
})) {
test(name, () => {
const value = fixture();
mutate(value);
assert.throws(() => validate(value));
});
}
test('Go 工具链必须精确匹配模块基线', () => {
assert.equal(validateGoVersion('module test\n\ngo 1.26.6\n', 'go1.26.6'), '1.26.6');
for (const version of ['go1.26.2', 'go1.26.7', 'devel go1.27', '']) {
assert.throws(() => validateGoVersion('go 1.26.6\n', version));
}
assert.throws(() => validateGoVersion('go 1.26\n', 'go1.26.6'));
});
test('拒绝外部构建覆盖和未支持的构建机', () => {
const host = { platform: 'win32', arch: 'x64' };
validateEnvironment({}, host);
validateEnvironment({ GOWORK: 'off' }, { ...host, platform: 'linux' });
for (const env of [{ ESBUILD_BINARY_PATH: 'external' }, { GOWORK: 'go.work' }, { GOFLAGS: '-mod=mod' }]) {
assert.throws(() => validateEnvironment(env, host));
}
assert.throws(() => validateEnvironment({}, { ...host, platform: 'darwin' }));
assert.throws(() => validateEnvironment({}, { ...host, arch: 'arm64' }));
});
test('发布入口在创建目录前检查并安装依赖,构建后复核版本文件', () => {
const script = readFileSync(join(root, 'build', 'build-release.ps1'), 'utf8');
const install = script.indexOf('--frontend $frontend --install');
assert.ok(install > 0 && install < script.indexOf('New-Item -ItemType Directory -Path $releaseDirectory'));
assert.match(script, /--ignore-scripts run build/);
assert.match(script, /go build -trimpath -mod=readonly/);
assert.match(script, /build_environment = \$toolchain/);
assert.match(script, /\$verifiedToolchain\.input_sha256/);
});
test('工具链命令实际拒绝外部二进制覆盖', () => {
const result = spawnSync(process.execPath, [join(root, 'build', 'toolchain.mjs'), '--install'], {
cwd: root, encoding: 'utf8', windowsHide: true,
env: { ...process.env, ESBUILD_BINARY_PATH: 'unreviewed-binary' },
});
assert.equal(result.status, 1);
assert.match(result.stderr, /禁止使用锁文件之外的 esbuild/);
assert.equal(result.stdout, '');
});
test('嵌套 npm 安装不继承项目安装禁止的用户脚本白名单', () => {
const original = { npm_config_allow_scripts: 'fixture', NPM_CONFIG_ALLOW_SCRIPTS: 'fixture', PATH: 'retained' };
assert.deepEqual(npmEnvironment(original), { PATH: 'retained' });
assert.equal(original.npm_config_allow_scripts, 'fixture');
});
test('发布脚本实际在工具链拒绝后不创建产物', { skip: process.platform !== 'win32' }, (t) => {
const directory = mkdtempSync(join(tmpdir(), 'scert-release-preflight-'));
t.after(() => rmSync(directory, { recursive: true, force: true }));
const output = join(directory, 'release');
const result = spawnSync('powershell.exe', [
'-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', join(root, 'build', 'build-release.ps1'),
'-OutputDirectory', output,
], {
cwd: root, encoding: 'utf8', windowsHide: true,
env: { ...process.env, ESBUILD_BINARY_PATH: 'unreviewed-binary' },
});
assert.equal(result.status, 1, `${result.error ?? ''}\n${result.stdout}\n${result.stderr}`);
assert.match(result.stderr, /esbuild/);
assert.ok(!existsSync(output));
});
test('真实离线安装禁止根项目及依赖脚本,命令行策略覆盖环境绕行', { timeout: 120000 }, (t) => {
const directory = mkdtempSync(join(tmpdir(), 'scert-install-policy-'));
t.after(() => rmSync(directory, { recursive: true, force: true }));
const dependency = join(directory, 'dependency');
mkdirSync(dependency);
const script = 'node -e "require(\'node:fs\').writeFileSync(\'lifecycle-ran\', \'yes\')"';
writeFileSync(join(dependency, 'package.json'), JSON.stringify({
name: 'scert-policy-fixture', version: '1.0.0', scripts: { postinstall: script },
}));
writeFileSync(join(directory, 'package.json'), JSON.stringify({
name: 'scert-policy-test', version: '1.0.0', private: true,
dependencies: { 'scert-policy-fixture': 'file:./dependency' },
scripts: { postinstall: script },
}));
writeFileSync(join(directory, '.npmrc'), 'ignore-scripts=true\n');
const run = (args) => {
const options = { cwd: directory, encoding: 'utf8', windowsHide: true, env: npmEnvironment() };
const result = process.platform === 'win32'
? spawnSync(`${npmCommand} ${args.join(' ')}`, { ...options, shell: true })
: spawnSync(npmCommand, args, options);
assert.equal(result.status, 0, `${result.error ?? ''}\n${result.stdout}\n${result.stderr}`);
};
run(['install', '--package-lock-only', '--ignore-scripts', '--offline', '--no-audit', '--no-fund']);
const lock = readFileSync(join(directory, 'package-lock.json'));
const envKeys = ['npm_config_ignore_scripts', 'npm_config_dangerously_allow_all_scripts', 'npm_config_offline'];
const previous = envKeys.map((key) => process.env[key]);
try {
process.env.npm_config_ignore_scripts = 'false';
process.env.npm_config_dangerously_allow_all_scripts = 'true';
process.env.npm_config_offline = 'true';
installDependencies(directory);
} finally {
envKeys.forEach((key, i) => {
if (previous[i] === undefined) delete process.env[key];
else process.env[key] = previous[i];
});
}
assert.ok(existsSync(join(directory, 'node_modules', 'scert-policy-fixture', 'package.json')));
assert.ok(!existsSync(join(directory, 'lifecycle-ran')));
assert.ok(!existsSync(join(dependency, 'lifecycle-ran')));
assert.deepEqual(readFileSync(join(directory, 'package-lock.json')), lock);
// 正向对照证明夹具确实会执行脚本,避免用未触发的脚本证明禁止策略。
run(['ci', '--ignore-scripts=false', '--dangerously-allow-all-scripts', '--offline', '--no-audit', '--no-fund']);
assert.ok(existsSync(join(directory, 'lifecycle-ran')));
assert.ok(existsSync(join(dependency, 'lifecycle-ran')));
});