Skip to content

Commit

Permalink
✨ feat: 命令自动执行
Browse files Browse the repository at this point in the history
  • Loading branch information
OVYVO committed Mar 20, 2023
1 parent 7679f38 commit 53a7877
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 49 deletions.
3 changes: 3 additions & 0 deletions src/commands/create/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function(params, cmd){
console.log(params, cmd, '=====')
}
16 changes: 16 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import commandCreate from './create'

const commands = {
'create <project-name>': {
description: 'create a project',
option: [
{
cmd: '-f, --force',
msg: 'overwrite target dir if it exit'
}
],
action: commandCreate
}
}

export default commands;
62 changes: 13 additions & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import { Command } from 'commander'
import Pack from '../package.json'
// import inquirer from 'inquirer'
// import { loading } from '@utils'
import {CBlue} from '@utils'

import Commands from '@commands'

const program = new Command()

Expand All @@ -14,50 +13,15 @@ program
.version(Pack.version)
.helpOption('-h, --help')
.usage(`<command> [option]`)
// .command('choose')
// .action(()=>{
// inquirer.prompt([{
// type: 'list',
// message: '测试一下',
// name: 'listcheck',
// choices:[
// new inquirer.Separator('===The Meats==='),
// {
// name: '选择1',
// value: 1
// },
// {
// name: '选择2',
// value: 2
// }
// ],
// validate(answer){
// if(answer.length < 1){
// console.log('请至少选择一项')
// }
// }
// }]).then(answer=>{
// console.log(answer)
// })
// })
// .command('loading')
// .action(()=>{
// loading.start({
// color: 'red',
// text: '测试loading'
// })
// setTimeout(()=>{
// loading.warn('警告')
// setTimeout(()=>{
// loading.info('提示')
// setTimeout(()=>{
// loading.stop()
// },2000)
// },2000)
// },2000)
// })
.command('chalk')
.action(()=>{
console.log(CBlue('测试一次啊'))
})

Object.keys(Commands).forEach(command=>{
const current = program.command(command)
if(Commands[command].option && Commands[command].option.length){
Commands[command].option.forEach(item=>{
current.option(item.cmd,item.msg || '')
})
}
current.action(Commands[command].action)
})

program.parse();

0 comments on commit 53a7877

Please sign in to comment.