forked from zendesk/zopim-chat-web-sdk-sample-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.js
executable file
·122 lines (108 loc) · 2.73 KB
/
tools.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
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
#!/usr/bin/env node
/*eslint no-console:0 */
const program = require('commander')
const conf = require('./package.json')
const request = require('request')
const fs = require('fs')
const { promisify } = require('util')
const requestPromiseFactory = promisify(request)
const writeFilePromiseFactory = promisify(fs.writeFile)
function purgeCdn() {
const options = {
method: 'POST',
url: 'https://purge.jsdelivr.net/',
headers: {
'content-type': 'application/json',
'cache-control': 'no-cache'
},
body: JSON.stringify({
path: [
`/npm/${conf.name}/dist/assets/widget.js`,
`/npm/${conf.name}@${conf.version.replace(
/(\d+)\.\d+\.\d+(?:\-.*)?/,
'$1'
)}/dist/assets/widget.js`,
`/npm/${conf.name}@${conf.version.replace(
/(\d+\.\d+)\.\d+(?:\-.*)?/,
'$1'
)}/dist/assets/widget.js`
]
})
}
console.log('purging jsdelivr cdn cache...')
return requestPromiseFactory(options)
.then(res => {
console.log(res.status)
if (res.statusCode !== 200) {
throw new Error('fetch error')
}
console.log('purged jsdelivr cdn cache')
})
.catch(err => {
console.error(err)
process.exitCode = 1
})
}
function downloadZendeskSdk() {
console.log('downloading zendesk web sdk...')
const options = {
method: 'GET',
url: 'https://dev.zopim.com/web-sdk/latest/web-sdk.js',
headers: {
'cache-control': 'no-cache'
}
}
return requestPromiseFactory(options)
.then(res => {
if (res.statusCode !== 200) {
throw new Error('fetch error')
}
return writeFilePromiseFactory('./vendor/web-sdk.js', res.body)
})
.catch(err => {
console.error(err)
process.exitCode = 1
})
}
program.version(conf.version).usage('<command>')
program
.command('purge-cdn')
.alias('pcdn')
.usage('[options]')
.description('purges the jsdelivr cdn cache')
.action(() => {
purgeCdn()
})
.on('--help', () => {
console.log(' Examples:')
console.log()
console.log(' $ tools purge-cdn')
console.log()
})
program
.command('download-zendesk-sdk')
.alias('dzs')
.usage('[options]')
.description(
'downloads the zendesk chat web sdk and places it under the vendor folder'
)
.action(() => {
downloadZendeskSdk()
})
.on('--help', () => {
console.log(' Examples:')
console.log()
console.log(' $ tools download-zendesk-sdk')
console.log()
})
program.on('command:*', () => {
console.error(
'Invalid command: %s\nSee --help for a list of available commands.',
program.args.join(' ')
)
process.exit(1)
})
program.parse(process.argv)
if (!process.argv.slice(2).length) {
program.help()
}