This repository was archived by the owner on Mar 14, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathShipit.js
315 lines (276 loc) · 7.87 KB
/
Shipit.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* eslint-disable no-console */
import { ConnectionPool, exec } from 'ssh-pool'
import LineWrapper from 'stream-line-wrapper'
import Orchestrator from 'orchestrator'
import chalk from 'chalk'
import prettyTime from 'pretty-hrtime'
/**
* An ExecResult returned when a command is executed with success.
* @typedef {object} ExecResult
* @property {Buffer} stdout
* @property {Buffer} stderr
* @property {ChildProcess} child
*/
/**
* An ExecResult returned when a command is executed with success.
* @typedef {object} MultipleExecResult
* @property {Buffer} stdout
* @property {Buffer} stderr
* @property {ChildProcess[]} children
*/
/**
* An ExecError returned when a command is executed with an error.
* @typedef {Error} ExecError
* @property {Buffer} stdout
* @property {Buffer} stderr
* @property {ChildProcess} child
*/
/**
* Format orchestrator error.
*
* @param {Error} e
* @returns {Error}
*/
function formatError(e) {
if (!e.err) {
return e.message
}
// PluginError
if (typeof e.err.showStack === 'boolean') {
return e.err.toString()
}
// normal error
if (e.err.stack) {
return e.err.stack
}
// unknown (string, number, etc.)
return new Error(String(e.err)).stack
}
class Shipit extends Orchestrator {
constructor(options) {
super()
const defaultOptions = {
stdout: process.stdout,
stderr: process.stderr,
log: console.log.bind(console),
}
this.config = {}
this.globalConfig = {}
this.options = { ...defaultOptions, ...options }
this.environment = options.environment
this.initializeEvents()
if (this.options.stdout === process.stdout)
process.stdout.setMaxListeners(100)
if (this.options.stderr === process.stderr)
process.stderr.setMaxListeners(100)
}
/**
* Initialize the `shipit`.
*
* @returns {Shipit} for chaining
*/
initialize() {
if (!this.globalConfig[this.environment])
throw new Error(`Environment '${this.environment}' not found in config`)
this.emit('init')
return this.initSshPool()
}
/**
* Initialize events.
*/
initializeEvents() {
this.on('task_start', e => {
// Specific log for noop functions.
if (this.tasks[e.task].fn.toString() === 'function () {}') return
this.log('\nRunning', `'${chalk.cyan(e.task)}' task...`)
})
this.on('task_stop', e => {
const task = this.tasks[e.task]
// Specific log for noop functions.
if (task.fn.toString() === 'function () {}') {
this.log(
'Finished',
`'${chalk.cyan(e.task)}'`,
chalk.cyan(`[ ${task.dep.join(', ')} ]`),
)
return
}
const time = prettyTime(e.hrDuration)
this.log(
'Finished',
`'${chalk.cyan(e.task)}'`,
'after',
chalk.magenta(time),
)
})
this.on('task_err', e => {
const msg = formatError(e)
const time = prettyTime(e.hrDuration)
this.log(
`'${chalk.cyan(e.task)}'`,
chalk.red('errored after'),
chalk.magenta(time),
)
this.log(msg)
})
this.on('task_not_found', err => {
this.log(chalk.red(`Task '${err.task}' is not in your shipitfile`))
this.log(
'Please check the documentation for proper shipitfile formatting',
)
})
}
/**
* Initialize SSH connections.
*
* @returns {Shipit} for chaining
*/
initSshPool() {
if (!this.config.servers) throw new Error('Servers not filled')
const servers = Array.isArray(this.config.servers)
? this.config.servers
: [this.config.servers]
const options = {
...this.options,
key: this.config.key,
strict: this.config.strict,
verbosityLevel: (this.config.verboseSSHLevel === undefined) ? 0 : this.config.verboseSSHLevel
}
this.pool = new ConnectionPool(servers, options)
return this
}
/**
* Initialize shipit configuration.
*
* @param {object} config
* @returns {Shipit} for chaining
*/
initConfig(config = {}) {
this.globalConfig = config
this.config = {
...config.default,
...config[this.environment],
}
return this
}
/**
* Run a command locally.
*
* @param {string} command
* @param {object} options
* @returns {ChildObject}
*/
local(command, { stdout, stderr, ...cmdOptions } = {}) {
this.log('Running "%s" on local.', command)
const prefix = '@ '
return exec(command, cmdOptions, child => {
if (this.options.stdout)
child.stdout.pipe(new LineWrapper({ prefix })).pipe(this.options.stdout)
if (this.options.stderr)
child.stderr.pipe(new LineWrapper({ prefix })).pipe(this.options.stderr)
})
}
/**
* Run a command remotely.
*
* @param {string} command
* @returns {ExecResult}
* @throws {ExecError}
*/
async remote(command, options) {
return this.pool.run(command, options)
}
/**
* Copy from local to remote or vice versa.
*
* @param {string} src
* @param {string} dest
* @returns {ExecResult|MultipleExecResult}
* @throws {ExecError}
*/
async remoteCopy(src, dest, options) {
const defaultOptions = {
ignores: this.config && this.config.ignores ? this.config.ignores : [],
rsync: this.config && this.config.rsync ? this.config.rsync : [],
}
const copyOptions = { ...defaultOptions, ...options }
return this.pool.copy(src, dest, copyOptions)
}
/**
* Run a copy from the local to the remote using rsync.
* All exec options are also available.
*
* @see https://nodejs.org/dist/latest-v8.x/docs/api/child_process.html#child_process_child_process_exec_command_options_callback
*
* @param {string} src
* @param {string} dest
* @param {object} [options] Options
* @param {string[]} [options.ignores] Specify a list of files to ignore.
* @param {string[]|string} [options.rsync] Specify a set of rsync arguments.
* @returns {MultipleExecResult}
* @throws {ExecError}
*/
async copyToRemote(src, dest, options) {
const defaultOptions = {
ignores: this.config && this.config.ignores ? this.config.ignores : [],
rsync: this.config && this.config.rsync ? this.config.rsync : [],
}
const copyOptions = { ...defaultOptions, ...options }
return this.pool.copyToRemote(src, dest, copyOptions)
}
/**
* Run a copy from the remote to the local using rsync.
* All exec options are also available.
*
* @see https://nodejs.org/dist/latest-v8.x/docs/api/child_process.html#child_process_child_process_exec_command_options_callback
* @param {string} src Source
* @param {string} dest Destination
* @param {object} [options] Options
* @param {string[]} [options.ignores] Specify a list of files to ignore.
* @param {string[]|string} [options.rsync] Specify a set of rsync arguments.
* @returns {MultipleExecResult}
* @throws {ExecError}
*/
async copyFromRemote(src, dest, options) {
const defaultOptions = {
ignores: this.config && this.config.ignores ? this.config.ignores : [],
rsync: this.config && this.config.rsync ? this.config.rsync : [],
}
const copyOptions = { ...defaultOptions, ...options }
return this.pool.copyFromRemote(src, dest, copyOptions)
}
/**
* Log.
*
* @see console.log
*/
log(...args) {
this.options.log(...args)
}
/**
* Create a new blocking task.
*
* @see shipit.task
*/
blTask(name, ...rest) {
this.task(name, ...rest)
const task = this.tasks[name]
task.blocking = true
return task
}
/**
* Test if we are ready to run a task.
* Implement blocking task.
*/
_readyToRunTask(...args) {
if (
Object.keys(this.tasks).some(key => {
const task = this.tasks[key]
return task.running === true && task.blocking === true
})
)
return false
return super._readyToRunTask(...args) // eslint-disable-line no-underscore-dangle
}
}
export default Shipit