Skip to content

Commit

Permalink
Merge pull request #31 from kneat/fixPwshCommandCheck
Browse files Browse the repository at this point in the history
Fix pwsh command check and error handling
  • Loading branch information
radu-v authored Dec 12, 2023
2 parents e2c8654 + 7ab25de commit c442bf6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 37 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [20.x]

steps:
- uses: actions/checkout@v2
Expand All @@ -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
run: |
Set-PSRepository psgallery -InstallationPolicy trusted
Install-Module -Name Pester -RequiredVersion 5.5.0 -Confirm:$false -Force
Invoke-Pester
shell: pwsh
7 changes: 4 additions & 3 deletions Gulp/Gulp.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ 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" {
BeforeEach {
$ErrorActionPreference = 'Continue'
Add-Task "name" @() {
Write-Error 'fail'
}
Expand All @@ -111,7 +112,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
Expand Down
63 changes: 35 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,38 @@ 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) {
const task = () => {
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
.toString()
.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;
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posh-gulp",
"version": "3.1.0",
"version": "3.1.1",
"description": "write gulp tasks in powershell",
"keywords": [
"powershell",
Expand Down

0 comments on commit c442bf6

Please sign in to comment.