Skip to content

Commit

Permalink
Feat: 完成 fixit create [project-name] 命令
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Nov 24, 2023
1 parent 0ec6aa2 commit c518681
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 81 deletions.
89 changes: 10 additions & 79 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#!/usr/bin/env node
import { Command } from 'commander'
import inquirer from 'inquirer'
import download from 'download-git-repo'
import chalk from 'chalk'
import ora from 'ora'
import fs from 'fs'
import {
createAction,
updateAction,
helpAction,
} from '../lib/actions.js'

const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const program = new Command()
const logo =
`
const logo = `
=============================================
▄████ ▄█ ▄ ▄█ ▄▄▄▄▀
Expand All @@ -28,83 +29,13 @@ FixIt is a clean, elegant but advanced blog theme for Hugo
built with love by Lruihao and his friends.\n
Complete documentation is available at ${chalk.cyan('https://fixit.lruihao.cn/')}.`

const createAction = () => {
inquirer
.prompt([
{
type: 'input',
message: '请输入项目名称',
name: 'name',
},
{
type: 'list',
message: '请选择项目模板',
name: 'template',
choices: [
'Based on Git submodule',
'Based on Go module',
],
},
])
.then((answers) => {
console.log(`正在初始化项目${answers.name},请稍等`)
const spinner = ora('download template......').start()
console.log(answers.template)
setTimeout(() => {
console.log(chalk.green('Success!'))
spinner.succeed()
}, 1000)
// download(remote, tarName, { clone: true }, function (err) {
// if (err) {
// console.log(chalk.red(err))
// spinner.fail()
// } else {
// console.log(chalk.green('成功'))
// spinner.succeed()
// }
// })
})
}

// define commands
program
.command('create <project>')
.description('create a new FixIt project from a template')
.action(createAction)

program
.command('update')
.description('update the FixIt theme to the latest version')
.action(() => {
console.log('update')
})

program
.command('help <command>')
.description('display help for a specific command')
.action((command) => {
switch (command) {
case 'create':
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')}`)
break
case 'help':
console.log('Display help for a specific command.')
console.log(`Usage: ${chalk.blue('fixit help <command>')}`)
break
default:
console.log(`Unknown help topic ${chalk.red(command)}.`)
console.log(`Refer to ${chalk.blue('fixit --help')} for supported commands.`)
}
})
program.command('create <project-name>').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)
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')
program.option('-l, --latest', 'check the latest version of FixIt theme')

// define cli
program
Expand Down
147 changes: 147 additions & 0 deletions lib/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import inquirer from 'inquirer'
import { simpleGit, CleanOptions } from 'simple-git'
import chalk from 'chalk'
import ora from 'ora'

/**
* action for create command
* @example fixit create [project-name]
*/
function createAction() {
const promptList = [
{
type: 'input',
message: 'Please input project name:',
name: 'name',
validate: (val) => {
if (val === '') {
return 'project name is required'
}
return true
}
},
{
type: 'list',
message: 'Please choose a template:',
name: 'template',
choices: [
{
name: 'Git submodule',
value: 'git',
},
{
name: 'Hugo module',
value: 'go',
},
],
}
]
const repositories = {
git: 'https://github.com/hugo-fixit/hugo-fixit-blog-git.git',
go: 'https://github.com/hugo-fixit/hugo-fixit-blog-go.git',
}
const projectName = process.argv[3]
if (projectName) {
promptList[0].default = projectName
}
inquirer
.prompt(promptList)
.then((answers) => {
console.log(`Initializing FixIt project ${answers.name}, please wait a moment.`)
// 1. download template
const spinnerClone = ora(`Downloading template from ${chalk.cyan(repositories[answers.template])}.`).start()
const progress = ({method, stage, progress}) => {
spinnerClone.text = chalk.yellow(`git.${method} ${stage} stage ${progress}% complete${'.'.repeat(Math.floor(Math.random() * 3) + 1)}`)
}
const git = simpleGit({ progress, recursive: true })
git.clean(CleanOptions.FORCE)
// TODO try to performance submodules download by fixit update command
git.clone(repositories[answers.template], answers.name, {
'--depth': 1,
'--branch': 'main',
'--single-branch': true,
'--recurse-submodules': answers.template === 'git',
'--shallow-submodules': answers.template === 'git',
}, (err) => {
if (err) {
spinnerClone.fail()
console.log(chalk.red(err))
return
}
spinnerClone.text = `${chalk.green('[Success]')} downloaded template from ${chalk.cyan(repositories[answers.template])}.`
spinnerClone.succeed()

// 2. initialize FixIt project
const spinnerInit = ora(`Initializing FixIt project ${answers.name}`).start()
// remove remote origin
git.cwd(answers.name)
spinnerInit.text = 'Removing remote origin.'
git.removeRemote('origin', (err) => {
if (err) {
spinnerInit.fail()
console.log(chalk.red(err))
return
}
spinnerInit.text = `${chalk.green('[Success]')} removed remote origin.`
})
// remove history commits
spinnerInit.text = 'Removing history commits.'
git.raw(['update-ref', '-d', 'HEAD'], (err) => {
if (err) {
spinnerInit.fail()
console.log(chalk.red(err))
return
}
spinnerInit.text = `${chalk.green('[Success]')} removed history commits.`
})
.then(async () => {
// commit first commit
await git.add('./*')
await git.commit('first commit')
spinnerInit.text = `${chalk.green('[Success]')} initialized FixIt project ${answers.name}.`
spinnerInit.succeed()
console.log('🎉 Congratulations! You have created a new FixIt project.\n')
console.log(`${chalk.blue(`cd ${answers.name} && hugo server`)}\n\nGo! Enjoy it and Fix it! 🐛`)
})
})
})
}

/**
* action for update command
* @example fixit update
*/
function updateAction() {
console.log('update')
}

/**
* action for help command
* @param {String} command specific command
* @example fixit help <command>
*/
function helpAction(command) {
switch (command) {
case 'create':
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')}`)
break
case 'help':
console.log('Display help for a specific command.')
console.log(`Usage: ${chalk.blue('fixit help <command>')}`)
break
default:
console.log(`Unknown help topic ${chalk.red(command)}.`)
console.log(`Refer to ${chalk.blue('fixit --help')} for supported commands.`)
}
}

export {
createAction,
updateAction,
helpAction,
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"dependencies": {
"chalk": "^5.3.0",
"commander": "^11.1.0",
"download-git-repo": "^3.0.2",
"inquirer": "^9.2.12",
"ora": "^7.0.1",
"shelljs": "^0.8.5"
"shelljs": "^0.8.5",
"simple-git": "^3.21.0"
},
"engines": {
"node": ">=16"
Expand Down

0 comments on commit c518681

Please sign in to comment.