From 77dd7933e2a5a4ec2a5f9de7a2868af3a7a6d0e9 Mon Sep 17 00:00:00 2001 From: Radu Vasilascu Date: Wed, 3 Jan 2024 12:22:52 +0000 Subject: [PATCH 1/2] Update index.js In some cases the powershell script might output some warnings before returning the JSON containing the exported tasks. When this happens, the script will fail due to it not being able to parse the output. This change adds a try..catch around parsing and outputs the non-JSON string. --- index.js | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 2fa18d2..1b946cd 100644 --- a/index.js +++ b/index.js @@ -21,13 +21,13 @@ const getPowershellCommand = (options) => { if (options.runOnWindowsPowershell || !commandExists(pwshCommand)) { return powershellCommand; } - + return pwshCommand; }; -module.exports = function (gulp, file, options = { runOnWindowsPowershell: false }) { +module.exports = async function (gulp, file, options = { runOnWindowsPowershell: false }) { const powershellCommand = getPowershellCommand(options); - + if (!commandExists(powershellCommand)) { console.error(`Command ${powershellCommand} not found. Please make sure it is installed and accessible through the PATH envvar.`); process.exit(1); @@ -35,13 +35,14 @@ module.exports = function (gulp, file, options = { runOnWindowsPowershell: false log(`Importing Tasks using ${powershellCommand}`, colors.magenta(file)); - const result = run(powershellCommand, switches.concat(file)); const debugOrVerbose = (args.debug || args.verbose); + const result = run(powershellCommand, switches.concat(file)); if (result.error || result.stderr && result.stderr.length > 0) log.error(result.error || result.stderr.toString()); else { - const tasks = JSON.parse(result.stdout); + const tasks = getTasksFromResult(result.stdout.toString()); + Object.keys(tasks).forEach(function (key) { const task = () => { const execSwitches = switches.concat(file, key, process.argv); @@ -52,7 +53,7 @@ module.exports = function (gulp, file, options = { runOnWindowsPowershell: false data .toString() .split(/\r?\n/) - .filter(l => l !== '') + .filter(Boolean) .map(lineAsJson) .forEach(l => logForLevel(l, taskLabel, debugOrVerbose)); }); @@ -66,6 +67,22 @@ module.exports = function (gulp, file, options = { runOnWindowsPowershell: false } }; +function getTasksFromResult(result) { + const lines = result.toString().split(/\r?\n/).filter(Boolean); + + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + try { + const tasks = JSON.parse(line); + return tasks; + } catch { + log.info(line); + } + } + + return []; +} + function lineAsJson(line) { try{ return JSON.parse(line) @@ -99,4 +116,4 @@ function logForLevel(l, taskLabel, debugOrVerbose) { default: log(taskLabel, l.message); } -} \ No newline at end of file +} From 521baafec7c23aa8d0c52e4dba4b5b1909048328 Mon Sep 17 00:00:00 2001 From: Radu Vasilascu Date: Wed, 3 Jan 2024 12:24:55 +0000 Subject: [PATCH 2/2] Update index.js Remove unused `async` --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 1b946cd..fcdbece 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ const getPowershellCommand = (options) => { return pwshCommand; }; -module.exports = async function (gulp, file, options = { runOnWindowsPowershell: false }) { +module.exports = function (gulp, file, options = { runOnWindowsPowershell: false }) { const powershellCommand = getPowershellCommand(options); if (!commandExists(powershellCommand)) {