From 29e0f82ad569386432493bf034ac450cc1a65713 Mon Sep 17 00:00:00 2001 From: akitaSummer <644171127@qq.com> Date: Thu, 1 Feb 2024 17:41:48 +0800 Subject: [PATCH] feat: generate package lock file (#63) * feat: generate package lock file * fix: fix test * fix: use child_process generate package-lock.json * fix: add package-lock option * fix: use execSync * fix: del env in test * fix: use execa handle npm install --------- Co-authored-by: shixia.ly --- README.md | 1 - integration/index.test.js | 1 + packages/cli/bin/rapid.js | 7 ++++ packages/cli/lib/index.js | 5 +++ packages/cli/lib/util.js | 33 +++++++++++++++++ .../fixtures/not-exist-lock-file/package.json | 7 ++++ packages/cli/test/package_lock.test.js | 37 +++++++++++++++++++ 7 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 packages/cli/test/fixtures/not-exist-lock-file/package.json diff --git a/README.md b/README.md index 896554e..ab0f657 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,6 @@ ### Independent Client ```bash $ npm i @cnpmjs/rapid --registry=https://registry.npmmirror.com -$ npm i --package-lock-only --registry=https://registry.npmmirror.com $ rapid install ``` diff --git a/integration/index.test.js b/integration/index.test.js index 946df43..8e220bd 100644 --- a/integration/index.test.js +++ b/integration/index.test.js @@ -87,4 +87,5 @@ describe('test/index.test.js', () => { .end(done); }); }); + }); diff --git a/packages/cli/bin/rapid.js b/packages/cli/bin/rapid.js index 150fa74..135f831 100755 --- a/packages/cli/bin/rapid.js +++ b/packages/cli/bin/rapid.js @@ -32,12 +32,18 @@ const argv = yargs describe: 'Dependency types to omit from the installation tree on disk', type: 'array', default: [], + }) + .option('package-lock', { + describe: 'Whether to generate package-lock.json file', + type: 'boolean', + default: true, }); }, handler: async argv => { const ignoreScripts = argv['ignore-scripts']; const mode = argv.by || NpmFsMode.NPM; const productionMode = argv.production || argv.omit.includes('dev') || process.env.NODE_ENV === 'production'; + const noPackageLock = !argv['package-lock']; const cwd = process.cwd(); const pkgRes = await util.readPkgJSON(); @@ -57,6 +63,7 @@ const argv = yargs nydusMode: NYDUS_TYPE.FUSE, ignoreScripts, productionMode, + noPackageLock, }); Alert.success('🚀 Success', [ diff --git a/packages/cli/lib/index.js b/packages/cli/lib/index.js index f8b18f4..a114cb0 100644 --- a/packages/cli/lib/index.js +++ b/packages/cli/lib/index.js @@ -18,6 +18,11 @@ const { MirrorConfig } = require('binary-mirror-config'); // 有依赖树(package-lock.json)走 npm / npminstall 极速安装 exports.install = async options => { options.env = util.getEnv(options.env, options.args); + + if (!options.noPackageLock) { + await util.generatePackageLock(options.cwd); + } + const { packageLock } = options.packageLock || (await util.readPackageLock(options.cwd)); const currentMountInfo = await util.listMountInfo(); diff --git a/packages/cli/lib/util.js b/packages/cli/lib/util.js index 8ffa051..480a0f0 100644 --- a/packages/cli/lib/util.js +++ b/packages/cli/lib/util.js @@ -600,6 +600,39 @@ exports.readPkgJSON = async function readPkgJSON(cwd) { return { pkg, pkgPath }; }; +exports.generatePackageLock = async cwd => { + let isExist = true; + try { + const lockPath = path.join(cwd || exports.findLocalPrefix(), './package-lock.json'); + await await fs.stat(lockPath); + } catch { + isExist = false; + } + try { + if (!isExist) { + console.log('npm install --force --package-lock-only --ignore-scripts is running'); + + const childProcess = execa('npm', [ 'install', '--force', '--package-lock-only', '--ignore-scripts' ], { + cwd, + stdio: 'inherit', + }); + + await new Promise((resolve, reject) => { + childProcess.then(resolve).catch(reject); + + process.on('exit', () => { + childProcess.kill(); + }); + }); + } + } catch (e) { + 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'); diff --git a/packages/cli/test/fixtures/not-exist-lock-file/package.json b/packages/cli/test/fixtures/not-exist-lock-file/package.json new file mode 100644 index 0000000..5a9685a --- /dev/null +++ b/packages/cli/test/fixtures/not-exist-lock-file/package.json @@ -0,0 +1,7 @@ +{ + "name": "test", + "version": "1.0.0", + "dependencies": { + "lodash.has": "4.5.2" + } +} diff --git a/packages/cli/test/package_lock.test.js b/packages/cli/test/package_lock.test.js index bfe9f22..b3d5c79 100644 --- a/packages/cli/test/package_lock.test.js +++ b/packages/cli/test/package_lock.test.js @@ -2,7 +2,13 @@ const assert = require('node:assert'); const path = require('node:path'); +const fs = require('node:fs/promises'); +const mm = require('mm'); const PackageLock = require('../lib/package_lock').PackageLock; +const { install } = require('../lib'); +const httpclient = require('../lib/httpclient'); +const nydusd = require('../lib/nydusd'); +const downloadDependency = require('../lib/download_dependency'); const fixture = path.join(__dirname, 'fixtures/lockfile'); @@ -43,4 +49,35 @@ describe('test/package_lock.test.js', () => { assert.strictEqual(packageLock.isWorkspacesPkg('xxx/lodash.has'), false); assert.strictEqual(packageLock.isDepsPkg('node_modules/lodash.has'), true); }); + + describe('not exist lock file', async () => { + let fixture; + beforeEach(async () => { + mm(process, 'cwd', () => fixture); + mm(nydusd, 'startNydusFs', async () => { }); + mm(downloadDependency, 'download', async () => { + return { + depsTree: [ 1 ], + }; + }); + + }); + afterEach(async () => { + await fs.rm(path.join(fixture, 'node_modules'), { recursive: true, force: true }); + await fs.rm(path.join(fixture, 'package-lock.json'), { force: true }); + mm.restore(); + }); + + it('should run all project installation scripts', async () => { + fixture = path.join(__dirname, './fixtures/not-exist-lock-file'); + const pkg = require(path.join(fixture, 'package.json')); + await install({ + httpclient, + pkg, + cwd: fixture, + console: global.console, + }); + await fs.stat(path.join(fixture, 'package-lock.json')); + }); + }); });