Skip to content

Commit

Permalink
refactor: disable default powershell setup (#757)
Browse files Browse the repository at this point in the history
* refactor: disable default powershell setup

* fix: rm win assert from powershell setup
  • Loading branch information
antongolub committed Mar 30, 2024
1 parent fb9554f commit 24dcf3a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
36 changes: 23 additions & 13 deletions src/core.ts
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
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
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
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

0 comments on commit 24dcf3a

Please sign in to comment.