-
Notifications
You must be signed in to change notification settings - Fork 8
/
shelly.ts
53 lines (49 loc) · 2.01 KB
/
shelly.ts
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
// tslint:disable-next-line:no-reference
/// <reference path="./declarations/child-process-promise.d.ts" />
import * as cp from "child-process-promise";
import * as _ from "lodash";
import * as path from "path";
import * as PluginError from "plugin-error";
import { Transform } from "stream";
import * as through from "through2";
// Consts
const PLUGIN_NAME = "gulp-dotnet-cli";
// Plugin level function(dealing with files)
/*
* Wrapper over through2 to execute commands with promises
* @param {string} command the root command to run usually dotnet
* @param {string|Array<string>} noun The action to take build, push, pack, etc
* @param {Array<String>} args the arguments to pass such as ['--verbosity', 'normal']
* @param {boolean} echo Should the command be printed to the console?
* @param {boolean} optional default false, if true sets the current working directory to the files directory.
*/
// tslint:disable-next-line:max-line-length
export default function shelly(command: string, noun: string|string[], args: any[], echo: boolean = false, setCwd: boolean = false) {
if (!command) {
throw new PluginError(PLUGIN_NAME, "command not passed");
}
if (!noun) {
throw new PluginError(PLUGIN_NAME, "noun not passed");
}
if (!_.isArray(args)) {
throw new PluginError(PLUGIN_NAME, "Arguments has to be an array");
}
// Creating a stream through which each file will pass
return through.obj((file, enc, cb) => {
if (!file || !file.path) {
// return empty file
return cb(null, file);
}
const calculatedArgs = _.concat([], noun, file.path, args);
if (echo) {
console.log(`${command} ${calculatedArgs.join(" ")}`);
}
const options = { stdio: "inherit" } as any;
if (setCwd) {
options.cwd = path.dirname(file.path);
}
cp.spawn(command, calculatedArgs, options)
.then((a: any) => cb(null, file))
.catch((ex: any) => cb(ex, file));
});
}