Skip to content

Commit

Permalink
Switch to powershell.exe
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Oct 4, 2022
1 parent cfa0fba commit 0c6c4e5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
parseDuration,
psTree,
quote,
quotePowerShell,
} from './util.js'

export type Shell = (
Expand Down Expand Up @@ -75,7 +76,8 @@ export const defaults: Options = {

try {
if (process.platform == 'win32') {
defaults.shell = true
defaults.shell = which.sync('powershell.exe')
defaults.quote = quotePowerShell
} else {
defaults.shell = which.sync('bash')
defaults.prefix = 'set -euo pipefail;'
Expand Down
14 changes: 14 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ export function quote(arg: string) {
)
}

export function quoteWindows(arg: string) {
if (/^[a-z0-9/_.\-@:=]+$/i.test(arg) || arg === '') {
return arg
}
return `"` + arg.replace(/"/g, '\\"').replace(/\n/g, '^\n') + `"`
}

export function quotePowerShell(arg: string) {
if (/^[a-z0-9/_.\-]+$/i.test(arg) || arg === '') {
return arg
}
return `'` + arg.replace(/'/g, "''") + `'`
}

export function exitCodeInfo(exitCode: number | null): string | undefined {
return {
2: 'Misuse of shell builtins',
Expand Down
6 changes: 6 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
noop,
parseDuration,
quote,
quotePowerShell,
randomId,
} from '../build/util.js'

Expand Down Expand Up @@ -58,6 +59,11 @@ test('quote()', () => {
assert.ok(quote(`'\f\n\r\t\v\0`) === `$'\\'\\f\\n\\r\\t\\v\\0'`)
})

test('quotePowerShgell()', () => {
assert.is(quotePowerShell('string'), 'string')
assert.is(quotePowerShell(`'`), `''`)
})

test('duration parsing works', () => {
assert.is(parseDuration(1000), 1000)
assert.is(parseDuration('2s'), 2000)
Expand Down
5 changes: 5 additions & 0 deletions test/win32.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ if (process.platform === 'win32') {
const p = await $`ver`
assert.match(p.stdout, /Windows/)
})

test('quotePowerShell works', async () => {
const p = await $`echo ${`Windows 'rulez!'`}`
assert.match(p.stdout, /Windows 'rulez!'/)
})
}

test.run()

0 comments on commit 0c6c4e5

Please sign in to comment.