Skip to content

Commit

Permalink
fix: use child_process generate package-lock.json
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Dec 12, 2023
1 parent 74c2750 commit b81b97a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
7 changes: 6 additions & 1 deletion packages/cli/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ const { MirrorConfig } = require('binary-mirror-config');
// 有依赖树(package-lock.json)走 npm / npminstall 极速安装
exports.install = async options => {
options.env = util.getEnv(options.env, options.args);
const { packageLock } = options.packageLock || (await util.readPackageLock(options.cwd, options.noPackageLock));

if (!options.noPackageLock) {
await util.generatePackageLock(options.cwd);
}

const { packageLock } = options.packageLock || (await util.readPackageLock(options.cwd));

const currentMountInfo = await util.listMountInfo();

Expand Down
45 changes: 29 additions & 16 deletions packages/cli/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const { existsSync } = require('node:fs');
const os = require('node:os');
const url = require('node:url');
const crypto = require('node:crypto');
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
const mapWorkspaces = require('@npmcli/map-workspaces');
const Arborist = require('@npmcli/arborist');
const fuse_t = require('./fuse_t');
const { Spin } = require('./logger');

Expand Down Expand Up @@ -601,25 +602,37 @@ exports.readPkgJSON = async function readPkgJSON(cwd) {
return { pkg, pkgPath };
};

exports.readPackageLock = async function readPackageLock(cwd, noPackageLock) {
exports.generatePackageLock = async cwd => {
try {
const lockPath = path.join(cwd || exports.findLocalPrefix(), './package-lock.json');
if (!noPackageLock) {
try {
await fs.stat(lockPath);
} catch {
const arb = new Arborist({
packageLockOnly: true,
ignoreScripts: true,
lockfileVersion: 3,
strictSSL: false,
allowSameVersion: false,
path: cwd,
const isExist = await fs.stat(lockPath).then(() => true).catch(() => false);
if (!isExist) {
await new Promise((resolve, reject) => {
exec('npm install --force --package-lock-only --ignore-scripts', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${stderr}`);
reject(new Error(`${error}`));
return;
}
console.log(`npm install --force --package-lock-only --ignore-scripts is running: ${stdout}`);
if (stderr) {
console.error(`run "npm install --force --package-lock-only --ignore-scripts" error: ${stderr}`);
}
resolve();
});
console.log('[rapid] Calling npm generates package-lock.json by default');
await arb.reify({ save: true });
}
});
}
} catch {
Alert.error('Error', [
'generate package-lock.json error.',
'Run `npm i --package-lock-only` to generate it.',
]);
}
};

exports.readPackageLock = async function readPackageLock(cwd) {
try {
const lockPath = path.join(cwd || exports.findLocalPrefix(), './package-lock.json');
const packageLock = JSON.parse(await fs.readFile(lockPath, 'utf8'));
return { packageLock, lockPath };
} catch (e) {
Expand Down

0 comments on commit b81b97a

Please sign in to comment.