-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
119 lines (100 loc) · 3.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
const run = require('child_process').spawnSync;
const spawn = require('child_process').spawn;
const colors = require('ansi-colors');
const log = require('fancy-log');
const args = require('yargs').argv;
const commandExists = require('command-exists').sync;
const switches = [
'-NoProfile',
'-NoLogo',
'-NonInteractive',
'-File'
];
const powershellCommand = 'powershell';
const pwshCommand = 'pwsh';
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 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 = getTasksFromResult(result.stdout.toString());
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);
taskProcess.stdout.on('data', data => {
data
.toString()
.split(/\r?\n/)
.filter(Boolean)
.map(lineAsJson)
.forEach(l => logForLevel(l, taskLabel, debugOrVerbose));
});
return taskProcess;
};
task.displayName = `${key} powershell task`;
gulp.task(key, gulp.series(tasks[key], task));
});
}
};
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)
} catch {
return {
level: 'unknown',
message: 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);
}
}