-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.js
More file actions
68 lines (52 loc) · 1.74 KB
/
package.js
File metadata and controls
68 lines (52 loc) · 1.74 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
const path = require('path')
const fs = require('fs')
const archiver = require('archiver')
const FormData = require('form-data')
const API = require('./api.js')
async function createPackage(srcRelativePath) {
const srcAbsolutePath = path.resolve(srcRelativePath)
if (!fs.existsSync(path.join(srcAbsolutePath, 'manifest.json'))) {
throw new Error('manifest.json not found')
}
if (!fs.existsSync(path.join(srcAbsolutePath, 'zcli.apps.config.json'))) {
throw new Error('zcli.apps.config.json not found')
}
const manifest = fs.readFileSync(path.join(srcAbsolutePath, 'manifest.json'), 'utf8')
const { version } = JSON.parse(manifest)
const packageName = `${new Date().toISOString().replace(/[^0-9]/g, '')}-v${version}`
const packagePath = `${srcAbsolutePath}/tmp/${packageName}.zip`
if (!fs.existsSync(path.join(srcAbsolutePath, 'tmp'))) {
fs.mkdirSync(path.join(srcAbsolutePath, 'tmp'))
}
const packageOutput = fs.createWriteStream(packagePath);
const packageArchive = archiver('zip')
packageArchive.pipe(packageOutput)
packageArchive.glob('**', {
cwd: srcAbsolutePath,
ignore: ['tmp/**']
})
await packageArchive.finalize()
if (!fs.existsSync(packagePath)) {
throw new Error(`Failed to create package at ${packagePath}`)
}
return packagePath
}
// Zendesk API route not working as expected
async function validatePackage(packagePath) {
const formData = new FormData()
formData.append('file', fs.createReadStream(packagePath))
const response = await API('/v2/apps/validate',
{
data: formData,
method: 'POST'
}
)
if (response.status !== 200) {
throw new Error('Package validation failed')
}
return response.data
}
module.exports = {
createPackage,
validatePackage
}