Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gruntjs 是否可以在Grunt任务中提取文件名(使用Grunt cmd传输插件)_Gruntjs - Fatal编程技术网

Gruntjs 是否可以在Grunt任务中提取文件名(使用Grunt cmd传输插件)

Gruntjs 是否可以在Grunt任务中提取文件名(使用Grunt cmd传输插件),gruntjs,Gruntjs,我读了大量的官方文件和这篇文章 不幸的是,我没有得到答案 我有以下文件结构: src/ modules/ module1/ static/ abc.js def.js ...... module2/ static/ 123.js 456.js

我读了大量的官方文件和这篇文章

不幸的是,我没有得到答案

我有以下文件结构:

src/
    modules/
        module1/
            static/
                abc.js
                def.js
                ......
        module2/
            static/
                123.js
                456.js
                ......
这是我的Grunfile片段:

        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”这样的配置


请给我看解决方案目录,谢谢

是的,有;你要找的是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.initConfig({
运输:{
模块:{
选项:{
idleading:“/static/”
},
档案:[
{
cwd:'模块//静态',
src:“***.js”,
目标:'.build//static'
}
]
}
},
多重:{
模块:{
选项:{
变量:{
模块:{
模式:“*”,
选项:{
cwd:‘模块’,
筛选器:“isDirectory”
} 
}
},
配置:{
模块:“”
},
任务:[“传输”]
}
}
}
});
grunt multi
的身份运行任务,它将迭代
模块
中的所有目录,为每个目录运行传输配置


完整的文档可从以下网址获得:。希望这有帮助。

很抱歉回答我自己的问题。但是我写了一个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('.', ':'));
        }

    });

});

嗨,本,谢谢你的帮助。然而,我不确定grunt multi能否完全解决我的问题。因为我在文件结构中有子目录,所以有点复杂实际上,我编写了一个自定义grunt任务来解决这个问题,基本思想是使用nodejs fs.readdirSync()和fs.statSync()函数遍历目录,然后多次调用grunt.task.run()。每次我创建一个名为“buildN”的新目标时