From 6ed5740bdb559409d7b3319ad8458e3efe337350 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sat, 22 Aug 2020 20:19:35 +0300 Subject: [PATCH] fix: use npm bin from deps instead of global --- lib/add-channel.js | 3 ++- lib/get-npm.js | 8 ++++++++ lib/prepare.js | 3 ++- lib/publish.js | 11 ++++++----- lib/verify-auth.js | 3 ++- test/get-npm.test.js | 23 +++++++++++++++++++++++ 6 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 lib/get-npm.js create mode 100644 test/get-npm.test.js diff --git a/lib/add-channel.js b/lib/add-channel.js index ccb39814..0d10dd5f 100644 --- a/lib/add-channel.js +++ b/lib/add-channel.js @@ -2,6 +2,7 @@ const execa = require('execa'); const getRegistry = require('./get-registry'); const getChannel = require('./get-channel'); const getReleaseInfo = require('./get-release-info'); +const getNpm = require('./get-npm'); module.exports = async (npmrc, {npmPublish}, pkg, context) => { const { @@ -19,7 +20,7 @@ module.exports = async (npmrc, {npmPublish}, pkg, context) => { logger.log(`Adding version ${version} to npm registry on dist-tag ${distTag}`); const result = execa( - 'npm', + getNpm(), ['dist-tag', 'add', `${pkg.name}@${version}`, distTag, '--userconfig', npmrc, '--registry', registry], { cwd, diff --git a/lib/get-npm.js b/lib/get-npm.js new file mode 100644 index 00000000..402fc389 --- /dev/null +++ b/lib/get-npm.js @@ -0,0 +1,8 @@ +const {resolve} = require('path'); +const isWindows = () => /^(msys|cygwin)$/.test(process.env.OSTYPE) || process.platform === 'win32'; + +module.exports = () => { + const cmd = isWindows() ? 'npm.cmd' : 'npm'; + + return resolve(require.resolve('npm'), '../../../.bin', cmd); +}; diff --git a/lib/prepare.js b/lib/prepare.js index 25fb8ee1..0ddf1142 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -1,6 +1,7 @@ const path = require('path'); const {move} = require('fs-extra'); const execa = require('execa'); +const getNpm = require('./get-npm'); module.exports = async (npmrc, {tarballDir, pkgRoot}, {cwd, env, stdout, stderr, nextRelease: {version}, logger}) => { const basePath = pkgRoot ? path.resolve(cwd, pkgRoot) : cwd; @@ -8,7 +9,7 @@ module.exports = async (npmrc, {tarballDir, pkgRoot}, {cwd, env, stdout, stderr, logger.log('Write version %s to package.json in %s', version, basePath); const versionResult = execa( - 'npm', + getNpm(), ['version', version, '--userconfig', npmrc, '--no-git-tag-version', '--allow-same-version'], { cwd: basePath, diff --git a/lib/publish.js b/lib/publish.js index c79d5652..ae2a2758 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -1,5 +1,6 @@ const path = require('path'); const execa = require('execa'); +const getNpm = require('./get-npm'); const getRegistry = require('./get-registry'); const getChannel = require('./get-channel'); const getReleaseInfo = require('./get-release-info'); @@ -18,13 +19,13 @@ module.exports = async (npmrc, {npmPublish, pkgRoot}, pkg, context) => { const basePath = pkgRoot ? path.resolve(cwd, pkgRoot) : cwd; const registry = getRegistry(pkg, context); const distTag = getChannel(channel); + const npm = getNpm(); logger.log(`Publishing version ${version} to npm registry on dist-tag ${distTag}`); - const result = execa( - 'npm', - ['publish', basePath, '--userconfig', npmrc, '--tag', distTag, '--registry', registry], - {cwd, env} - ); + const result = execa(npm, ['publish', basePath, '--userconfig', npmrc, '--tag', distTag, '--registry', registry], { + cwd, + env, + }); result.stdout.pipe(stdout, {end: false}); result.stderr.pipe(stderr, {end: false}); await result; diff --git a/lib/verify-auth.js b/lib/verify-auth.js index 03421697..c958f15f 100644 --- a/lib/verify-auth.js +++ b/lib/verify-auth.js @@ -3,6 +3,7 @@ const normalizeUrl = require('normalize-url'); const AggregateError = require('aggregate-error'); const getError = require('./get-error'); const getRegistry = require('./get-registry'); +const getNpm = require('./get-npm'); const setNpmrcAuth = require('./set-npmrc-auth'); module.exports = async (npmrc, pkg, context) => { @@ -18,7 +19,7 @@ module.exports = async (npmrc, pkg, context) => { if (normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)) { try { - const whoamiResult = execa('npm', ['whoami', '--userconfig', npmrc, '--registry', registry], {cwd, env}); + const whoamiResult = execa(getNpm(), ['whoami', '--userconfig', npmrc, '--registry', registry], {cwd, env}); whoamiResult.stdout.pipe(stdout, {end: false}); whoamiResult.stderr.pipe(stderr, {end: false}); await whoamiResult; diff --git a/test/get-npm.test.js b/test/get-npm.test.js new file mode 100644 index 00000000..7e1c1e44 --- /dev/null +++ b/test/get-npm.test.js @@ -0,0 +1,23 @@ +const {resolve} = require('path'); +const test = require('ava'); +const getNpm = require('../lib/get-npm'); + +let ostype; + +test.before(() => { + ostype = process.env.OSTYPE; +}); + +test.after(() => { + process.env.OSTYPE = ostype; +}); + +test.serial('Get npm cmd for linux', (t) => { + process.env.OSTYPE = 'linux'; + t.is(getNpm(), resolve(__dirname, '../node_modules/.bin/npm')); +}); + +test.serial('Get npm cmd for windows', (t) => { + process.env.OSTYPE = 'msys'; + t.is(getNpm(), resolve(__dirname, '../node_modules/.bin/npm.cmd')); +});