From 24dcf3a2953777b70cc54effe2989621a9133886 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Sat, 30 Mar 2024 17:37:33 +0300 Subject: [PATCH] refactor: disable default powershell setup (#757) * refactor: disable default powershell setup * fix: rm win assert from powershell setup --- src/core.ts | 36 +++++++++++++++++++++++------------- src/globals.ts | 2 ++ test/core.test.js | 7 +++++++ test/win32.test.js | 8 ++++---- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/core.ts b/src/core.ts index f005e8510f..aea3be1b58 100644 --- a/src/core.ts +++ b/src/core.ts @@ -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`) } } @@ -125,6 +129,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 @@ -179,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/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..d7f164474c 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -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` diff --git a/test/win32.test.js b/test/win32.test.js index d018c83468..5010ba9c11 100644 --- a/test/win32.test.js +++ b/test/win32.test.js @@ -22,9 +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 () => { - $.shell = which.sync('powershell.exe') - $.quote = quotePowerShell + setupPowerShell() + assert.match($.shell, /powershell/i) const p = await $`get-host` assert.match(p.stdout, /PowerShell/) }) @@ -32,8 +33,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!'/) })