Skip to content

Commit

Permalink
feat: introduce gh assets uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 27, 2023
1 parent deba902 commit 4967fd9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
53 changes: 47 additions & 6 deletions src/main/js/gh.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import {queuefy} from 'queuefy'
import {$, path} from 'zx-extra'
import {$, path, tempy, glob, fs} from 'zx-extra'
import {log} from './log.js'
import {getRepo, pushCommit} from './git.js'
import {formatTag} from './meta.js'
import {formatReleaseNotes} from './changelog.js'
import {msgJoin} from './util.js'
import {asArray, msgJoin} from './util.js'

// https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release
export const ghRelease = async (pkg) => {
log({pkg})('create gh release')

const {ghBasicAuth: basicAuth, ghToken} = pkg.config
const {ghBasicAuth: basicAuth, ghToken, ghAssets} = pkg.config
if (!ghToken) return null

log({pkg})('create gh release')

const now = Date.now()
const {name, version, absPath: cwd} = pkg
const {repoName} = await getRepo(cwd, {basicAuth})
const tag = formatTag({name, version})
Expand All @@ -25,7 +27,13 @@ export const ghRelease = async (pkg) => {
const {stdout} = await $.o({cwd})`curl -H 'Authorization: token ${ghToken}' -H 'Accept: application/vnd.github.v3+json' https://api.github.com/repos/${repoName}/releases -d ${releaseData}`
const res = JSON.parse(stdout.toString().trim())

log({pkg})('gh release url:', res.url, res.html_url)
log({pkg})('gh release url:', res.url, res.html_url, res.upload_url)

if (ghAssets) {
await ghUploadAssets({ghToken, uploadUrl: res.upload_url})
}

log({pkg})(`duration gh release: ${Date.now() - now}`)
}

export const ghPages = queuefy(async (pkg) => {
Expand All @@ -50,3 +58,36 @@ export const ghPages = queuefy(async (pkg) => {
basicAuth
})
})

// https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset8
export const ghPrepareAssets = async (assets) => {
const temp = tempy.temporaryDirectory()

await Promise.all(assets.map(async ({name, source = 'target/**/*', zip, cwd}) => {
const patterns = asArray(source)
const target = path.join(temp, name)
if (patterns.some(s => s.includes('*'))) {
zip = true
}
const files = await glob(patterns, {cwd, absolute: true, onlyFiles: true})

if (!zip && files.length === 1) {
await fs.copy(files[0], target)
return
}

return $.raw`tar -C ${cwd} -cv${zip ? 'z' : ''}f ${target} ${files.join(' ')}`
}))

return temp
}

export const ghUploadAssets = async ({ghToken, ghAssets, uploadUrl}) => {
const cwd = await ghPrepareAssets(ghAssets)

return Promise.all(ghAssets.map(async ({name}) => {
const url = `${uploadUrl}?name=${name}`
return $.o({cwd})`curl -H 'Authorization: token ${ghToken}' -H 'Accept: application/vnd.github.v3+json' -H 'Content-Type: application/octet-stream' ${url} --data-binary '@${name}'`
}))
}

2 changes: 2 additions & 0 deletions src/main/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,5 @@ export const memoizeBy = (fn, getKey = v => v, memo = new Map()) => async (...ar
}

export const camelize = s => s.replace(/-./g, x => x[1].toUpperCase())

export const asArray = v => Array.isArray(v) ? v : [v]
23 changes: 23 additions & 0 deletions src/test/js/gh.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {suite} from 'uvu'
import * as assert from 'uvu/assert'
import {tempy, fs} from 'zx-extra'
import path from 'node:path'
import { ghPrepareAssets } from '../../main/js/gh.js'

const test = suite('gh')

test('`prepareAssets()` preprocesses files for publishing', async () => {
const cwd = tempy.temporaryDirectory()
await fs.outputFile(path.resolve(cwd, 'a.json'), '{"foo": "bar"}')
await fs.outputFile(path.resolve(cwd, 'b.json'), '{"baz": "qux"}')

const temp = await ghPrepareAssets([
{ name: 'jsons.zip', source: ['*.json'], zip: true, cwd},
{ name: 'a.zip', source: 'a.json', cwd, zip: true },
{ name: 'a.json', source: 'a.json', cwd },
])

assert.equal(await fs.readFile(path.resolve(temp, 'a.json'), 'utf8'), '{"foo": "bar"}')
})

test.run()

0 comments on commit 4967fd9

Please sign in to comment.