-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
71 lines (59 loc) · 2.68 KB
/
client.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
const {getDiff, every, detectLanguage, joinPath, run, calculateHash, coerceArray, which, mkdirp, dirname, fs, notSoSafeEval} = require('./helpers')
const {generateClientConfig} = require('./config')
module.exports = queryUrl => {
const URL = new (require('url').URL)(queryUrl)
let [_,token] = split('/', URL.pathname)
let tags = process.argv[3] ? split(',', process.argv[3]) : null
let id = process.argv[4]
require('./ws').connect('ws://'+URL.host+'/Sync', {
connect: emit => {},
'config:ask': emit => {
if (tags) emit({config: {id, platform, token, tags, hash: calculateHash(conf)}})
},
tags: async (emit, serverTags) => {
if (isEmpty(serverTags)) serverTags = ['default']
if (!tags) await generateClientConfig(serverTags, queryUrl).then(x => {
tags = x.tags
id = x.id
})
emit({config: {id, platform, token, tags, hash: calculateHash(conf)}})
},
config: (emit, update) => {
const prevConf = clone(conf)
conf = update
const log = (service, message, json) => {
// console.log({service, message, json})
emit({log: {message, service, json, id}})
}
const inSequence = x =>
inSequence.Q = inSequence.Q.then(x)
inSequence.Q = Promise.resolve()
mapObjIndexed(async (s, service) => {
if (calculateHash(propOr({}, service, prevConf)) == calculateHash(s)) return
for (const f of values(s.files)) {
mkdirp.sync(dirname(f.path))
const prev = tryCatch(fs.readFileSync, () => '')(f.path, 'utf8')
if (calculateHash(prev) == calculateHash(f.content)) continue
try {
f.content = replace(/{{{(.*?)}}}/gim, (x,code) => notSoSafeEval(code), f.content)
fs.writeFileSync(f.path, f.content)
log(service, 'file:updated '+f.path, {status: 'inprogress', diff: getDiff(prev, f.content)})
} catch(e) {
log(service, 'file:updated '+f.path, {status: 'error', error: e.message, diff: getDiff(prev, f.content)})
return
}
}
await Promise.all(values(mapObjIndexed(async (install, check) => {
if (!install.check)
return which(check).catch(() => inSequence(() => run(install, x => log(service, install, x))))
const {status} = await run(install.check, () => {}, true)
if (status == 'error') return inSequence(() => run(install.install, x => log(service, install.install, x)))
}, s.install || {})))
if (s.command) {
// run(split(/;|\n/, replace(/\\\n/, ' ', s.command)), x => log(service, s.command, x))
run(s.command, x => log(service, s.command, x))
}
}, conf)
}
})
}