Commit 18e8e13

Grigorii K. Shartsev <me@shgk.me>
2024-04-30 16:37:05
feat: add `usePwsh` helper for PowerShell v7+ (#790)
* feat: add `usePwsh` for PowerShell v7+ Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * test: add usePwsh smoke test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * test: add `usePwsh` unit test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * fixup! test: add usePwsh smoke test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * fixup! test: add `usePwsh` unit test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * fixup! test: add `usePwsh` unit test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> * fixup! test: add usePwsh smoke test Signed-off-by: Grigorii K. Shartsev <me@shgk.me> --------- Signed-off-by: Grigorii K. Shartsev <me@shgk.me>
1 parent eda722c
src/core.ts
@@ -117,6 +117,13 @@ export function usePowerShell() {
   $.quote = quotePowerShell
 }
 
+export function usePwsh() {
+  $.shell = which.sync('pwsh')
+  $.prefix = ''
+  $.postfix = '; exit $LastExitCode'
+  $.quote = quotePowerShell
+}
+
 export function useBash() {
   $.shell = which.sync('bash')
   $.prefix = 'set -euo pipefail;'
src/globals.ts
@@ -42,6 +42,7 @@ declare global {
   var quotePowerShell: typeof _.quotePowerShell
   var retry: typeof _.retry
   var usePowerShell: typeof _.usePowerShell
+  var usePwsh: typeof _.usePwsh
   var useBash: typeof _.useBash
   var sleep: typeof _.sleep
   var spinner: typeof _.spinner
test/smoke/win32.test.js
@@ -18,6 +18,8 @@ import '../../build/globals.js'
 
 const _describe = process.platform === 'win32' ? describe : describe.skip
 
+const _testPwsh = which.sync('pwsh', { nothrow: true }) ? test : test.skip
+
 _describe('win32', () => {
   test('should work with windows-specific commands', async () => {
     const p = await $`echo $0` // Bash is first by default.
@@ -38,4 +40,13 @@ _describe('win32', () => {
       assert.match(p.stdout, /Windows 'rulez!'/)
     })
   })
+
+  _testPwsh('should work with pwsh when it is available', async () => {
+    await within(async () => {
+      usePwsh()
+      assert.match($.shell, /pwsh/i)
+      const p = await $`echo 'Hello,' && echo ${`new 'PowerShell'!`}`
+      assert.match(p.stdout, /Hello,\s+new 'PowerShell'!/)
+    })
+  })
 })
test/core.test.js
@@ -583,4 +583,16 @@ describe('core', () => {
     }
     assert.ok(ok, 'Expected failure!')
   })
+
+  test('usePwsh() sets proper defaults', () => {
+    const originalWhichSync = which.sync
+    which.sync = (bin) => (bin === 'pwsh' ? 'pwsh' : originalWhichSync(bin))
+    usePwsh()
+    assert.equal($.shell, 'pwsh')
+    assert.equal($.prefix, '')
+    assert.equal($.postfix, '; exit $LastExitCode')
+    assert.equal($.quote, quotePowerShell)
+    which.sync = originalWhichSync
+    useBash()
+  })
 })
test/index.test.js
@@ -22,6 +22,7 @@ import {
   cd,
   syncProcessCwd,
   usePowerShell,
+  usePwsh,
   useBash,
   kill,
   ProcessOutput,
@@ -69,6 +70,7 @@ describe('index', () => {
     assert(defaults)
     assert(within)
     assert(usePowerShell)
+    assert(usePwsh)
     assert(useBash)
 
     // goods