tj大神出品
平级命令执行
最近在研究antd库,自带antd-tools命令工具处理编译逻辑,就好奇这块的配置。antd-tools run compile追踪到 antd-tools/lib/cli/index.js, 发现它只是普通代码,并没有触发其他任务,那么antd-tools run compile的任务执行逻辑呢
后来发现,commander的command方法有这么一段逻辑
antd-tools代码
#!/usr/bin/env node
'use strict';
require('colorful').colorful();
const program = require('commander');
const packageInfo = require('../../package.json');
program
.version(packageInfo.version)
// run compile
.command('run [name]', 'run specified task')
.parse(process.argv);
// https://github.com/tj/commander.js/pull/260
const proc = program.runningCommand;
if (proc) {
proc.on('close', process.exit.bind(process));
proc.on('error', () => {
process.exit(1);
});
}
const subCmd = program.args[0];
if (!subCmd || subCmd !== 'run') {
program.help();
}
let bin = path.basename(scriptPath, path.extname(scriptPath)) + '-' + subcommand._name;
// scriptPath就是 antd-tools/lib/cli/ 子命令_name为run 参数为compile
if (subcommand._executableFile) {
bin = subcommand._executableFile;
}
// 也就是会执行 antd-tools/lib/cli/run.js 继而执行链出来了
proc = childProcess.spawn(bin, args, { stdio: 'inherit' });
```