Skip to content

Commit

Permalink
fix: bind cwd to spawn call (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Mar 17, 2022
1 parent 3794306 commit cf369ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export function registerGlobals() {
export function $(pieces, ...args) {
let {verbose, shell, prefix, spawn, maxBuffer = 200 * 1024 * 1024 /* 200 MiB*/} = $
let __from = (new Error().stack.split(/^\s*at\s/m)[2]).trim()
let cwd = process.cwd()

let cmd = pieces[0], i = 0
while (i < args.length) {
Expand All @@ -81,7 +82,7 @@ export function $(pieces, ...args) {
}

let child = spawn(prefix + cmd, {
cwd: process.cwd(),
cwd,
shell: typeof shell === 'string' ? shell : true,
stdio: [promise._inheritStdin ? 'inherit' : 'pipe', 'pipe', 'pipe'],
windowsHide: true,
Expand Down
10 changes: 8 additions & 2 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,16 @@ if (test('The cd() works with relative paths')) {
try {
fs.mkdirpSync('/tmp/zx-cd-test/one/two')
cd('/tmp/zx-cd-test/one/two')
let p1 = $`pwd`
cd('..')
let p2 = $`pwd`
cd('..')
let pwd = (await $`pwd`).stdout.trim()
assert.equal(path.basename(pwd), 'zx-cd-test')
let p3 = $`pwd`

let results = (await Promise.all([p1, p2, p3]))
.map(p => path.basename(p.stdout.trim()))

assert.deepEqual(results, ['two', 'one', 'zx-cd-test'])
} finally {
fs.rmSync('/tmp/zx-cd-test', {recursive: true})
cd(__dirname)
Expand Down

0 comments on commit cf369ce

Please sign in to comment.