diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index ca2c9d6..4fe4e6a 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -15,7 +15,7 @@ jobs: uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 with: os: 'ubuntu-latest, macos-latest, windows-latest' - version: '14.18.0, 14, 16, 18' + version: '14.18.0, 14, 16, 18, 20' # npm install npm will be fail on Windows with Node.js 14.x, use npminstall instead # see https://github.com/npm/cli/issues/4438 install: 'npm i -g npminstall && npminstall' diff --git a/bin/cnpm b/bin/cnpm index 46db5c2..3dbd442 100755 --- a/bin/cnpm +++ b/bin/cnpm @@ -11,7 +11,6 @@ const extendsMethd = { doc: 1, sync: 1, user: 1, - update: 1, }; debug('cnpm %s', action); diff --git a/bin/cnpm-update b/bin/cnpm-update deleted file mode 100755 index 35106bc..0000000 --- a/bin/cnpm-update +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env node - -const debug = require('node:util').debuglog('cnpm:update'); -const path = require('node:path'); -const { fork } = require('node:child_process'); -const { rmSync } = require('node:fs'); - -const cwd = process.cwd(); -const cnpmCli = path.join(__dirname, 'cnpm'); -const options = { - stdio: 'inherit', -}; - -const nodeModulesDir = path.join(cwd, 'node_modules'); -console.log('[cnpm:update] Removing %s', nodeModulesDir); -rmSync(nodeModulesDir, { force: true, recursive: true }); - -console.log('[cnpm:update] Running "cnpm install" on %s', cwd); -fork(cnpmCli, [ 'install' ], options) - .on('exit', code => { - debug('exit %s', code); - process.exit(code); - }); - diff --git a/lib/origin_npm.js b/lib/origin_npm.js index 836c896..7df81b6 100644 --- a/lib/origin_npm.js +++ b/lib/origin_npm.js @@ -11,6 +11,7 @@ const program = parseArgv(); const rawArgs = program.rawArgs.slice(2); const args = []; let isInstall = false; +let isUpdate = false; let installer = 'npminstall'; for (let i = 0; i < rawArgs.length; i++) { @@ -22,6 +23,10 @@ for (let i = 0; i < rawArgs.length; i++) { isInstall = true; continue; } + if (i === 0 && (arg === 'update')) { + isUpdate = true; + continue; + } // support `$ cnpm i --by=npm` if (arg.indexOf('--by=') === 0) { @@ -77,6 +82,12 @@ if (isInstall) { if (!fs.existsSync(npmBin)) { npmBin = installer; } +} else if (isUpdate) { + execMethod = cp.fork; + stdio.push('ipc'); + npmBin = require.resolve('npminstall/bin/update.js'); + args.unshift('--china'); + args.unshift('--fix-bug-versions'); } else { npmBin = path.join(__dirname, '..', 'node_modules', '.bin', 'npm'); } diff --git a/test/cnpm-update.test.js b/test/cnpm-update.test.js index bb8e095..8d02cf1 100644 --- a/test/cnpm-update.test.js +++ b/test/cnpm-update.test.js @@ -13,18 +13,20 @@ describe('test/cnpm-update.test.js', () => { cwd: root, }) .debug() - .expect('stdout', /\[cnpm:update] Removing/) - .expect('stdout', /\[cnpm:update] Running "cnpm install" on /) + .expect('stdout', /\[npmupdate] removing/) + .expect('stdout', /\[npmupdate] reinstall on /) .expect('code', 0) .end(); await coffee.fork(cnpm, [ 'update', + '--offline', ], { cwd: root, }) .debug() - .expect('stdout', /\[cnpm:update] Removing/) - .expect('stdout', /\[cnpm:update] Running "cnpm install" on /) + .expect('stderr', /npminstall WARN running on offline mode/) + .expect('stdout', /\[npmupdate] removing/) + .expect('stdout', /\[npmupdate] reinstall on /) .expect('code', 0) .end(); });