From 2ad03cee7c65ee1aaa8c39f0f9f0dd073e84ade0 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sat, 30 Mar 2024 12:35:22 +0300 Subject: [PATCH 1/2] refactor: disable default powershell setup --- src/core.ts | 29 ++++++++++++++++++++--------- src/globals.ts | 2 ++ test/core.test.js | 13 +++++++++++++ test/win32.test.js | 6 ++---- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/core.ts b/src/core.ts index f005e8510f..e53b2e82a4 100644 --- a/src/core.ts +++ b/src/core.ts @@ -104,18 +104,27 @@ export const defaults: Options = { } const isWin = process.platform == 'win32' try { + setupBash() +} catch (err) {} + +export function setupPowerShell() { + if (!isWin) throw new Error('PowerShell is only available on Windows') + + defaults.shell = which.sync('powershell.exe') + defaults.prefix = '' + defaults.postfix = '; exit $LastExitCode' + defaults.quote = quotePowerShell +} + +export function setupBash() { 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? - } +} + +function checkShell() { + if (!$.shell) { + throw new Error(`shell is not available: setup guide goes here`) } } @@ -125,6 +134,8 @@ function getStore() { export const $: Shell & Options = new Proxy( function (pieces, ...args) { + checkShell() + if (!Array.isArray(pieces)) { return function (this: any, ...args: any) { const self = this diff --git a/src/globals.ts b/src/globals.ts index d268a25108..68ffc835bc 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -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 @@ -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 diff --git a/test/core.test.js b/test/core.test.js index 0d5166b25c..96d6721722 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -139,6 +139,19 @@ describe('core', () => { assert.equal((await p5).stdout, 'baz') }) + describe('shell', () => { + test('requires $.shell to be specified', async () => { + await within(() => { + $.shell = undefined + assert.throws(() => $`echo foo`, /shell/) + }) + }) + + test('setupPowerShell() requires windows', () => { + assert.throws(() => setupPowerShell(), /on Windows/) + }) + }) + test('`$.sync()` provides synchronous API', () => { const o1 = $.sync`echo foo` const o2 = $({ sync: true })`echo foo` diff --git a/test/win32.test.js b/test/win32.test.js index d018c83468..34cf8dcf02 100644 --- a/test/win32.test.js +++ b/test/win32.test.js @@ -23,8 +23,7 @@ _describe('win32', () => { 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() const p = await $`get-host` assert.match(p.stdout, /PowerShell/) }) @@ -32,8 +31,7 @@ _describe('win32', () => { 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!'/) }) From 2e781ed2dc7eb0169cbf6b1f27ecd44af31cbedf Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sat, 30 Mar 2024 13:36:50 +0300 Subject: [PATCH 2/2] fix: rm win assert from powershell setup --- src/core.ts | 23 +++++++++++------------ test/core.test.js | 14 ++++---------- test/win32.test.js | 2 ++ 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/core.ts b/src/core.ts index e53b2e82a4..aea3be1b58 100644 --- a/src/core.ts +++ b/src/core.ts @@ -103,23 +103,18 @@ export const defaults: Options = { kill, } const isWin = process.platform == 'win32' -try { - setupBash() -} catch (err) {} export function setupPowerShell() { - if (!isWin) throw new Error('PowerShell is only available on Windows') - - defaults.shell = which.sync('powershell.exe') - defaults.prefix = '' - defaults.postfix = '; exit $LastExitCode' - defaults.quote = quotePowerShell + $.shell = which.sync('powershell.exe') + $.prefix = '' + $.postfix = '; exit $LastExitCode' + $.quote = quotePowerShell } export function setupBash() { - defaults.shell = which.sync('bash') - defaults.prefix = 'set -euo pipefail;' - defaults.quote = quote + $.shell = which.sync('bash') + $.prefix = 'set -euo pipefail;' + $.quote = quote } function checkShell() { @@ -190,6 +185,10 @@ export const $: Shell & Options = new Proxy( } ) +try { + setupBash() +} catch (err) {} + type Resolve = (out: ProcessOutput) => void type IO = StdioPipe | StdioNull diff --git a/test/core.test.js b/test/core.test.js index 96d6721722..d7f164474c 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -139,16 +139,10 @@ describe('core', () => { assert.equal((await p5).stdout, 'baz') }) - describe('shell', () => { - test('requires $.shell to be specified', async () => { - await within(() => { - $.shell = undefined - assert.throws(() => $`echo foo`, /shell/) - }) - }) - - test('setupPowerShell() requires windows', () => { - assert.throws(() => setupPowerShell(), /on Windows/) + test('requires $.shell to be specified', async () => { + await within(() => { + $.shell = undefined + assert.throws(() => $`echo foo`, /shell/) }) }) diff --git a/test/win32.test.js b/test/win32.test.js index 34cf8dcf02..5010ba9c11 100644 --- a/test/win32.test.js +++ b/test/win32.test.js @@ -22,8 +22,10 @@ _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 () => { setupPowerShell() + assert.match($.shell, /powershell/i) const p = await $`get-host` assert.match(p.stdout, /PowerShell/) })