From 765b203803c6c60ef0494a01f8727028e155e360 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Fri, 10 May 2024 13:24:39 +0300 Subject: [PATCH] refactor: npm-run-path with own helper --- package-lock.json | 34 ++++------------------------------ package.json | 5 ++--- src/main/js/index.mjs | 11 ++++------- src/main/js/util.mjs | 21 +++++++++++++++++++++ src/test/js/test.mjs | 18 ++++++++++++------ 5 files changed, 43 insertions(+), 46 deletions(-) diff --git a/package-lock.json b/package-lock.json index d104b86..88f29f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,8 +17,7 @@ "ini": "^4.1.2", "ip": "^2.0.1", "is-reachable": "^5.2.1", - "npm-run-path": "^5.3.0", - "semver": "^7.6.1", + "semver": "^7.6.2", "ssri": "^10.0.6", "tempy": "^3.1.0", "zx": "8.0.2-dev.b6420eb" @@ -1021,31 +1020,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/npm-run-path": { - "version": "5.3.0", - "resolved": "https://registry.npmjs.org/npm-run-path/-/npm-run-path-5.3.0.tgz", - "integrity": "sha512-ppwTtiJZq0O/ai0z7yfudtBpWIoxM8yE6nHi1X47eFR2EWORqfbu6CnPlNsjeN683eT0qG6H/Pyf9fCcvjnnnQ==", - "dependencies": { - "path-key": "^4.0.0" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/npm-run-path/node_modules/path-key": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/path-key/-/path-key-4.0.0.tgz", - "integrity": "sha512-haREypq7xkM7ErfgIyA0z+Bj4AGKlMSdlQE2jvJo6huWD1EdkKYV+G/T4nq0YEF2vgTT8kqMFKo1uHn950r4SQ==", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -1309,9 +1283,9 @@ } }, "node_modules/semver": { - "version": "7.6.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.1.tgz", - "integrity": "sha512-f/vbBsu+fOiYt+lmwZV0rVwJScl46HppnOA1ZvIuBWKOTlllpyJ3bfVax76/OrhCH38dyxoDIA8K7uB963IYgA==", + "version": "7.6.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.2.tgz", + "integrity": "sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==", "bin": { "semver": "bin/semver.js" }, diff --git a/package.json b/package.json index 03ea71e..c8e1d42 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "test": "PATH=$(env -i bash -c 'echo $PATH') node ./src/main/js/cli.mjs ./src/test/js/test.mjs --quiet", "test:cov": "c8 npm run test && c8 report -r lcov", "test:bun": "bun ./src/main/js/cli.mjs ./src/test/js/test.mjs --quiet", - "publish:byhand": "npm publish --no-git-tag-version", + "publish:manual": "npm publish --no-git-tag-version", "publish:beta": "npm publish --no-git-tag-version --tag beta", "publish:rc": "npm publish --no-git-tag-version --tag rc" }, @@ -46,8 +46,7 @@ "ini": "^4.1.2", "ip": "^2.0.1", "is-reachable": "^5.2.1", - "npm-run-path": "^5.3.0", - "semver": "^7.6.1", + "semver": "^7.6.2", "ssri": "^10.0.6", "tempy": "^3.1.0", "zx": "8.0.2-dev.b6420eb" diff --git a/src/main/js/index.mjs b/src/main/js/index.mjs index 0175b7e..cfd84c4 100644 --- a/src/main/js/index.mjs +++ b/src/main/js/index.mjs @@ -1,9 +1,8 @@ import {$ as _$, quiet, ProcessPromise, within, ProcessOutput} from 'zx' import childProcess from 'node:child_process' import process from 'node:process' -import {isTemplateSignature, randomId} from './util.mjs' -import {npmRunPath} from 'npm-run-path' import {DeepProxy} from '@qiwi/deep-proxy' +import {injectNmBinToPathEnv, isTemplateSignature, randomId} from './util.mjs' import {semver} from './goods.mjs' export * from 'zx' @@ -22,13 +21,11 @@ ProcessOutput.prototype.toString = function () { export const $ = new DeepProxy(_$, ({name, DEFAULT, target: t, trapName, args}) => { if (trapName === 'apply') { - if (!t.preferLocal) { - return DEFAULT - } + if (!t.preferLocal) return DEFAULT + const env = t.env try { - const PATH = npmRunPath({cwd: t.cwd}) - t.env = {...t.env, PATH} + t.env = injectNmBinToPathEnv(t.env, t.cwd) return t(...args) } finally { t.env = env diff --git a/src/main/js/util.mjs b/src/main/js/util.mjs index 995c06f..a601a94 100644 --- a/src/main/js/util.mjs +++ b/src/main/js/util.mjs @@ -1,3 +1,5 @@ +import path from 'node:path' + export const isString = (obj) => typeof obj === 'string' export const randomId = () => Math.random().toString(36).slice(2) @@ -7,3 +9,22 @@ export const isTemplateSignature = (pieces, ...args) => { return Array.isArray(pieces) && pieces.every(isString) && lastIdx === args.length } + +export const injectNmBinToPathEnv = (env, ...dirs) => { + const pathKey = + process.platform === 'win32' + ? Object.keys(env) + .reverse() + .find((key) => key.toUpperCase() === 'PATH') || 'Path' + : 'PATH' + const pathValue = dirs + .map((c) => c && path.resolve(c, 'node_modules', '.bin')) + .concat(env[pathKey]) + .filter(Boolean) + .join(path.delimiter) + + return { + ...env, + [pathKey]: pathValue, + } +} diff --git a/src/test/js/test.mjs b/src/test/js/test.mjs index 2ca531e..e37f1c3 100644 --- a/src/test/js/test.mjs +++ b/src/test/js/test.mjs @@ -167,19 +167,25 @@ password = dbpassword // preferLocal { + const env = $.env + const PATH = env.PATH + .split(':') + .filter(p => !/\/node_modules\//.test(p)) + .join(':') + $.env = {...$.env, PATH } $.verbose = 0 - try { - await $`semver` - } catch (e){ - assert.ok(/command not found/.test(e.message)) - } + $.nothrow = true - $.preferLocal = true + const e = await $`semver` + assert.ok(/command not found/.test(e.message)) + $.preferLocal = true await $`semver` $.preferLocal = false $.verbose = 2 + $.env = env + $.nothrow = false } // ip