Skip to content

Commit 24dcf3a

Browse files
authoredMar 30, 2024··
refactor: disable default powershell setup (#757)
* refactor: disable default powershell setup * fix: rm win assert from powershell setup
1 parent fb9554f commit 24dcf3a

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed
 

‎src/core.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,23 @@ export const defaults: Options = {
103103
kill,
104104
}
105105
const isWin = process.platform == 'win32'
106-
try {
107-
defaults.shell = which.sync('bash')
108-
defaults.prefix = 'set -euo pipefail;'
109-
defaults.quote = quote
110-
} catch (err) {
111-
if (isWin) {
112-
try {
113-
defaults.shell = which.sync('powershell.exe')
114-
defaults.postfix = '; exit $LastExitCode'
115-
defaults.quote = quotePowerShell
116-
} catch (err) {
117-
// no powershell?
118-
}
106+
107+
export function setupPowerShell() {
108+
$.shell = which.sync('powershell.exe')
109+
$.prefix = ''
110+
$.postfix = '; exit $LastExitCode'
111+
$.quote = quotePowerShell
112+
}
113+
114+
export function setupBash() {
115+
$.shell = which.sync('bash')
116+
$.prefix = 'set -euo pipefail;'
117+
$.quote = quote
118+
}
119+
120+
function checkShell() {
121+
if (!$.shell) {
122+
throw new Error(`shell is not available: setup guide goes here`)
119123
}
120124
}
121125

@@ -125,6 +129,8 @@ function getStore() {
125129

126130
export const $: Shell & Options = new Proxy<Shell & Options>(
127131
function (pieces, ...args) {
132+
checkShell()
133+
128134
if (!Array.isArray(pieces)) {
129135
return function (this: any, ...args: any) {
130136
const self = this
@@ -179,6 +185,10 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
179185
}
180186
)
181187

188+
try {
189+
setupBash()
190+
} catch (err) {}
191+
182192
type Resolve = (out: ProcessOutput) => void
183193
type IO = StdioPipe | StdioNull
184194

‎src/globals.ts

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ declare global {
3131
var fs: typeof _.fs
3232
var glob: typeof _.glob
3333
var globby: typeof _.globby
34+
var kill: typeof _.kill
3435
var minimist: typeof _.minimist
3536
var nothrow: typeof _.nothrow
3637
var os: typeof _.os
@@ -40,6 +41,7 @@ declare global {
4041
var quote: typeof _.quote
4142
var quotePowerShell: typeof _.quotePowerShell
4243
var retry: typeof _.retry
44+
var setupPowerShell: typeof _.setupPowerShell
4345
var sleep: typeof _.sleep
4446
var spinner: typeof _.spinner
4547
var stdin: typeof _.stdin

‎test/core.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ describe('core', () => {
139139
assert.equal((await p5).stdout, 'baz')
140140
})
141141

142+
test('requires $.shell to be specified', async () => {
143+
await within(() => {
144+
$.shell = undefined
145+
assert.throws(() => $`echo foo`, /shell/)
146+
})
147+
})
148+
142149
test('`$.sync()` provides synchronous API', () => {
143150
const o1 = $.sync`echo foo`
144151
const o2 = $({ sync: true })`echo foo`

‎test/win32.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ _describe('win32', () => {
2222
test('should work with windows-specific commands', async () => {
2323
const p = await $`echo $0` // Bash is first by default.
2424
assert.match(p.stdout, /bash/)
25+
2526
await within(async () => {
26-
$.shell = which.sync('powershell.exe')
27-
$.quote = quotePowerShell
27+
setupPowerShell()
28+
assert.match($.shell, /powershell/i)
2829
const p = await $`get-host`
2930
assert.match(p.stdout, /PowerShell/)
3031
})
3132
})
3233

3334
test('quotePowerShell works', async () => {
3435
await within(async () => {
35-
$.shell = which.sync('powershell.exe')
36-
$.quote = quotePowerShell
36+
setupPowerShell()
3737
const p = await $`echo ${`Windows 'rulez!'`}`
3838
assert.match(p.stdout, /Windows 'rulez!'/)
3939
})

0 commit comments

Comments
 (0)
Please sign in to comment.