-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·236 lines (217 loc) · 8.04 KB
/
index.js
File metadata and controls
executable file
·236 lines (217 loc) · 8.04 KB
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
#!/usr/bin/env node
const log = require('loglevel')
const Vorpal = require('vorpal')
const program = require('commander')
const Ora = require('ora')
const colors = require('colors')
const watch = require('node-watch')
const path = require('path')
const _ = require('underscore')
const commandLineCommands = require('command-line-commands')
const validCommands = [ null, 'sync', 'new', 'run', 'rm', 'remove', 'ls', 'list' ]
const { command, argv } = commandLineCommands(validCommands)
const User = require('./user')
const { notDuplicateEvent, getContent, compareParameters } = require('./util')
const { printUnits, syncUnits, createNewUnit, updateUnit, deleteUnit, runUnit } = require('./units')
const info = require('./package.json')
let watched = []
let watcher = {fileHound: null}
let lastEvent = 0
let unitUpdated = { name: null }
colors.setTheme({
warn: 'yellow',
debug: 'blue',
error: 'red'
})
program
.version(info.version)
// .option('--no-sync', 'Disable sync at start', false) // disabled for capability
.option('-d, --dir [dir]', 'Define path to save units')
.option('-l, --login [login]', 'Set login to sync units from unitcluster')
.option('-k, --key [key]', 'Set API key from your Unitcluster account')
.option('-n, --new [unit]', 'Create new unit', '')
.option('-s, --server [server]', 'Set server to work with')
.option('-o, --loglevel [level]', 'Set level of logs to print [error, warn, info, debug]', 'info')
.option('-r, --remove [unit]', 'Delete unit from unitcluster', '')
.option('-p, --public [boolean]', 'Using with -n defines type of new unit, default true', true)
.parse(process.argv)
const sleep = time => { return new Promise(resolve => setTimeout(() => { resolve() }, time)) }
async function watchUnits (user, unitUpdated, vorpal) {
await sleep(100)
const watcher = watch(user.path, { recursive: true }, async (evt, filePath) => {
if (notDuplicateEvent(filePath, watched, lastEvent)) {
const file = path.parse(filePath)
const unitName = file.dir.replace(user.path + '/', '')
log.debug('%s changed'.debug, file.base)
log.debug('in %s'.debug, file.dir)
const spinner = new Ora('Updating unit ' + colors.cyan(unitName)).start()
let content
try {
content = getContent(filePath)
} catch (err) {
spinner.fail(err)
return vorpal.show()
}
if (content) {
log.debug(content)
// log.info('Changes in "'.debug + unitName + '", updating...'.debug)
let result
try {
result = await updateUnit(unitName, user, content) // here should be object like {code: "let a..."} or {readme:}
} catch (err) {
spinner.fail(err).stop()
return vorpal.show()
}
let key = _.keys(content)[0]
log.debug('[RESULT]'.debug, result)
let resultState
if (key === 'parameters') {
resultState = compareParameters(content.parameters, JSON.parse(result).deployment.parameters)
} else {
resultState = JSON.parse(result)[key] !== content[key]
}
if (resultState) { // Secret params are not belongs to unit but to the deployment
spinner.fail('Unit not saved'.error)
// log.debug('Unit not saved'.error)
} else {
spinner.succeed(`Press ${'[Ctrl + R]'.cyan} or type ${`[run ${unitName}]`.cyan} to run ${unitName.green}`)
// log.info('Press', '[Enter]'.cyan, 'to run', colors.info(unitName))
unitUpdated.name = unitName
vorpal.show()
}
}
}
})
log.info('Watching units in %s for changes...'.cyan, user.path)
return watcher
}
const spinnerWrap = async (startMessage, successMessage, asyncFn, ...params) => {
const spinner = new Ora(startMessage.cyan).start()
let result
try {
result = await asyncFn.apply(null, params)
} catch (err) {
spinner.fail(err.message.debug).stop()
return
}
spinner.succeed(typeof successMessage === 'function' ? successMessage(result).cyan : successMessage.cyan)
}
const setShortcuts = user => process.stdin.on('keypress', (key, param) => {
if (param.name === 'r' && param.ctrl) {
if (unitUpdated.name) {
try {
runUnit(user, unitUpdated.name)
} catch (error) {
log.error(error)
}
}
}
if (param.name === 'd' && param.ctrl) {
console.log('\nBye!')
process.exit()
}
})
const pauseWatcherWrap = async (fn, reopenWatcher, watcher) => {
if (watcher.fileHound) {
if (!watcher.fileHound.isClosed()) watcher.fileHound.close()
}
await fn()
if (reopenWatcher) watcher.fileHound = await reopenWatcher()
}
async function main () {
/**
* Actions:
* Sync
* Login
* New [unit]
* LS
* RM [unit]
* Run [unit]
*/
const handler = async (asyncFn, args, callback) => {
try {
await asyncFn(args)
} catch (err) {
log.error(err.message.error)
}
callback()
}
let vorpal
const deleteUnitAction = args => spinnerWrap('Deleting...', (units) => `Deleted ${units}`, deleteUnit, user, args.units)
const syncUnitsAction = () => pauseWatcherWrap(spinnerWrap.bind(null, 'Updating units', 'Units updated', syncUnits, user, watched),
watchUnits.bind(null, user, unitUpdated, vorpal), watcher) // yeah, complicated
const createUnitAction = (args) => spinnerWrap('Creating unit', (path) => `Unit created at ${path}`,
createNewUnit, user, watched, args.name, args.description, args.isPrivate)
const printUnitsAction = (args) => printUnits(user, args.level)
const updateUserAction = (args) => log.info(args)
const logArgs = args => log.info(args.message)
const printUserAction = () => log.info(user)
const runUnitAction = (args) => runUnit.call(this, user, args.unit) // tryCatchWrap(runUnit.bind(this, user, args.unit))
function startVorpal () {
const vorpal = new Vorpal()
vorpal.command('sync', 'Update units from server')
.action(handler.bind(this, syncUnitsAction))
vorpal.command('login', 'Login to unitcluster')
.action(handler.bind(this, updateUserAction))
vorpal.command('new <name> [description] [isPrivate]', 'Create new unit').alias('create')
.action(handler.bind(this, createUnitAction))
vorpal.command('ls [level]', 'Prints all units and files if level is defined').alias('list')
.action(handler.bind(this, printUnitsAction))
vorpal.command('rm <units...>', 'Delete unit').alias('remove')
.action(handler.bind(this, deleteUnitAction))
vorpal.command('run <unit>', 'Run unit and view logs in console')
.action(handler.bind(vorpal, runUnitAction))
vorpal.command('user', 'Show user information')
.action(handler.bind(this, printUserAction))
vorpal.command('echo [message]', 'Repeat providen message')
.action(handler.bind(this, logArgs))
vorpal.delimiter('unit-cli-$')
return vorpal
}
async function checkCommands (user) {
log.debug(command, argv[0], argv[1], argv[2])
switch (command) {
case 'sync': {
await spinnerWrap('Updating units', 'Units updated', syncUnits, user, watched)
return 1
}
case 'new': {
await createUnitAction({name: argv[0], description: argv[1], isPrivate: argv[2]})
return 1
}
case 'ls':
case 'list': {
printUnitsAction({level: argv[0]})
return 1
}
case 'rm':
case 'remove': {
await deleteUnitAction({units: argv})
return 1
}
case 'run': {
runUnitAction({unit: argv[0]})
return 0
}
case null:
default: return -1
}
}
vorpal = startVorpal()
log.methodFactory = (methodName, logLevel, loggerName) => (...params) => vorpal.log(...params)
log.setDefaultLevel('info')
log.setLevel(program.loglevel)
const user = new User()
await user.init()
setShortcuts(user)
const state = await checkCommands(user)
if (state === -1) {
await syncUnitsAction(user, watched)
vorpal.show()
} else if (state === 1) {
process.exit()
} else if (state === 0) {
vorpal.show()
}
}
main()