Skip to content

Commit

Permalink
fix: missing module packaging not working - swap octonode with octoki…
Browse files Browse the repository at this point in the history
…t [publish binary]
  • Loading branch information
JCMais committed Dec 17, 2023
1 parent 384faf8 commit ac08162
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 398 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@commitlint/cli": "18.4.3",
"@microsoft/api-documenter": "7.23.12",
"@microsoft/api-extractor": "7.38.3",
"@octokit/rest": "^20.0.2",
"@types/body-parser": "1.19.5",
"@types/cookie-parser": "1.4.6",
"@types/express": "4.17.21",
Expand Down Expand Up @@ -109,7 +110,6 @@
"mocha": "10.2.0",
"np": "9.2.0",
"nyc": "15.1.0",
"octonode": "0.9.5",
"prettier": "3.1.0",
"progress": "2.0.3",
"should": "13.2.3",
Expand Down
199 changes: 94 additions & 105 deletions scripts/module-packaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const octonode = require('octonode')
const { Octokit } = require('@octokit/rest')
const log = require('npmlog')
const fs = require('fs')
const path = require('path')
const versionTag =
const tag =
process.env['NODE_LIBCURL_VERSION_TAG'] ||
'v' + require('../package.json').version //current version of the package.

Expand All @@ -31,83 +31,87 @@ if (args[0] !== validArgs[0] && args[0] !== validArgs[1]) {
process.exit(-1)
}

const octo = octonode.client(process.env['NODE_LIBCURL_GITHUB_TOKEN'])
const repo = octo.repo('JCMais/node-libcurl')
const octo = new Octokit({
auth: process.env['NODE_LIBCURL_GITHUB_TOKEN'],
})

const [owner, repo] = 'JCMais/node-libcurl'.split('/')
const commands = {
publish: publish,
unpublish: unpublish,
}

commands[args[0].replace('--', '')](args[1])

function publish(pathToPackage) {
getReleaseByTag(versionTag, (error, data, headers) => {
if (error) {
if (error.statusCode && error.statusCode === 404) {
createRelease(
versionTag,
attachPackageToRelease.bind(null, pathToPackage),
)
} else {
doSomethingWithError(error)
}
} else {
attachPackageToRelease(pathToPackage, null, data, headers)
}
})
}
commands[args[0].replace('--', '')](args[1]).catch((error) => {
console.error(error)
process.exit(1)
})

function unpublish(pathToResource) {
getReleaseByTag(versionTag, (error, release /*, headers*/) => {
if (error) {
doSomethingWithError(error)
return
}
/**
* @param {string} pathToPackage
*/
async function publish(pathToPackage) {
let release = await getReleaseByTag(tag)

if (release) {
log.info('', 'release for tag "%s" found: %s', tag, release.url)
} else {
log.info('', 'release "%s" not found', tag)
release = await createRelease(tag)
}

removePackageFromRelease(pathToResource, release)
})
await attachPackageToRelease(pathToPackage, release)
}

function getReleaseByTag(tagName, cb) {
log.info('', 'searching for release "%s"', tagName)
async function unpublish(pathToResource) {
const release = await getReleaseByTag(tag)

repo.release('tags/' + tagName).info((error, data, headers) => {
if (error && error.statusCode && error.statusCode === 404) {
log.info('', 'release for tag "%s" not found.', tagName)
} else {
log.info('', 'release for tag "%s" found: %s', tagName, data.url)
}

cb(error, data, headers)
})
await removePackageFromRelease(pathToResource, release)
}

function createRelease(tagName, cb) {
log.info('', 'creating release for tag "%s"', tagName)
async function getReleaseByTag(tag) {
log.info('', 'searching for release "%s"', tag)

const release = await octo.repos
.getReleaseByTag({
owner,
repo,
tag,
})
.catch((error) => {
if (error.status === 404) {
return null
}
throw error
})

repo.release(
{
tag_name: tagName,
},
cb,
)
return release?.data
}

function attachPackageToRelease(pckg, err, release /*, headers*/) {
if (err) {
doSomethingWithError(err)
return
}
async function createRelease(tag) {
log.info('', 'creating release for tag "%s"', tag)

const { data } = await octo.repos.createRelease({
owner,
repo,
tag_name: tag,
})

return data
}

/**
*
* @param {string} pckg
* @param {Awaited<ReturnType<Octokit["repos"]["getRelease"]>>["data"]} release
* @returns
*/
async function attachPackageToRelease(pckg, release) {
const packagePath = path.resolve(pckg)

if (!fs.existsSync(packagePath)) {
log.error(
'',
'could not find the package in the specified path: "%s"',
packagePath,
throw new Error(
`Could not find the package in the specified path: "${packagePath}"`,
)
process.exit(-1)
}

log.info(
Expand All @@ -124,39 +128,38 @@ function attachPackageToRelease(pckg, err, release /*, headers*/) {
if (release.assets[i].name === packageFileName) {
log.warn(
'',
'package "%s" already attached to release "%s"',
'package "%s" already attached to release "%s" (%s)',
packageFileName,
release.tag_name,
release.id,
)
process.exit(0)
return
}
}

const fileContent = fs.readFileSync(packagePath)

repo.release(release.id).uploadAssets(
fileContent,
{
name: packageFileName,
contentType: 'application/x-gzip',
const { data } = await octo.repos.uploadReleaseAsset({
owner,
repo,
name: packageFileName,
data: fileContent,
release_id: release.id,
headers: {
'Content-Type': 'application/x-gzip',
},
(error, data /*, headers*/) => {
if (error) {
doSomethingWithError(error)
return
}
})

log.info(
'',
'package attached with success: %s',
data.browser_download_url,
)
process.exit(0)
},
)
log.info('', 'package attached with success: %s', data.browser_download_url)
}

function removePackageFromRelease(packageToDelete, release) {
/**
*
* @param {string} pckg
* @param {Awaited<ReturnType<Octokit["repos"]["getRelease"]>>["data"]} release
* @returns
*/
async function removePackageFromRelease(packageToDelete, release) {
const packageToDeleteName = path.basename(packageToDelete)

let found = false
Expand All @@ -174,25 +177,17 @@ function removePackageFromRelease(packageToDelete, release) {
if (releaseAsset.name === packageToDeleteName) {
found = true

//@FIXME using internals because there is no way to remove directly
// uri to remove assets is: repos/:owner/:repo/releases/assets/:id
repo.client.del(
'/repos/' + repo.name + '/releases/assets/' + releaseAsset.id,
null,
(error /*, data, headers*/) => {
if (error) {
doSomethingWithError(error)
return
}

log.info(
'',
'removed package "%s" from release "%s"',
packageToDelete,
release.tag_name,
)
process.exit(0)
},
await octo.repos.deleteReleaseAsset({
owner,
repo,
asset_id: releaseAsset.id,
})

log.info(
'',
'removed package "%s" from release "%s"',
packageToDelete,
release.tag_name,
)
}
}
Expand All @@ -204,11 +199,5 @@ function removePackageFromRelease(packageToDelete, release) {
packageToDelete,
release.tag_name,
)
process.exit(-1)
}
}

function doSomethingWithError(error) {
log.error('', error)
process.exit(-1)
}
Loading

0 comments on commit ac08162

Please sign in to comment.