You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A lot of packages have one or more executable files that they’d like to install into the PATH. npm makes this pretty easy (in fact, it uses this feature to install the “npm” executable.)
To use this, supply a bin field in your package.json which is a map of command name to local file name. On install, npm will symlink that file into prefix/bin for global installs, or ./node_modules/.bin/ for local installs.
//版本号对比工具constsemver=require('semver')constrequiredVersion=require('../package.json').engines.nodefunctioncheckNodeVersion(wanted,id){//process.version返回当前使用的Node.js版本号if(!semver.satisfies(process.version,wanted)){console.log(chalk.red('You are using Node '+process.version+', but this version of '+id+' requires Node '+wanted+'.\nPlease upgrade your Node version.'))//终止当前进程并返回1状态码process.exit(1)}}checkNodeVersion(requiredVersion,'vue-cli')if(semver.satisfies(process.version,'9.x')){//以红色字体渲染文本console.log(chalk.red(`You are using Node ${process.version}.\n`+`Node.js 9.x has already reached end-of-life and will not be supported in future major releases.\n`+`It's strongly recommended to use an active LTS version instead.`))}
program.command('create <app-name>').description('create a new project powered by vue-cli-service').option('-p, --preset <presetName>','Skip prompts and use saved or remote preset').option('-d, --default','Skip prompts and use default preset').option('-i, --inlinePreset <json>','Skip prompts and use inline JSON string as preset').option('-m, --packageManager <command>','Use specified npm client when installing dependencies').option('-r, --registry <url>','Use specified npm registry when installing dependencies (only for npm)').option('-g, --git [message]','Force git initialization with initial commit message').option('-n, --no-git','Skip git initialization').option('-f, --force','Overwrite target directory if it exists').option('-c, --clone','Use git clone when fetching remote preset').option('-x, --proxy','Use specified proxy when creating project').option('-b, --bare','Scaffold project without beginner instructions').action((name,cmd)=>{constoptions=cleanArgs(cmd)// --git makes commander to default git to trueif(process.argv.includes('-g')||process.argv.includes('--git')){options.forceGit=true}require('../lib/create')(name,options)})
.command('create <app-name>')绑定了create命令,并保存了输入的项目名,.option('-d,--default','Skip prompts and use default preset')用于定义选项值,设置描述及选项快捷键,并将选项值保存到process.argv中。
在链式配置好选项值后,.action(()=>{})用于配置该命令的处理函数,将输入的项目名和配置的选项传入create.js中执行。
constfs=require('fs-extra')constpath=require('path')constchalk=require('chalk')constinquirer=require('inquirer')constCreator=require('./Creator')const{ clearConsole }=require('./util/clearConsole')const{ getPromptModules }=require('./util/createTools')const{ error, stopSpinner, exit }=require('@vue/cli-shared-utils')constvalidateProjectName=require('validate-npm-package-name')asyncfunctioncreate(projectName,options){if(options.proxy){//设置代理process.env.HTTP_PROXY=options.proxy}constcwd=options.cwd||process.cwd()constinCurrent=projectName==='.'constname=inCurrent ? path.relative('../',cwd) : projectNameconsttargetDir=path.resolve(cwd,projectName||'.')constresult=validateProjectName(name)if(!result.validForNewPackages){console.error(chalk.red(`Invalid project name: "${name}"`))result.errors&&result.errors.forEach(err=>{console.error(chalk.red.dim('Error: '+err))})result.warnings&&result.warnings.forEach(warn=>{console.error(chalk.red.dim('Warning: '+warn))})exit(1)}if(fs.existsSync(targetDir)){if(options.force){awaitfs.remove(targetDir)}else{awaitclearConsole()if(inCurrent){const{ ok }=awaitinquirer.prompt([{name: 'ok',type: 'confirm',message: `Generate project in current directory?`}])if(!ok){return}}else{const{ action }=awaitinquirer.prompt([{name: 'action',type: 'list',message: `Target directory ${chalk.cyan(targetDir)} already exists. Pick an action:`,choices: [{name: 'Overwrite',value: 'overwrite'},{name: 'Merge',value: 'merge'},{name: 'Cancel',value: false}]}])if(!action){return}elseif(action==='overwrite'){console.log(`\nRemoving ${chalk.cyan(targetDir)}...`)awaitfs.remove(targetDir)}}}}constcreator=newCreator(name,targetDir,getPromptModules())awaitcreator.create(options)}module.exports=(...args)=>{returncreate(...args).catch(err=>{stopSpinner(false)// do not persisterror(err)if(!process.env.VUE_CLI_TEST){process.exit(1)}})}
The text was updated successfully, but these errors were encountered:
源码地址点这里:Vue-Cli源码地址
初始化项目
在全局安装Vue-Cli后,可以使用
vue create hello-world
初始化项目,我们就从这里开始看源码。package.json
下面为项目的package.json文件
首先,vue命令被注册在package.json的
bin
字段下,关于bin
字段的使用可以参考npm官方文档介绍部分:由
bin
字段注册的可执行文件vue.js为项目的入口,在经过全局安装后,vue
命令被注册到全局的bin
目录下,这时我们就可以直接全局使用vue.js可执行文件。vue.js
在vue.js顶部写入的
#!/usr/bin/env node
,可以保证默认使用node来运行该可执行文件,这样运行时就可以直接通过输入在bin
中注册号的的执行名vue
来运行vue.js。执行vue命令后,首先检查node.js版本号:
检查版本
初始化项目
Vue-Cli使用commander作为命令工具,详细关于commander的API可以查阅commander的github主页。
接着往下看,进入初始化项目的配置,下面是源码:
.command('create <app-name>')
绑定了create
命令,并保存了输入的项目名,.option('-d,--default','Skip prompts and use default preset')
用于定义选项值,设置描述及选项快捷键,并将选项值保存到process.argv
中。在链式配置好选项值后,
.action(()=>{})
用于配置该命令的处理函数,将输入的项目名和配置的选项传入create.js中执行。The text was updated successfully, but these errors were encountered: