From 9aaf7ff18c5138a2a5cf424b027ba78bc820e523 Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 09:13:25 +0000
Subject: [PATCH 01/10] Fix pwsh command check and error handling

---
 index.js          | 63 ++++++++++++++++++++++++++---------------------
 package-lock.json |  4 +--
 package.json      |  2 +-
 3 files changed, 38 insertions(+), 31 deletions(-)

diff --git a/index.js b/index.js
index a397c42..b6f8928 100644
--- a/index.js
+++ b/index.js
@@ -12,18 +12,23 @@ const switches = [
    '-NoLogo',
    '-NonInteractive',
    '-File'
-]; 
+];
 
 module.exports = function (gulp, file, options = { runOnWindowsPowershell: false }) {
-
    log('Importing Tasks', colors.magenta(file));
 
-   const powershellCommand = !options.runOnWindowsPowershell && commandExists('pwsh') ? 'pwsh' : 'powershell';
+   const powershellCommand = !options.runOnWindowsPowershell ? 'pwsh' : 'powershell';
+
+   if (!commandExists(powershellCommand)) {
+      console.error(`Command ${powershellCommand} not found. Please make sure it is installed and accessible through the PATH envvar.`);
+      process.exit(1);
+   }
 
    const result = run(powershellCommand, switches.concat(file));
+   const debugOrVerbose = (args.debug || args.verbose);
 
-   if (result.stderr.length > 0)
-      log.error(result.stderr.toString());
+   if (result.error || result.stderr && result.stderr.length > 0)
+      log.error(result.error || result.stderr.toString());
    else {
       const tasks = JSON.parse(result.stdout);
       Object.keys(tasks).forEach(function (key) {
@@ -31,7 +36,6 @@ module.exports = function (gulp, file, options = { runOnWindowsPowershell: false
             const execSwitches = switches.concat(file, key, process.argv);
             const taskProcess = spawn(powershellCommand, execSwitches, { stdio: ['inherit', 'pipe', 'inherit'] });
             const taskLabel = colors.cyan(key);
-            const debugOrVerbose = (args.debug || args.verbose);
 
             taskProcess.stdout.on('data', data => {
                data
@@ -39,28 +43,7 @@ module.exports = function (gulp, file, options = { runOnWindowsPowershell: false
                   .split(/\r?\n/)
                   .filter(l => l !== '')
                   .map(lineAsJson)
-                  .forEach(l => {
-                     switch (l.level)
-                     {
-                        case 'debug':
-                           debugOrVerbose && log.info(taskLabel, l.message);
-                           break;
-                        case 'verbose':
-                           args.verbose && log.info(taskLabel, l.message);
-                           break;
-                        case 'information':
-                           log.info(taskLabel, l.message);
-                           break;
-                        case 'warning':
-                           log.warn(taskLabel, l.message);
-                           break;
-                        case 'error':
-                           log.error(taskLabel, l.message);
-                           break;
-                        default:
-                           log(taskLabel, l.message);
-                     }
-                  });
+                  .forEach(l => logForLevel(l, taskLabel, debugOrVerbose));
             });
 
             return taskProcess;
@@ -82,3 +65,27 @@ function lineAsJson(line) {
       };
    }
 }
+
+function logForLevel(l, taskLabel, debugOrVerbose) {
+   switch (l.level)
+   {
+      case 'debug':
+         debugOrVerbose && log.info(taskLabel, l.message);
+         break;
+      case 'verbose':
+         args.verbose && log.info(taskLabel, l.message);
+         break;
+      case 'information':
+         log.info(taskLabel, l.message);
+         break;
+      case 'warning':
+         // this should use log.warn(), but for some reason when called via gulp and level is warning, stderr seems to be suppressed
+         log.info(taskLabel, l.message);
+         break;
+      case 'error':
+         log.error(taskLabel, l.message);
+         break;
+      default:
+         log(taskLabel, l.message);
+   }
+}
\ No newline at end of file
diff --git a/package-lock.json b/package-lock.json
index d89d6fa..1fd8c1a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
 {
   "name": "posh-gulp",
-  "version": "3.1.0",
+  "version": "3.1.1",
   "lockfileVersion": 3,
   "requires": true,
   "packages": {
     "": {
       "name": "posh-gulp",
-      "version": "3.1.0",
+      "version": "3.1.1",
       "license": "ISC",
       "dependencies": {
         "ansi-colors": "^4.1.3",
diff --git a/package.json b/package.json
index 3488b45..c5534c4 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "posh-gulp",
-  "version": "3.1.0",
+  "version": "3.1.1",
   "description": "write gulp tasks in powershell",
   "keywords": [
     "powershell",

From 5e7c441c231868ea2204f8a765aaca7611b0182d Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 13:09:58 +0000
Subject: [PATCH 02/10] Set node 20.x in the pipeline

---
 .github/workflows/node.js.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml
index 5b4905f..d9784de 100644
--- a/.github/workflows/node.js.yml
+++ b/.github/workflows/node.js.yml
@@ -9,7 +9,7 @@ jobs:
 
     strategy:
       matrix:
-        node-version: [10.x, 12.x, 14.x]
+        node-version: [20.x]
 
     steps:
     - uses: actions/checkout@v2

From d5f5b1d35517ee151db73eedf28ec420064e7431 Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 13:14:10 +0000
Subject: [PATCH 03/10] Try enable pester on the pipeline

---
 .github/workflows/node.js.yml | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml
index d9784de..2da8c54 100644
--- a/.github/workflows/node.js.yml
+++ b/.github/workflows/node.js.yml
@@ -18,5 +18,9 @@ jobs:
       with:
         node-version: ${{ matrix.node-version }}
     - run: npm ci
-    - run: npm run build --if-present
-    - run: npm test
+    - name: pester
+      uses: Amadevus/pwsh-script@v2.0.3
+      with:
+        script: |
+          Install-Module -Name Pester -Force
+          Invoke-Pester -EnableExit

From a785641c2b78eb7205267aaec6292f2b480e915e Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 13:52:19 +0000
Subject: [PATCH 04/10] Remove deprecated Pester arg

---
 .github/workflows/node.js.yml | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml
index 2da8c54..b1732ea 100644
--- a/.github/workflows/node.js.yml
+++ b/.github/workflows/node.js.yml
@@ -22,5 +22,5 @@ jobs:
       uses: Amadevus/pwsh-script@v2.0.3
       with:
         script: |
-          Install-Module -Name Pester -Force
-          Invoke-Pester -EnableExit
+          if (!(Get-Module -ListAvailable -Name Pester)) { Install-Module -Name Pester -Scope CurrentUser }
+          Invoke-Pester

From 059819e85edca5df5b28c7532855b9c0719e1df1 Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 13:52:30 +0000
Subject: [PATCH 05/10] Fix a test

---
 Gulp/Gulp.tests.ps1 | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/Gulp/Gulp.tests.ps1 b/Gulp/Gulp.tests.ps1
index 3e72aee..885a65d 100644
--- a/Gulp/Gulp.tests.ps1
+++ b/Gulp/Gulp.tests.ps1
@@ -95,8 +95,8 @@ Describe "Publish-Tasks 'name'" {
             }
             $result = Publish-Tasks 'name'
         }
-        It "result should be like ""*\Gulp""" {
-            ($result | ConvertFrom-Json).Message | Should -BeLike "*\Gulp"
+        It "result should be like ""*\Gulp"" or ""*/Gulp""" {
+            ($result | ConvertFrom-Json).Message | Should -Match "[*\\/]*Gulp"
         }
     }
     Context "'name' writes 'fail' error" {
@@ -111,7 +111,7 @@ Describe "Publish-Tasks 'name'" {
             ) > $null) 3>&1
         }
         It "result should be 'fail'" {
-            $result | Should -Match """fail"""
+            $result | Should -Match "fail"
         }
         It "error stream should be null" {
             $errors | Should -Be $null

From f6334c2b7945947ef128383eb2a3c9f116ffafe2 Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 14:21:11 +0000
Subject: [PATCH 06/10] Use pwsh as the shell for running tests

---
 .github/workflows/node.js.yml | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml
index b1732ea..adba4f2 100644
--- a/.github/workflows/node.js.yml
+++ b/.github/workflows/node.js.yml
@@ -19,8 +19,8 @@ jobs:
         node-version: ${{ matrix.node-version }}
     - run: npm ci
     - name: pester
-      uses: Amadevus/pwsh-script@v2.0.3
-      with:
-        script: |
-          if (!(Get-Module -ListAvailable -Name Pester)) { Install-Module -Name Pester -Scope CurrentUser }
-          Invoke-Pester
+      run: |
+        Set-PSRepository psgallery -InstallationPolicy trusted
+        Install-Module -Name Pester -RequiredVersion 5.5.0 -Confirm:$false -Force
+        Invoke-Pester
+      shell: pwsh

From b9ddd810213efa2fc332817a4dd40ddf3666c6d9 Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 15:42:21 +0000
Subject: [PATCH 07/10] Try..catch around Write-Error in the test

---
 Gulp/Gulp.tests.ps1 | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/Gulp/Gulp.tests.ps1 b/Gulp/Gulp.tests.ps1
index 885a65d..3ead715 100644
--- a/Gulp/Gulp.tests.ps1
+++ b/Gulp/Gulp.tests.ps1
@@ -102,7 +102,11 @@ Describe "Publish-Tasks 'name'" {
     Context "'name' writes 'fail' error" {
         BeforeEach {
             Add-Task "name" @() {
-                Write-Error 'fail'
+                try {
+                    Write-Error 'fail'
+                }
+                catch {
+                }
             }
             $warnings = $((
                 $errors = $((

From 2735bfad2a018be66429875a54147716f74a060b Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 15:47:23 +0000
Subject: [PATCH 08/10] Deserialise test result before assertion

---
 Gulp/Gulp.tests.ps1 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Gulp/Gulp.tests.ps1 b/Gulp/Gulp.tests.ps1
index 3ead715..603c4bc 100644
--- a/Gulp/Gulp.tests.ps1
+++ b/Gulp/Gulp.tests.ps1
@@ -115,7 +115,7 @@ Describe "Publish-Tasks 'name'" {
             ) > $null) 3>&1
         }
         It "result should be 'fail'" {
-            $result | Should -Match "fail"
+            ($result | ConvertFrom-Json).Message | Should -Be "fail"
         }
         It "error stream should be null" {
             $errors | Should -Be $null

From 102915cf31a0f6be34d2f8c4f83f37d9050dab5d Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 16:16:27 +0000
Subject: [PATCH 09/10] Well, that didn't work

---
 Gulp/Gulp.tests.ps1 | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/Gulp/Gulp.tests.ps1 b/Gulp/Gulp.tests.ps1
index 603c4bc..a849b6b 100644
--- a/Gulp/Gulp.tests.ps1
+++ b/Gulp/Gulp.tests.ps1
@@ -102,11 +102,7 @@ Describe "Publish-Tasks 'name'" {
     Context "'name' writes 'fail' error" {
         BeforeEach {
             Add-Task "name" @() {
-                try {
-                    Write-Error 'fail'
-                }
-                catch {
-                }
+                Write-Error 'fail'
             }
             $warnings = $((
                 $errors = $((

From 7ab25de651eace09a01d9847e1b5be71b43bae5d Mon Sep 17 00:00:00 2001
From: Radu Vasilascu <radu.vasilascu@kneat.com>
Date: Tue, 12 Dec 2023 16:37:42 +0000
Subject: [PATCH 10/10] Change $ErrorActionPreference for the Write-Error test

---
 Gulp/Gulp.tests.ps1 | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Gulp/Gulp.tests.ps1 b/Gulp/Gulp.tests.ps1
index a849b6b..bce74f4 100644
--- a/Gulp/Gulp.tests.ps1
+++ b/Gulp/Gulp.tests.ps1
@@ -101,6 +101,7 @@ Describe "Publish-Tasks 'name'" {
     }
     Context "'name' writes 'fail' error" {
         BeforeEach {
+            $ErrorActionPreference = 'Continue'
             Add-Task "name" @() {
                 Write-Error 'fail'
             }