Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.js #33

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,28 @@ const getPowershellCommand = (options) => {
if (options.runOnWindowsPowershell || !commandExists(pwshCommand)) {
return powershellCommand;
}

return pwshCommand;
};

module.exports = 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);
}

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);
Expand All @@ -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));
});
Expand All @@ -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)
Expand Down Expand Up @@ -99,4 +116,4 @@ function logForLevel(l, taskLabel, debugOrVerbose) {
default:
log(taskLabel, l.message);
}
}
}
Loading