-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpublisher.js
82 lines (72 loc) · 2.03 KB
/
publisher.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
import request from 'request'
import * as _ from 'lodash'
import fs from 'fs'
import syncFor from './utils/syncFor'
import print from './print'
import _progress from 'cli-progress'
/**
* pubPhrase method, this method send phrases and
* snippet data to your composr
*/
function pubPhrase (_type, data, callback) {
let modelType = (_type === 'phrase') ? 'phrase' : 'snippet'
let uploadUrl = process.env.ENV_ENDPOINT + modelType
//let _json = (_type === 'phrase') ? JSON.parse(data) : data
data.version = process.env.PROJECT_VERSION
// call to request
request({
url: uploadUrl,
headers: {
'Authorization': 'Bearer ' + process.env.AT
},
method: 'PUT',
json: data
}, (err, response, body) => {
if (err) callback(err, body)
if (response.statusCode === 401) {
console.log(response.statusCode)
} else if (response.statusCode.toString().indexOf('2') === 0) {
//gt.pulse(body.url)
} else {
print.error(_type + ' not published: ' + body.url + response.statusCode)
}
return callback(null, response)
})
}
function Publisher (_type, items, next) {
let _items = null
if (_type === 'phrase'){
_items = items.filter((i) => {
return (i.__meta.marked === true)
}).map((it) => {
delete it.__meta
return it
})
} else{
_items = items
}
// Progress Bar
let bar1 = new _progress.Bar({
format : 'Publishing [{bar}] {percentage}% | ETA: {eta}s | Current: P({value})',
hideCursor: true,
barCompleteChar: '#',
barIncompleteChar: '.',
fps: 5,
clearOnComplete: true
})
bar1.start(_items.length, 0)
syncFor(0, _items.length, 'start', (i, status, call) => {
if (status === 'done') {
bar1.stop()
print.info('All '+ _type +' are published successfully')
return next(null, true)
} else {
bar1.update(i)
pubPhrase(_type, _items[i], (err, res) => {
if (err) print.error({ error : err, msg: res })
call('next') // this acts as increment (i++)
})
}
})
}
module.exports = Publisher