Skip to content

Commit

Permalink
feat: intruduce $.preferLocal
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 3, 2022
1 parent 3346eee commit a665caf
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ import {semver} from 'zx-extra'
semver.gte('1.0.1', '1.0.0')
```

### $.preferLocal
In npm run scripts you can execute locally installed binaries by name. This enables the same for zx.
```js
$`terser input.js --compress ecma=2015,computed_props=false`
```

### `$.raw`
Evaluates target cmd as is without `shq`.
```js
Expand Down
66 changes: 66 additions & 0 deletions npm-shrinkwrap.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"zx-extra": "./src/main/js/cli.mjs"
},
"scripts": {
"test": "node ./src/main/js/cli.mjs ./src/test/js/test.mjs",
"test": "PATH=$(env -i bash -c 'echo $PATH') node ./src/main/js/cli.mjs ./src/test/js/test.mjs",
"publish": "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 All @@ -35,8 +35,10 @@
},
"homepage": "https://github.com/qiwi/zx-extra#readme",
"dependencies": {
"@qiwi/deep-proxy": "^1.9.0",
"@types/node": "^17.0.38",
"@types/semver": "^7.3.9",
"npm-run-path": "^5.1.0",
"zx": "^6.2.0"
}
}
5 changes: 5 additions & 0 deletions src/main/js/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as semver from 'semver'
import {ProcessPromise} from 'zx'

export * from 'zx'
export { semver }

interface $ {
raw: $
silent: $
preferLocal?: boolean
opt: (options: any) => $
}

export function createHook(opts?: $, name?: string, cb?: (p: ProcessPromise) => any, configurable?: boolean)
21 changes: 20 additions & 1 deletion src/main/js/index.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import {$, quiet, ProcessPromise} from 'zx'
import {$ as _$, quiet, ProcessPromise} from 'zx'
import {ctx} from 'zx/experimental'
import {isTemplateSignature, randomId} from './util.mjs'
import {npmRunPath} from 'npm-run-path'
import {DeepProxy} from '@qiwi/deep-proxy'

export { semver } from './semver.mjs'
export * from 'zx'

export const $ = new DeepProxy(_$, ({DEFAULT, trapName, args}) => {
if (trapName === 'apply') {
const [t,, receiver] = args
if (!t.preferLocal) {
return DEFAULT
}
const env = t.env
t.env = {...t.env, PATH: npmRunPath({cwd: t.cwd})}
const res = t(...receiver)
t.env = env

return res
}

return DEFAULT
})

$.raw = async (...args) => {
const q = $.quote
$.quote = v => v
Expand Down
16 changes: 16 additions & 0 deletions src/test/js/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ import {$, semver, createHook} from '../../main/js/index.mjs'
}
}

// preferLocal
{
$.verbose = 0
try {
await $`ps-tree`
} catch (e){
assert.ok(/command not found/.test(e.message))
}

$.preferLocal = true

await $`ps-tree`

$.verbose = 2
}

0 comments on commit a665caf

Please sign in to comment.