2015-11-16 70 views
1

我正在使用node-wp-cli从Node.js运行WP-cli命令。所有内置的WP-CLI命令都能正常工作,但是我试图运行自定义的WP-CLI命令时卡住了。如何使用node-wp-cli运行自定义wp-cli命令?

有没有办法从node-wp-cli运行我的自定义WP-Cli命令wp MyRegisteredCommand SubCommand?像:

WP.MyRegisteredCommand.SubCommand(function(err,res){ //get response 
    console.log(res); 
}); 

回答

0

这是比较容易的,但需要一些调整...

节点WP-CLI使用wp cli cmd-dump,以确定可用的命令,但该命令WordPress的运行前加载,所以它会只识别内置命令。

在短的TWEAK:

  • 参见WP.js,线schema(function(err,schema){...。您需要知道schema的结构,因此只需运行一些内置命令并在此处放置一个断点即可。
  • 按照上面的架构结构实现您的命令的模式。它会是这个样子:

    my_commands.js

    var schema = { 
        mycmd: { 
         mysubcmd: { 
          args: [ { 
           arg: 'arg1', 
           required: true, 
           type: 'args' 
          } ], 
          endpoint: true, 
          options: { 
           opt1: { 
            required: true, 
            type: 'param' 
           }, 
           flag1: { 
            required: false, 
            type: 'flag' 
           }, 
          } 
         }, 
         use: 'wp mycmd mysubcmd' 
        } 
    } 
    
    module.exports.schema = schema; 
    
  • 修改WP.js

    var mySchema = require('my_commands.js').schema; 
    
    ... 
    
    schema(function(err,schema){ 
    
        schema = _.merge(schema, mySchema); // this line added 
    
        var toMerge = function mapSchema(schema){