Skip to content

Commit

Permalink
Feat: 完成 fixit check 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Nov 25, 2023
1 parent 3b8f70c commit 99d3222
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 15 deletions.
11 changes: 4 additions & 7 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chalk from 'chalk'
import fs from 'fs'
import {
createAction,
updateAction,
checkAction,
helpAction,
} from '../lib/actions.js'

Expand Down Expand Up @@ -35,17 +35,14 @@ program
.description('create a new FixIt project from a template')
.action(createAction)
program
.command('update')
.description('update the FixIt theme to the latest version')
.action(updateAction)
.command('check')
.description('check the latest version of FixIt theme')
.action(checkAction)
program
.command('help <command>')
.description('display help for a specific command')
.action(helpAction)

// define options
program.option('-l, --latest', 'check the latest version of FixIt theme')

// define cli
program
.usage('<command> [options]')
Expand Down
35 changes: 27 additions & 8 deletions lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import inquirer from 'inquirer'
import { simpleGit, CleanOptions } from 'simple-git'
import chalk from 'chalk'
import ora from 'ora'
import { getLatestRelease } from './utils.js'

/**
* action for create command
Expand Down Expand Up @@ -108,11 +109,27 @@ function createAction() {
}

/**
* action for update command
* @example fixit update
* action for check command
* @example fixit check
* @example GITHUB_TOKEN=ghp_ifbeKixxxxxxxxxxxxxxxxxxxxxxxx0gVAgF fixit check
*/
function updateAction() {
console.log('update')
function checkAction() {
const spinner = ora('Checking the latest version of FixIt theme.').start()
getLatestRelease('hugo-fixit', 'FixIt')
.then(({ version, changelog, homeUrl }) => {
spinner.text = `${chalk.green('[Success]')} the latest version of FixIt theme is ${chalk.blue(version)}.`
spinner.succeed()
console.log(`Release: ${chalk.cyan(homeUrl)}\n\n${chalk.magenta(changelog)}\n`)
console.log(`${chalk.green('Note:')}\nYou can use commands below to update FixIt theme to the latest version.\n`)
console.log(`Git submodule:\n ${chalk.blue('git submodule update --remote --merge themes/FixIt')}`)
console.log(`Hugo module:\n ${chalk.blue(`hugo mod get -u github.com/hugo-fixit/FixIt@${version}\n hugo mod tidy\n`)}`)
})
.catch((error) => {
spinner.text = `${chalk.red('[Failed]')} failed to check the latest version of FixIt theme.`
spinner.fail()
console.log(chalk.red(error))
console.log(`\n${chalk.green('Note:')}\nYou can set GITHUB_TOKEN env to avoid GitHub API rate limit.\nRun command ${chalk.blue('fixit help check')} for more details.\n`)
})
}

/**
Expand All @@ -126,9 +143,11 @@ function helpAction(command) {
console.log('Create a new FixIt project from a template based on Git submodule or Hugo module.')
console.log(`Usage: ${chalk.blue('fixit create <project-name>')}`)
break
case 'update':
console.log('Update the FixIt theme to the latest version for current project.')
console.log(`Usage: ${chalk.blue('fixit update')}`)
case 'check':
console.log('Check the latest version of FixIt theme.')
console.log(`Usage: ${chalk.blue('[GITHUB_TOKEN=xxx] fixit check')}`)
console.log(`\n${chalk.green('Note:')}\nYou can set GITHUB_TOKEN env to avoid GitHub API rate limit.`)
console.log(`Head to ${chalk.cyan('https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token')}\nfor guidance on how to create a personal access token.\n`)
break
case 'help':
console.log('Display help for a specific command.')
Expand All @@ -142,6 +161,6 @@ function helpAction(command) {

export {
createAction,
updateAction,
checkAction,
helpAction,
}
52 changes: 52 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import https from 'https'

/**
* get latest release info from GitHub API
* @param {String} repoOwner repo owner
* @param {String} repoName repo name
* @example getLatestRelease('hugo-fixit', 'FixIt')
* @returns
*/
function getLatestRelease(repoOwner, repoName) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.github.com',
path: `/repos/${repoOwner}/${repoName}/releases/latest`,
headers: { 'User-Agent': 'mozilla/5.0' },
}
// set Authorization header set to avoid GitHub API rate limit
if (process.env.GITHUB_TOKEN) {
options.headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`
}

const req = https.get(options, (res) => {
let data = ''

res.on('data', (chunk) => {
data += chunk
})

res.on('end', () => {
if (res.statusCode === 200) {
const releaseInfo = JSON.parse(data)
const version = releaseInfo.tag_name
const changelog = releaseInfo.body
const homeUrl = releaseInfo.html_url
resolve({ version, changelog, homeUrl })
} else {
reject(`Failed to get latest release (${res.statusCode})`)
}
})
})

req.on('error', (err) => {
reject(err.message)
})

req.end()
})
}

export {
getLatestRelease,
}

0 comments on commit 99d3222

Please sign in to comment.