Commit 24dcf3a

Anton Golub <antongolub@antongolub.com>
2024-03-30 15:37:33
refactor: disable default powershell setup (#757)
* refactor: disable default powershell setup * fix: rm win assert from powershell setup
1 parent fb9554f
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<Shell & Options>(
   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<Shell & Options>(
   }
 )
 
+try {
+  setupBash()
+} catch (err) {}
+
 type Resolve = (out: ProcessOutput) => void
 type IO = StdioPipe | StdioNull
 
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
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`
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!'/)
     })