Skip to content

Commit

Permalink
[TASK] add publishGithubRelease task, update jsdoc spec, refactor, cl…
Browse files Browse the repository at this point in the history
…eanup
  • Loading branch information
dmh committed Jun 27, 2023
1 parent 2710bfe commit 81c321b
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 72 deletions.
2 changes: 1 addition & 1 deletion lib/lib/args/help.js
Expand Up @@ -4,7 +4,7 @@
* @memberof COMMANDS
* @returns undefined
*/
async function help () {
function help () {
console.log('\nUsage: quick-release [OPTIONS]\n')
console.log(`Options:
-v, --version Print version information and quit
Expand Down
3 changes: 1 addition & 2 deletions lib/lib/changelog.js
Expand Up @@ -25,12 +25,11 @@ function dateNow () {

/**
* #### parse git logs and return commits as a changelog object
* @async
* @memberof CHANGELOG
* @param {DATA} data - data object
* @returns undefined
*/
async function saveChangelogData (data) {
function saveChangelogData (data) {
data.changelog.generic = saveGenericCommits(data.git.log, data.config.changelog.labels)
data.changelog.breaking = saveBreakingCommits(data.git.log, data.config.changelog.breakingLabel)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/lib/config.js
Expand Up @@ -109,7 +109,7 @@ async function parseQuickreleaseConfig () {

/**
* #### remove not existing files from config
* @description check if quickreleaseConfig.files exists in cwd and remove not existing files
* check if quickreleaseConfig.files exists in cwd and remove not existing files
* @memberof CONFIG
* @private
* @async
Expand All @@ -131,7 +131,7 @@ async function removeNonExistConfigFiles (config) {

/**
* #### update changelog config
* @description prepare changelog config and create changelog file if not exists
* prepare changelog config and create changelog file if not exists
* @async
* @memberof CONFIG
* @param {DATA} data - data
Expand Down
1 change: 0 additions & 1 deletion lib/lib/data.js
@@ -1,7 +1,6 @@
import * as TYPES from '#types/types.js' // eslint-disable-line
import path from 'node:path'
import dotenv from 'dotenv'
// import url from 'node:url'
import { isFileDirExists } from '#utils/fs.js'
import { execa } from 'execa'
import { gitData } from '#git.js'
Expand Down
35 changes: 13 additions & 22 deletions lib/lib/git.js
Expand Up @@ -407,7 +407,6 @@ async function gitRemoteUpdate () {

/**
* #### update git remote data
* @private
* @async
* @param {DATA} data - data object
* @returns {Promise<DATA>} local data
Expand Down Expand Up @@ -447,9 +446,12 @@ async function gitAdd (files) {
* @returns undefined
*/
async function gitCommit (msg) {
const spinner = ora('Commit release').start()
try {
await $`git commit -m ${msg}`
spinner.succeed()
} catch (error) {
spinner.fail()
console.error(error)
}
}
Expand All @@ -458,28 +460,16 @@ async function gitCommit (msg) {
* #### git push to remote
* @async
* @memberof GIT
* @param {string} remote - remote name
* @returns undefined
*/
async function gitPush (remote) {
try {
await $`git push ${remote}`
} catch (error) {
console.error(error)
}
}
/**
* #### git push dry-run to remote
* @async
* @memberof GIT
* @param {string} remote - remote name
* @returns undefined
*/
async function gitPushDryRun (remote) {
async function gitPush () {
const spinner = ora('Push to remote').start()
try {
await $`git push --dry-run ${remote}`
await $`git push`
spinner.succeed()
} catch (error) {
spinner.fail()
console.error(error)
}
}
Expand All @@ -503,13 +493,15 @@ async function gitTag (tag) {
* #### git push tag to remote
* @async
* @memberof GIT
* @param {string} remote - remote name
* @returns undefined
*/
async function gitPushTag (remote) {
async function gitPushTag () {
const spinner = ora('Push tag to remote').start()
try {
await $`git push ${remote} --tags`
await $`git push --tags`
spinner.succeed()
} catch (error) {
spinner.fail()
console.error(error)
}
}
Expand Down Expand Up @@ -589,7 +581,6 @@ export {
gitAdd,
gitCommit,
gitPush,
gitPushDryRun,
gitTag,
gitPushTag,
saveGitlog
Expand Down
55 changes: 42 additions & 13 deletions lib/lib/github.js
Expand Up @@ -20,21 +20,21 @@ let octokit = {}
* @param {DATA} data - data object
* @returns undefined
*/
async function checkGitHubToken (data) {
async function checkGithubToken (data) {
const spinner = ora('Checking GitHub token').start()
if (data.env.GITHUB_TOKEN) {
octokit = new Octokit({ auth: data.env.GITHUB_TOKEN })
try {
await octokit.request('/user')
data.github.token = true
spinner.succeed('GitHub token is valid')
spinner.succeed()
} catch (error) {
spinner.fail('GitHub token is invalid')
spinner.fail()
console.warn(`${chalk.red('Warning:')} GitHub token is invalid. Github release generation will be skipped.`)
}
return octokit
} else {
spinner.fail('GitHub token is not set')
spinner.fail()
console.warn(`${chalk.red('Warning:')} GitHub token is not set. Github release generation will be skipped.`)
console.log(`Add ${chalk.yellow('GITHUB_TOKEN=your-token')} to ${chalk.green('.env')} file to enable GitHub release generation.`)
}
Expand All @@ -48,21 +48,20 @@ async function checkGitHubToken (data) {
* @param {DATA} data - data object
* @returns {Promise<COMMIT_MESSAGE[]|undefined>} - undefined
*/
async function getGitHubCommits (data) {
async function getGithubCommits (data) {
const spinner = ora('Get GitHub commits').start()
try {
// const octokit = new Octokit({ auth: data.env.GITHUB_TOKEN })
const commits = await octokit.request('GET /repos/{owner}/{repo}/commits?sha={branch}&since={date}', {
owner: data.git.remote.owner,
repo: data.git.remote.name,
branch: data.git.branch,
date: data.git.log[data.git.log.length - 1].date
})
const parsedCommits = parseGitHubCommits(data, commits.data)
spinner.succeed('Get GitHub commits')
const parsedCommits = parseGithubCommits(data, commits.data)
spinner.succeed()
return parsedCommits
} catch (error) {
spinner.fail('GitHub commits are not received')
spinner.fail()
console.warn(`${chalk.bold.red('Error:')} GitHub commits are not received. Github release generation will be skipped.`)
}
}
Expand All @@ -75,7 +74,7 @@ async function getGitHubCommits (data) {
* @param {Array<Object>} commits - github commits
* @returns {Array<COMMIT_MESSAGE>} - parsed commits
*/
function parseGitHubCommits (data, commits) {
function parseGithubCommits (data, commits) {
/** @type {Array<COMMIT_MESSAGE>} */
const parsedCommits = []
commits.forEach((/** @type {any} */ item) => {
Expand All @@ -95,8 +94,8 @@ function parseGitHubCommits (data, commits) {
* @param {DATA} data - data object
* @returns undefined
*/
async function saveGitHubCommits (data) {
data.github.commits.all = await getGitHubCommits(data) ?? []
async function saveGithubCommits (data) {
data.github.commits.all = await getGithubCommits(data) ?? []
if (data.github.commits.all.length > 0) {
data.github.commits.generic = saveGenericCommits(data.github.commits.all, data.config.changelog.labels)
data.github.commits.breaking = saveBreakingCommits(data.github.commits.all, data.config.changelog.breakingLabel)
Expand All @@ -105,6 +104,7 @@ async function saveGitHubCommits (data) {

/**
* #### format github release Notes
* @memberof GITHUB
* @param {DATA} data - data object
* @returns undefined
*/
Expand Down Expand Up @@ -156,4 +156,33 @@ function saveGithubReleaseNotes (data) {
data.github.releaseNotes = generic + breaking
}

export { checkGitHubToken, saveGitHubCommits, saveGithubReleaseNotes }
/**
* #### publish GitHub release
* @async
* @memberof GITHUB
* @param {DATA} data - data object
* @returns undefined
*/
async function publishGithubRelease (data) {
const spinner = ora('Publish GitHub release').start()
try {
await octokit.request('POST /repos/{owner}/{repo}/releases', {
owner: data.git.remote.owner,
repo: data.git.remote.name,
tag_name: data.answers.releaseVersion,
target_commitish: data.git.branch,
name: data.answers.releaseVersion,
body: data.github.releaseNotes,
draft: false,
prerelease: false,
generate_release_notes: false
})
spinner.succeed()
} catch (error) {
spinner.fail()
console.error(`${chalk.bold.red('Error:')} Publish GitHub release failed.`)
console.error(error)
}
}

export { checkGithubToken, saveGithubCommits, saveGithubReleaseNotes, publishGithubRelease }
15 changes: 11 additions & 4 deletions lib/lib/init.js
Expand Up @@ -12,8 +12,9 @@ import { info } from '#args/info.js'
import { help } from '#args/help.js'
import { debug } from '#utils/debug.js'
import { release } from '#release.js'
import chalk from 'chalk'

// required checks for cli
// required general checks
await checkIfBinExists('node')
await checkIfBinExists('npm')
await checkIfBinExists('git')
Expand All @@ -34,31 +35,37 @@ const data = await collectLocalData(cliPackageJson)
*/
async function init () {
try {
// init debug mode
if (args.debug) {
process.env.QR_MODE = 'debug'
debug('Initial Debug info', data)
}

// check local repo
await checkLocalRepo(data)

if (args.version) {
// show version
console.log(`v${data.cli.version}`)
process.exit(0)
} else if (args.help) {
await help()
// show help
help()
process.exit(0)
} else if (args.info) {
await info(data)
// show info
info(data)
process.exit(0)
} else {
// release steps
await release(data)
}
} catch (error) {
if (error && process.env.QR_MODE === 'debug') {
console.error(chalk.bold.red('Error:'))
console.error(error)
} else {
console.error(error.message)
console.error(`${chalk.bold.red('Error:')} ${error.message}`)
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions lib/lib/prompts.js
Expand Up @@ -7,7 +7,6 @@ import { stagedFiles } from '#git.js'
/**
* @ignore
* @typedef {TYPES.DATA} DATA {@link DATA}
* @typedef {TYPES.PROMPTS_ANSWERS} PROMPTS_ANSWERS {@link PROMPTS_ANSWERS}
*/

/**
Expand Down Expand Up @@ -202,14 +201,28 @@ async function confirmReleaseCommit (data) {
* @async
* @returns {Promise<boolean>} - true if confirmed
*/
async function publishReleaseNotes () {
async function confirmPublishReleaseNotes () {
const answer = await confirm({
message: 'Publish GitHub release notes?',
default: true
})
return answer
}

/**
* #### Confirm git push to remote repository and push tags
* @memberof PROMPTS
* @async
* @returns {Promise<boolean>} - true if confirmed
*/
async function confirmPush () {
const answer = await confirm({
message: `Push to remote repository with tags? ${chalk.grey('(git push & git push --tags)')}`,
default: true
})
return answer
}

/**
* #### Ask if the release should be pushed to the remote repository
* @memberof PROMPTS
Expand All @@ -226,5 +239,6 @@ export {
selectReleaseType,
createChangelogFile,
checkStagedFiles,
publishReleaseNotes
confirmPublishReleaseNotes,
confirmPush
}

0 comments on commit 81c321b

Please sign in to comment.