【问题标题】:is it possible to extract file name in a Grunt task (with the grunt-cmd-transport plugin)是否可以在 Grunt 任务中提取文件名(使用 grunt-cmd-transport 插件)
【发布时间】:2013-11-05 08:24:06
【问题描述】:

我阅读了 Grunt 官方文档和这个帖子respective file name

很遗憾,我没有得到答案

我的文件结构如下:

src/
    modules/
        module1/
            static/
                abc.js
                def.js
                ......
        module2/
            static/
                123.js
                456.js
                ......

这是我的 Gruntfile 片段:

        module1: {
            options: {
                idleading: 'module1/static/'
            },
            files: [
                {
                    cwd: 'modules/module1/static',
                    src: '**/*.js',
                    dest: '.build/module1/static'
                }
            ]
        },
        module2: {
            options: {
                idleading: 'module2/static/'
            },
            files: [
                {
                    cwd: 'modules/module2/static',
                    src: '**/*.js',
                    dest: '.build/module2/static'
                }
            ]
        },

如您所见,这两个目标非常相似,唯一的区别是“模块”名称,我们得到了大约 20 多个模块......所以将上述配置复制 20 多次似乎太愚蠢了.. .

所以我的问题是:我能否找到一种方法来编写函数,遍历所有目录,提取模块名称,然后传递给“cwd”、“dest”、“idleading”等配置?

请给我解决方案目录,非常感谢!

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    很抱歉回答我自己的问题。但我写了一个 Gruntfile 来解决这个问题。我无法在评论中发布它...

    grunt.registerTask('recursive_transport', 'recursive every directory, and perform transport task', function () {
    
        var dirList = [];
        var root = "applications";
    
        (function walk(path) {
            var items = fs.readdirSync(path);
            items.forEach(function (item) {
                if (fs.statSync(path + '/' + item).isDirectory()) {
                    dirList.push(path + '/' + item);
                    walk(path + '/' + item);
                }
            });
        })(root);// search all directories
    
        // exclude special directory
        function isExcludeDir(dirName) {
            if (dirName.indexOf('test') > -1) {
                return true;
            }
            if (dirName.indexOf('service') > -1) {
                return true;
            }
            if (dirName.indexOf('css') > -1) {
                return true;
            }
            if (dirName.indexOf('html') > -1) {
                return true;
            }
            if (dirName.indexOf('3rd-lib') > -1) {
                return true;
            }
            return false;
        }
    
        dirList.forEach(function (item, index) {
    
            item = item.substr((root + '/').length);// cut the leading path "applications/", then leaving "employee/static"
    
            if (!isExcludeDir(item)) {
    
                var targetName = "transport.build" + index;// such as 'transport.build0'
    
                grunt.config.set(targetName + '.options.idleading', item + '/');
                grunt.config.set(targetName + '.files', [
                    {
                        cwd: root + '/' + item,
                        src: '*.js',
                        dest: '.build/' + item
                    }
                ]);
    
                grunt.task.run(targetName.replace('.', ':'));
            }
    
        });
    
    });
    

    【讨论】:

      【解决方案2】:

      是的,有;您正在寻找的是 grunt-multi。我已经对此进行了一些尝试,并且为复制任务提供了类似的配置,因此希望这对您有所帮助:

      grunt.initConfig({
          transport: {
              modules: {
                  options: {
                      idleading: '<%= module %>/static/'
                  },
                  files: [
                      {
                          cwd: 'modules/<%= module %>/static',
                          src: '**/*.js',
                          dest: '.build/<%= module %>/static'
                      }
                  ]
              }
          },
          multi: {
              modules: {
                  options: {
                      vars: {
                          modules: {
                              patterns: '*',
                              options: {
                                  cwd: 'modules',
                                  filter: 'isDirectory'
                              } 
                          }
                      },
                      config: {
                          module: '<%= modules %>'
                      },
                      tasks: [ 'transport' ]
                  }
              }
          }
      });
      

      grunt multi 运行您的任务,它将遍历modules 中的所有目录,为每个目录运行传输配置。

      完整文档可在https://github.com/neekey/grunt-multi 获得。希望这会有所帮助。

      【讨论】:

      • 嗨,本,感谢您的帮助。但是,我不确定 grunt-multi 能否完全解决我的问题。因为我在文件结构中有子目录,所以有点复杂
      • 其实,我写了一个自定义的 grunt 任务来解决这个问题,基本思想是使用 nodejs fs.readdirSync() 和 fs.statSync() 函数,遍历目录,然后多次调用 grunt.task.run()。每次我创建一个名为“buildN”的新目标时
      猜你喜欢
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      相关资源
      最近更新 更多