Skip to content

Commit

Permalink
refactor: set npm install opts via std env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Aug 28, 2022
1 parent 8097d81 commit 9d6ca3a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 38 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,19 +409,17 @@ await spinner('working...', () => $`sleep 99`)

## CLI

| Flag | Description | Default |
|-----------------------|-------------------------------------------------------|---------|
| `--quiet` | don't echo commands | `false` |
| `--shell=<path>` | custom shell binary | |
| `--prefix=<command>` | prefix all commands | |
| `--interactive, -i` | start repl | |
| `--eval=<js>, -e` | evaluate script | |
| `--experimental` | enable new api proposals | |
| `--install` | parse and load script dependencies from the registry | `false` |
| `--registry=<url>` | registry url to use for install | |
| `--userconfig=<path>` | .npmrc config path to use for install | |
| `--version, -v` | print current zx version | |
| `--help, -h` | print help | |
| Flag | Description | Default |
|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
| `--quiet` | don't echo commands | `false` |
| `--shell=<path>` | custom shell binary | |
| `--prefix=<command>` | prefix all commands | |
| `--interactive, -i` | start repl | |
| `--eval=<js>, -e` | evaluate script | |
| `--experimental` | enable new api proposals | |
| `--install` | parse and load script dependencies from the registry. You can pass additional [params via env vars](https://docs.npmjs.com/cli/v8/using-npm/config) like `npm_config_registry=<url>` or `npm_config_userconfig=<path>`. | `false` |
| `--version, -v` | print current zx version | |
| `--help, -h` | print help | |

```bash
zx script.js
Expand Down
8 changes: 1 addition & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ async function writeAndImport(
await fs.writeFile(filepath, contents)

if (argv.install) {
await installDeps(parseDeps(contents), {
prefix: dirname(filepath),
registry: argv.registry,
userconfig: argv.userconfig,
})
await installDeps(parseDeps(contents), dirname(filepath))
}

let wait = importPath(filepath, origin)
Expand Down Expand Up @@ -264,8 +260,6 @@ function printUsage() {
--eval=<js>, -e evaluate script
--experimental enable new api proposals
--install parse and load script dependencies from the registry
--registry=<url> registry url to use for install
--userconfig=<path> .npmrc config path to use for install
--version, -v print current zx version
--help, -h print help
`)
Expand Down
12 changes: 2 additions & 10 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,15 @@

import { $ } from './core.js'

interface DepOptions {
userconfig?: string
registry?: string
prefix?: string
}

export async function installDeps(
dependencies: Record<string, any> = {},
options: DepOptions = {}
prefix?: string
) {
const pkgs = Object.entries(dependencies).map(
([name, version]) => `${name}@${version}`
)

const flags = Object.entries(options)
.filter(([, value]) => !!value)
.map(([name, value]) => `--${name}=${value}`)
const flags = prefix ? `--prefix=${prefix}` : ''

if (pkgs.length === 0) {
return
Expand Down
13 changes: 5 additions & 8 deletions test/deps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@ const test = suite('deps')
$.verbose = false

test('installDeps() loader works via JS API', async () => {
await installDeps(
{
cpy: '9.0.1',
'lodash-es': '4.17.21',
},
{ registry: 'https://registry.yarnpkg.com/' }
)
await installDeps({
cpy: '9.0.1',
'lodash-es': '4.17.21',
})
assert.instance((await import('cpy')).default, Function)
assert.instance((await import('lodash-es')).pick, Function)
})

test('installDeps() loader works via CLI', async () => {
let out =
await $`node build/cli.js --install --registry="https://registry.yarnpkg.com" <<< 'import lodash from "lodash" /* 4.17.15 */; console.log(lodash.VERSION)'`
await $`npm_config_registry="https://registry.yarnpkg.com" node build/cli.js --install <<< 'import _ from "lodash" /* 4.17.15 */; console.log(_.VERSION)'`
assert.match(out.stdout, '4.17.15')
})

Expand Down

0 comments on commit 9d6ca3a

Please sign in to comment.