generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.ts
80 lines (74 loc) · 2.08 KB
/
upload.ts
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
import path from 'path'
import { fileFromPath } from 'formdata-node/file-from-path'
import { uploadByPublicKey } from './request'
import crypto from 'crypto'
import fs from 'fs'
import * as core from '@actions/core'
const publicKey = process.env['OASISBE_PUBLIC_KEY']
function wait(time: number) {
return new Promise<void>(resolve => {
setTimeout(() => {
resolve()
}, time)
})
}
async function recursiveDist(
distPath: string,
callback: (filepath: string) => Promise<any>
) {
const files = fs.readdirSync(distPath)
for (let i = 0; i < files.length; i++) {
const filename = files[i]
const filepath = path.join(distPath, filename)
const stat = fs.statSync(filepath)
if (stat.isFile()) {
await callback(filepath)
await wait(80)
} else if (stat.isDirectory()) {
await recursiveDist(filepath, callback)
}
}
}
export async function uploadPackageJS(dirPath: string) {
const distPath = path.join(dirPath, 'dist')
if (!fs.existsSync(distPath)) {
core.info(`${distPath} does not exist, ignore release.`)
return
}
const pkg = JSON.parse(
fs.readFileSync(path.join(dirPath, 'package.json'), {
encoding: 'utf-8'
})
)
const version = pkg.version
core.debug(`upload package: ${pkg.name}`)
await recursiveDist(distPath, async filepath => {
core.debug(`start upload: ${filepath}`)
const res = await upload({
filename: path.basename(filepath),
filepath,
alias: `${pkg.name}/${version}/${path.relative(distPath, filepath)}`
})
core.info(`uploaded: ${res.data}`)
})
}
export async function upload({
filename,
alias,
filepath
}: {
filename: string
alias: string
filepath: string
}) {
const form = new FormData()
const message = 'upload'
const signature = crypto.publicEncrypt(publicKey, Buffer.from(message))
const file = await fileFromPath(filepath, 'index.txt')
form.append('signature', signature.toString('base64'))
form.append('filename', filename)
form.append('alias', alias)
form.append('file', file)
const result = await uploadByPublicKey(form)
return result.data
}