Skip to content

Commit

Permalink
feat: upload js
Browse files Browse the repository at this point in the history
  • Loading branch information
gz65555 committed Aug 30, 2024
1 parent 8891178 commit 5544950
Show file tree
Hide file tree
Showing 10 changed files with 28,693 additions and 19,750 deletions.
47,919 changes: 28,182 additions & 19,737 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

305 changes: 305 additions & 0 deletions dist/licenses.txt

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
},
"dependencies": {
"@actions/core": "^1.10.1",
"micromatch": "^4.0.8"
"axios": "1.6.7",
"formdata-node": "^6.0.3"
},
"devDependencies": {
"@types/micromatch": "^4.0.9",
Expand Down
115 changes: 112 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* The entrypoint for the action.
*/
import { run } from './main'
export * from './upload'

// eslint-disable-next-line @typescript-eslint/no-floating-promises
run()
16 changes: 8 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from '@actions/core'
import { exec } from './exec'
import path from 'path'
import fs from 'fs/promises'
import { uploadPackageJS } from './upload'

/**
* The main function for the action.
Expand All @@ -11,15 +12,14 @@ export async function run(): Promise<void> {
try {
const tag = await getPublishTag()
core.debug(`publish tag is ${tag}`)
try {
const stdout = await exec(`pnpm publish -r --tag ${tag} --no-git-checks`)
core.info(stdout)
} catch (error) {
core.error(JSON.stringify(error))
throw error.output[1].toString()
}
const stdout = await exec(`pnpm publish -r --tag ${tag} --no-git-checks`)
core.info(stdout)

const cwd = process.cwd()
const dirs = await fs.readdir(path.join(cwd, 'packages'))
await Promise.all(dirs.map(dir => uploadPackageJS(path.join(cwd, dir))))
} catch (error) {
core.debug(`error: ${typeof error}`)
core.error(JSON.stringify(error))
core.setFailed(error)
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/request/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios from 'axios'
import https from 'https'

// At request level
const agent = new https.Agent({
rejectUnauthorized: false
})

export function uploadByPublicKey(form: FormData) {
return axios.post(process.env['OASISBE_UPLOAD_URL'], form, {
httpsAgent: agent,
headers: JSON.parse(process.env['OASISBE_REQUEST_HEADER'])
})
}
56 changes: 56 additions & 0 deletions src/upload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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']

export async function uploadPackageJS(dirPath: string) {
const distPath = path.join(dirPath, 'dist')
if (!fs.existsSync(distPath)) {
return
}
const pkg = JSON.parse(
fs.readFileSync(path.join(dirPath, 'package.json'), {
encoding: 'utf-8'
})
)
const version = pkg.version
const files = fs.readdirSync(distPath)
core.debug(`upload package: ${pkg.name}`)
for (let i = 0; i < files.length; i++) {
const filename = files[i]
const filepath = path.join(distPath, filename)

const res = await upload({
filename,
filepath,
alias: `${pkg.name}/${version}/${filename}`
})
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
}
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { uploadPackageJS, upload } = require('./dist/index')
const path = require('path')

uploadPackageJS(process.cwd())
.then(res => console.log(res))
.catch(err => console.log(err))

// upload({
// filename: 'index.js',
// alias: '1.2.10/192.js',
// filepath: path.join(process.cwd(), 'dist','index.js')
// })

0 comments on commit 5544950

Please sign in to comment.