Skip to content

Commit

Permalink
refactor: npm-run-path with own helper
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 10, 2024
1 parent 474f3de commit 765b203
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
34 changes: 4 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 4 additions & 7 deletions src/main/js/index.mjs
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/main/js/util.mjs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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,
}
}
18 changes: 12 additions & 6 deletions src/test/js/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 765b203

Please sign in to comment.