-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathuploader.ts
More file actions
62 lines (55 loc) · 2.11 KB
/
uploader.ts
File metadata and controls
62 lines (55 loc) · 2.11 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
import type { AxiosRequestConfig } from 'axios'
import fs from 'node:fs'
import axios from 'axios'
import chalk from 'chalk'
import FormData from 'form-data'
import { ApiTokenResolver } from './api-token-resolver'
import { getAxiosProxyConfiguration, getJwtClaims } from './helpers'
import { getProjectDirectoryPath } from './path.helpers'
export class Uploader {
public async upload(params: { tokenhost: string, apitoken: string, proxyURL?: string, store?: { assetVersionId: string, host?: string } }) {
const { tokenhost, apitoken, proxyURL, store } = params
const accessToken = await ApiTokenResolver.getAccessToken(`https://${tokenhost}`, apitoken, proxyURL)
const claims = getJwtClaims(accessToken)
let url: string
if (store) {
const { host = 'store.leanix.net', assetVersionId } = store
url = `https://${host}/services/torg/v1/assetversions/${assetVersionId}/payload`
}
else {
url = `${claims.instanceUrl}/services/pathfinder/v1/reports/upload`
}
console.log(chalk.yellow(chalk.italic(`Uploading to ${url} ${proxyURL ? `through a proxy` : ``}...`)))
const formData = new FormData()
formData.append('file', fs.createReadStream(getProjectDirectoryPath('bundle.tgz')))
const options: AxiosRequestConfig = {
headers: {
Authorization: `Bearer ${accessToken}`,
...formData.getHeaders()
}
}
if (proxyURL) {
options.httpsAgent = getAxiosProxyConfiguration(proxyURL)
}
try {
const response = await axios.post(url.toString(), formData, options)
if (response.data.status === 'OK') {
console.log(chalk.green('\u2713 Project successfully uploaded!'))
return true
}
else if (response.data.status === 'ERROR') {
console.log(chalk.red(`ERROR: ${response.data.errorMessage}`))
return false
}
}
catch (err) {
if (err.response && err.response.data && err.response.data.errorMessage) {
console.log(chalk.red(`ERROR: ${err.response.data.errorMessage}`))
}
else {
console.log(chalk.red(`ERROR: ${err.message}`))
}
return false
}
}
}