使用nodejs创建CLI-将变量传递给Exec Linux命令



im尝试使用nodejs和commander.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:控制器newcontroller

linux:触摸newcontroller

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


P.S(创建新文件后,我也想在其中写入一些内容)

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);

像这样的exec(name)中的变量。

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

相关内容

  • 没有找到相关文章

最新更新