Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: disable default powershell setup #757

Merged
merged 2 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,23 @@ export const defaults: Options = {
kill,
}
const isWin = process.platform == 'win32'
try {
defaults.shell = which.sync('bash')
defaults.prefix = 'set -euo pipefail;'
defaults.quote = quote
} catch (err) {
if (isWin) {
try {
defaults.shell = which.sync('powershell.exe')
defaults.postfix = '; exit $LastExitCode'
defaults.quote = quotePowerShell
} catch (err) {
// no powershell?
}

export function setupPowerShell() {
$.shell = which.sync('powershell.exe')
$.prefix = ''
$.postfix = '; exit $LastExitCode'
$.quote = quotePowerShell
}

export function setupBash() {
$.shell = which.sync('bash')
$.prefix = 'set -euo pipefail;'
$.quote = quote
}

function checkShell() {
if (!$.shell) {
throw new Error(`shell is not available: setup guide goes here`)
}
}

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

export const $: Shell & Options = new Proxy<Shell & Options>(
function (pieces, ...args) {
checkShell()

if (!Array.isArray(pieces)) {
return function (this: any, ...args: any) {
const self = this
Expand Down Expand Up @@ -179,6 +185,10 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
}
)

try {
setupBash()
} catch (err) {}

type Resolve = (out: ProcessOutput) => void
type IO = StdioPipe | StdioNull

Expand Down
2 changes: 2 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare global {
var fs: typeof _.fs
var glob: typeof _.glob
var globby: typeof _.globby
var kill: typeof _.kill
var minimist: typeof _.minimist
var nothrow: typeof _.nothrow
var os: typeof _.os
Expand All @@ -40,6 +41,7 @@ declare global {
var quote: typeof _.quote
var quotePowerShell: typeof _.quotePowerShell
var retry: typeof _.retry
var setupPowerShell: typeof _.setupPowerShell
var sleep: typeof _.sleep
var spinner: typeof _.spinner
var stdin: typeof _.stdin
Expand Down
7 changes: 7 additions & 0 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ describe('core', () => {
assert.equal((await p5).stdout, 'baz')
})

test('requires $.shell to be specified', async () => {
await within(() => {
$.shell = undefined
assert.throws(() => $`echo foo`, /shell/)
})
})

test('`$.sync()` provides synchronous API', () => {
const o1 = $.sync`echo foo`
const o2 = $({ sync: true })`echo foo`
Expand Down
8 changes: 4 additions & 4 deletions test/win32.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ _describe('win32', () => {
test('should work with windows-specific commands', async () => {
const p = await $`echo $0` // Bash is first by default.
assert.match(p.stdout, /bash/)

await within(async () => {
$.shell = which.sync('powershell.exe')
$.quote = quotePowerShell
setupPowerShell()
assert.match($.shell, /powershell/i)
const p = await $`get-host`
assert.match(p.stdout, /PowerShell/)
})
})

test('quotePowerShell works', async () => {
await within(async () => {
$.shell = which.sync('powershell.exe')
$.quote = quotePowerShell
setupPowerShell()
const p = await $`echo ${`Windows 'rulez!'`}`
assert.match(p.stdout, /Windows 'rulez!'/)
})
Expand Down
Loading