关于npm项目中的bin文件

最近看vue-cli 2.9.0源码的时候看到bin文件下的vue文件中定义了一些指令

#!/usr/bin/env node

require('commander')
  .version(require('../package').version)
  .usage('<command> [options]')
  .command('init', 'generate a new project from a template')
  .command('list', 'list available official templates')
  .command('build', 'prototype a new project')
  .parse(process.argv)

同时在bin文件夹下还有 vue-init、vue-list等文件。
我想请问一下,为什么在commander中定义init指令之后,在命令行输入vue init 指令,可以去执行vue-init里面的代码,这是怎么样的一种实现原理,还是npm帮我们做了这个事情。

阅读 4.3k
2 个回答

你应该看的是这里以及相应的源码

README 里的描述

The commander will try to search the executables in the directory of the entry script (like ./examples/pm) with the name program-command, like pm-install, pm-search
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题