0

我正在尝试使用 nodejs 和 command.js 包创建一个简单的 CLI。

CLI 的目的是触摸一个新文件

const program = require('commander');
const exec    = require('child_process').exec;

program.version('0.0.1')
       .description('Command Line Interface (CLI)');

program.command('make:controller <name>')
       .description('Add a new controller called <name>')
       .action(function (name) {
           exec("touch name");
       });

program.parse(process.argv);

命令:make:controller NewController

Linux:触摸 NewController

我如何将名称变量传递给 exec() 命令。


PS(创建新文件后,我还想在其中写入一些内容)

4

2 回答 2

0
const program = require('commander');
const exec    = require('child_process').exec;

program.version('0.0.1')
       .description('Command Line Interface (CLI)');

program.command('make:controller <name>')
       .description('Add a new controller called <name>')
       .action(function (name) {
           exec("touch " + name);//mind the space after touch
       });

program.parse(process.argv);
于 2017-09-10T11:19:36.120 回答
0

像这样在 exec(name) 中传递变量。

program.command('make:controller <name>')
   .description('Add a new controller called <name>')
   .action(function (name) {
       exec(name);
   });
于 2017-09-10T11:24:12.660 回答