Skip to content

Commit

Permalink
fix: Respect volta npm and yarn paths (antfu-collective#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
muzaisimao authored Dec 10, 2021
1 parent c2c6e8f commit 8614e03
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ dist
# IDE
.idea
_storage.json

# System files
.DS_Store
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@types/ini": "^1.3.30",
"@types/node": "^16.7.10",
"@types/prompts": "^2.4.0",
"@types/which": "^2.0.1",
"ava": "^3.15.0",
"c8": "^7.8.0",
"esbuild-register": "^3.0.0",
Expand All @@ -60,7 +61,8 @@
"rimraf": "^3.0.2",
"terminal-link": "^3.0.0",
"tsup": "^4.14.0",
"typescript": "^4.4.2"
"typescript": "^4.4.2",
"which": "^2.0.2"
},
"eslintConfig": {
"extends": "@antfu/eslint-config-ts"
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 5 additions & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import execa from 'execa'
import { Agent, agents } from './agents'
import { getDefaultAgent, getGlobalAgent } from './config'
import { detect, DetectOptions } from './detect'
import { remove } from './utils'
import { remove, getVoltaPrefix } from './utils'

const DEBUG_SIGN = '?'

Expand Down Expand Up @@ -63,6 +63,10 @@ export async function run(fn: Runner, args: string[], options: DetectOptions = {
if (!command)
return

const voltaPrefix = getVoltaPrefix()
if (voltaPrefix)
command = voltaPrefix.concat(' ').concat(command)

if (debug) {
// eslint-disable-next-line no-console
console.log(command)
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os from 'os'
import { execSync } from 'child_process'
import which from 'which'

export function remove<T>(arr: T[], v: T) {
const index = arr.indexOf(v)
Expand Down Expand Up @@ -27,3 +28,10 @@ export function cmdExists(cmd: string) {
return false
}
}

export function getVoltaPrefix(): string {
// https://blog.volta.sh/2020/11/25/command-spotlight-volta-run/
const VOLTA_PREFIX = 'volta run'
const hasVoltaCommand = which.sync('volta', { nothrow: true }) !== null
return hasVoltaCommand ? VOLTA_PREFIX : ''
}

0 comments on commit 8614e03

Please sign in to comment.