Node.js 交互命令开发

  • A+
所属分类:node.js

#!/usr/bin/env node

const commander = require('commander')
const inquirer = require('inquirer')
const colors = require('colors')
const exec = require('child_process').exec

commander
	.version( '0.0.1' )
	.option( '-i, --init', ' initialize normal workspace ' )
	.parse( process.argv );

if( commander.init ){
	var questions = [
		{
			type: 'input',
			name: 'workname',
			message: 'Please write a short string as workspace name!',
			validate: function( text ){
				if( /^(\w+)/.test( text ) ){
					return true;	
				}
				return 'incorrect workspace name!'.red;
			},
			filter: function( text ){
				return text.trim();
			}
	  	},
	  	{
			type: 'rawlist',
			name: 'type',
			choices: ['normal', 'vue', 'react'],
			message: 'Please choice a type to initialize workspace!',
	  	}
	];

	inquirer.prompt(questions).then(function (answers) {
	  	exec( 'mkdir ${answers.workname}' );
	  	console.log( process.cwd() )
	});
}

保存bin文件夹下test.js
package.json bin:{ test: bin/test.js }
执行npm install -g 注册到全局
执行test测试

参考连接: commander  inquirer

weinxin
我的微信
欢迎来撩!!

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: