-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.ts
67 lines (60 loc) · 2.11 KB
/
run.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
import {Command, flags} from '@oclif/command'
const {spawn} = require('child_process')
const sendSms = require('../helpers/sms-sender.ts')
const sendEmail = require('../helpers/email-sender.ts')
export class Run extends Command {
static flags = {
help: flags.help({char: 'h'}),
// flag with no value (-s, --sms)
sms: flags.string({char: 's', description: 'Phone number to send command execution status.'}),
// flag with no value (-f, --jobName)
jobName: flags.string({char: 'j', description: 'Executing job name.'}),
email: flags.string({char: 'e', description: 'Email id to send command execution status.'}),
}
static args = [
{
name: 'command',
description: 'Command to be executed.',
required: true},
]
static examples = [
'$ orchestrator run "commandToBeExecuted" --sms=phoneNumber',
]
async run() {
const {args, flags} = this.parse(Run)
const child = spawn(args.command, {
shell: true,
cwd: process.cwd(),
env: process.env,
stdio: ['inherit', 'pipe', 'pipe'],
encoding: 'utf-8',
})
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stdout)
child.on('close', (data: number) => {
// eslint-disable-next-line no-negated-condition
if (data !== 0) {
child.stderr.on('data', (error: any) => this.sendNotification(flags, args, true, error))
} else {
child.stdout.on('data', (data: any) => this.sendNotification(flags, args, false, data))
}
})
// child.stderr.on('data', (error: any) => this.sendNotification(flags, args, true, error))
}
async sendNotification(flags: { sms: any; jobName: any; email: any }, args: { command?: any }, error: boolean, data: { toString: () => any }) {
let subject = flags.jobName || args.command
if (error) {
subject += ' execution failed.'
} else {
subject += ' execution successful.'
}
if (flags.sms) {
const msgId = await sendSms(flags, subject)
this.log(msgId)
}
if (flags.email) {
await sendEmail(flags, subject, data.toString() || subject)
// this.log(msgId)
}
}
}