-
Notifications
You must be signed in to change notification settings - Fork 3
/
commands.js
69 lines (62 loc) · 1.49 KB
/
commands.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
const _ = require('lodash')
const uuid = require('short-uuid')
const memCache = require('./mem_cache')
const { MESSAGE_TYPES, sendMessage } = require('../ws')
const { separateEnvsFromString } = require('./envs')
const COMMAND_DEFAULT_DATA = {
logs: [],
isRun: false,
}
const getCommandById = (configId, taskId) =>
_.find(
_.find(_.get(memCache.get('rc-snapshot'), 'configs', []), {
id: configId,
}).commands,
{ id: taskId }
) || {}
/**
* @typedef {Object} Command
* @property {string} task
* @property {Object} envs
* @property {string} name
*/
const createCommand = (configId, name, commandData, type) => {
const commonData = separateEnvsFromString(commandData)
return {
...COMMAND_DEFAULT_DATA,
configId,
task: commonData.string,
envs: commonData.envs,
name: name,
id: `c${uuid.generate()}`,
type,
}
}
const updateCommand = (command, { isRun, isLaunching, isStopping, log }) => {
const message = {
name: command.name,
isRun: command.isRun,
type: command.type,
id: command.id,
}
if (!_.isUndefined(isLaunching)) {
message.isLaunching = isLaunching
}
if (!_.isUndefined(isStopping)) {
message.isStopping = isStopping
}
if (!_.isUndefined(isRun)) {
message.isRun = command.isRun = isRun
}
if (!_.isUndefined(log)) {
command.logs.push(log)
message.log = log
}
sendMessage(MESSAGE_TYPES.LOG, message)
}
module.exports = {
COMMAND_DEFAULT_DATA,
getCommandById,
createCommand,
updateCommand,
}