-
Notifications
You must be signed in to change notification settings - Fork 62
/
index.ts
136 lines (115 loc) · 3.13 KB
/
index.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import chalk from 'chalk'
import { spawn } from 'child_process'
import fancyLog from 'fancy-log'
import template from 'lodash.template'
import * as path from 'path'
import PluginError from 'plugin-error'
import { obj as throughObj } from 'through2'
import Vinyl from 'vinyl'
const PLUGIN_NAME = 'gulp-shell'
interface Options {
cwd?: string
env?: NodeJS.ProcessEnv
shell?: true | string
quiet?: boolean
verbose?: boolean
ignoreErrors?: boolean
errorMessage?: string
templateData?: object
}
const normalizeCommands = (commands: string | string[]): string[] => {
if (typeof commands === 'string') {
commands = [commands]
}
if (!Array.isArray(commands)) {
throw new PluginError(PLUGIN_NAME, 'Missing commands')
}
return commands
}
const normalizeOptions = (options: Options = {}): Required<Options> => {
const pathToBin = path.join(process.cwd(), 'node_modules', '.bin')
/* istanbul ignore next */
const pathName = process.platform === 'win32' ? 'Path' : 'PATH'
const newPath = pathToBin + path.delimiter + process.env[pathName]
const env = {
...process.env,
[pathName]: newPath,
...options.env
}
return {
cwd: process.cwd(),
env,
shell: true,
quiet: false,
verbose: false,
ignoreErrors: false,
errorMessage:
'Command `<%= command %>` failed with exit code <%= error.code %>',
templateData: {},
...options
}
}
const runCommand = (
command: string,
options: Required<Options>,
file: Vinyl | null
): Promise<void> => {
const context = { file, ...options.templateData }
command = template(command)(context)
if (options.verbose) {
fancyLog(`${PLUGIN_NAME}:`, chalk.cyan(command))
}
const child = spawn(command, {
env: options.env,
cwd: template(options.cwd)(context),
shell: options.shell,
stdio: options.quiet ? 'ignore' : 'inherit'
})
return new Promise((resolve, reject) => {
child.on('exit', code => {
if (code === 0 || options.ignoreErrors) {
return resolve()
}
const context = {
command,
file,
error: { code },
...options.templateData
}
const message = template(options.errorMessage)(context)
reject(new PluginError(PLUGIN_NAME, message))
})
})
}
const runCommands = async (
commands: string[],
options: Required<Options>,
file: Vinyl | null
): Promise<void> => {
for (const command of commands) {
await runCommand(command, options, file)
}
}
const shell = (
commands: string | string[],
options?: Options
): NodeJS.ReadWriteStream => {
const normalizedCommands = normalizeCommands(commands)
const normalizedOptions = normalizeOptions(options)
const stream = throughObj(function(file, _encoding, done) {
runCommands(normalizedCommands, normalizedOptions, file)
.then(() => {
this.push(file)
})
.catch(error => {
this.emit('error', error)
})
.finally(done)
})
stream.resume()
return stream
}
shell.task = (commands: string | string[], options?: Options) => (): Promise<
void
> => runCommands(normalizeCommands(commands), normalizeOptions(options), null)
export = shell