如何从Grunt任务函数运行shell命令 [英] How to run a shell command from Grunt task function

查看:172
本文介绍了如何从Grunt任务函数运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据我的 Gruntfile.js 中的函数在我的应用程序目录中移动一些图标。有可能做这样的事情吗?我已经尝试了以下(进入开发或暂存文件夹,并将所有文件复制到以前的目录),但coudn't得到它的工作。

  grunt.registerTask('setAppIcon','设置应用图标的任务',function(environment){ 
if(environment.toLowerCase()==development){

grunt.task.run(['exec:command:cd app / lib / extras / res / icon / ios / dev& cp -a。../']);

} else if(environment.toLowerCase()==staging){

grunt .task.run(['exec:command:cd app / lib / extras / res / icon / ios / staging&&& cp -a .. ../']);

}
});


解决方案

可以达到您的要求,当您在函数中调用 grunt.task.run 命令时(即自定义任务),您需要提供对任务运行的引用。



如果您定义了单独的 Target (让我们称它们为 copy_dev copy_staging - 如下例所示),每个 cd ...&& cp ... 命令在 grunt-exec 任务它应该




Gruntfile.js



以下 Gruntfile.js gist显示了如何实现:

 module.exports = function(grunt){

grunt.loadNpmTasks('grunt-exec');

grunt.initConfig({
exec:{
copy_dev:{
cmd:'cd app / lib / extras / res / icon / ios / dev& & cp -a。../'
},
copy_staging:{
cmd:'cd app / lib / extras / res / icon / ios / staging&& cp - a。../'
}
}
});

grunt.registerTask('setAppIcon','设置应用图标的任务',function(){
var environment = process.env.NODE_ENV;

如果没有设置NODE_ENV变量,则提前退出
if(!environment){
grunt.log.writeln(
'\setAppIcon \task failed - NODE_ENV has '(''yellow']

return
}

if(environment.toLowerCase()==development){
grunt.task.run('exec:copy_dev');
grunt.log.writeln('>>复制图标\ dev \...')
} else if(environment.toLowerCase()==staging){
grunt.task.run('exec:copy_staging');
grunt.log.writeln('>>将图标从\\ \\staging \...')
}
});

grunt.registerTask('default',['setAppIcon']);
};






其他注意事项

在名为 setAppIcon 的自定义任务/函数中,我们使用内置节点 process.env



运行 $ grunt 通过您的CLI (使用上面显示的要点),并假设您的 process.env.NODE_ENV 变量尚未设置,或者它可能已通过运行 $ unset NODE_ENV 来取消设置,您将看到以下消息:


setAppIcon任务失败 - NODE_ENV尚未设置


但是,如果 process.env.NODE_ENV 变量已设置为开发 staging 这些文件将按预期复制。



例如通过CLI将成功运作:



$ export NODE_ENV = developm恩&& grunt





$ export NODE_ENV = staging &安培;&安培; grunt



您还会看到以下任一消息记录到控制台:



< blockquote>

>>从dev复制图标...



>>复制staging中的图标...


process.env之后。 NODE_ENV 已被设置为开发暂存,然后运行 $ grunt ,将根据设置的环境复制文件。


I'm trying to move some icons in my app directory based on a function i have inside my Gruntfile.js. Would it be possible to do something like this? I've tried the following (going into dev or staging folder and copying all files to the previous directory), but coudn't get it to work. Thanks in advance.

grunt.registerTask('setAppIcon', 'Task that sets the app icon', function(environment) {
    if (environment.toLowerCase() == "development") {

        grunt.task.run(['exec:command:cd app/lib/extras/res/icon/ios/dev && cp -a . ../']);

    } else if (environment.toLowerCase() == "staging") {

        grunt.task.run(['exec:command:cd app/lib/extras/res/icon/ios/staging && cp -a . ../']);

    } 
});

解决方案

Yes, it's possible to achieve your requirement, however, when you invoke the grunt.task.run command inside your function (i.e. custom task) you need to provide a reference to a task to run.

If you define a separate Target, (Let's call call them copy_dev and copy_staging - as shown in the example below), for each cd ... && cp ... command in the grunt-exec Task it should work successfully.


Gruntfile.js

The following Gruntfile.js gist shows how this can be achieved:

module.exports = function (grunt) {

  grunt.loadNpmTasks('grunt-exec');

  grunt.initConfig({
    exec: {
      copy_dev: {
        cmd: 'cd app/lib/extras/res/icon/ios/dev && cp -a . ../'
      },
      copy_staging: {
        cmd: 'cd app/lib/extras/res/icon/ios/staging && cp -a . ../'
      }
    }
  });

  grunt.registerTask('setAppIcon', 'Task that sets the app icon', function() {
    var environment = process.env.NODE_ENV;

    // Exit early if NODE_ENV variable has not been set.
    if (!environment) {
      grunt.log.writeln(
        '\"setAppIcon\"" task failed - NODE_ENV has not been set.'['yellow']
      )
      return
    }

    if (environment.toLowerCase() == "development") {
      grunt.task.run('exec:copy_dev');
      grunt.log.writeln('>> Copying icons from \"dev\"...')
    } else if (environment.toLowerCase() == "staging") {
      grunt.task.run('exec:copy_staging');
      grunt.log.writeln('>> Copying icons from \"staging\"...')
    }
  });

  grunt.registerTask('default', [ 'setAppIcon' ]);
};


Additional notes

Inside the custom task/function named setAppIcon we obtain the current node environment using nodes builtin process.env

When running $ grunt via your CLI (using the gist shown above), and assuming your process.env.NODE_ENV variable has not been set, or it has possibly been unset by running $ unset NODE_ENV, you will see the following message:

"setAppIcon"" task failed - NODE_ENV has not been set.

However, if the process.env.NODE_ENV variable has been set to either development or staging the files will be copied as expected.

For example running either of the following via your CLI will work successfully:

$ export NODE_ENV=development && grunt

or

$ export NODE_ENV=staging && grunt

You will also see either of the following messages logged to the console:

>> Copying icons from "dev"...

or

>> Copying icons from "staging"...

After process.env.NODE_ENV has been set to either development or staging then just running $ grunt via your CLI, will copy files according to which environment is set.

这篇关于如何从Grunt任务函数运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆