这是我的Gruntfile的修剪版本:
module.exports = function(grunt) {
grunt.config.init({
folder_list: {
options: {
files: false
},
files: {
src: (function() {
// some logic here that gets the folders to parse
}()),
dest: '.tmp/folderlist.json'
}
},
copy: {
// Makes a backup of all the views files
backup: {
files: grunt.folders.backup
}
}
});
grunt.loadNpmTasks('grunt-folder-list');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask('parse_folders', 'Parsing the generated folder list!', function () {
var folderJSON = grunt.file.readJSON('.tmp/folderlist.json'),
backupList = [];
for (var i = 0, total = folderJSON.length; i < total; i++) {
var dir = folderJSON[i].location;
// list of folders for the backup task
backupList[i] = {
expand: true, flatten: true, filter: 'isFile',
src: dir + '/*.php',
dest: dir + '/bak/'
};
}
grunt.folders = {
backup: backupList
};
/* It gives me something like that:
backupList: [
{
expand: true,
flatten: true,
filter: 'isFile',
src: 'Path/To/My/Files/*.php',
dest: 'Path/To/My/Files/bak/'
},
{
expand: true,
flatten: true,
filter: 'isFile',
src: 'Other/Path/To/My/Files/*.php',
dest: 'Other/Path/To/My/Files/bak/'
},
// etc.
]
*/
});
grunt.registerTask('test', 'Test', function () {
console.log(grunt.folders.backup);
});
grunt.registerTask('default', 'Asset Compiler', function () {
grunt.task.run('folder_list');
grunt.task.run('parse_folders');
grunt.task.run('test');
grunt.task.run('copy:backup');
});
};
简而言之:
grunt.folders.backup
包含正确的数据!。“copy:backup”输出
TypeError:无法读取未定义
的属性“backup”
我做错了什么?
答案 0 :(得分:2)
&#34; grunt.folder&#34;不会在&#34; parse_folders&#34;之外访问。任务和咕噜声任务都不符合返回数据的要求。
在任务之间共享信息的一种好方法是使用grunt.config,它是grunt设置的全局键值全局空间,然后以这种方式检索配置&#34;&#39;&lt; %= backupList%&gt;&#39;&#34;。
module.exports = function(grunt) {
grunt.config.init({
folder_list: {
options: {
files: false
},
files: {
src: (function() {
// some logic here that gets the folders to parse
}()),
dest: '.tmp/folderlist.json'
}
},
copy: {
// Makes a backup of all the views files
backup: {
files: '<%= backupList %>'
}
}
});
grunt.loadNpmTasks('grunt-folder-list');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask('parse_folders', 'Parsing the generated folder list!', function () {
var folderJSON = grunt.file.readJSON('.tmp/folderlist.json'),
backupList = [];
for (var i = 0, total = folderJSON.length; i < total; i++) {
var dir = folderJSON[i].location;
// list of folders for the backup task
backupList[i] = {
expand: true, flatten: true, filter: 'isFile',
src: dir + '/*.php',
dest: dir + '/bak/'
};
}
grunt.config.set("backupList", backupList);
/* It gives me something like that:
backupList: [
{
expand: true,
flatten: true,
filter: 'isFile',
src: 'Path/To/My/Files/*.php',
dest: 'Path/To/My/Files/bak/'
},
{
expand: true,
flatten: true,
filter: 'isFile',
src: 'Other/Path/To/My/Files/*.php',
dest: 'Other/Path/To/My/Files/bak/'
},
// etc.
]
*/
});
grunt.registerTask('test', 'Test', function () {
console.log(grunt.folders.backup);
});
grunt.registerTask('default', 'Asset Compiler', function () {
grunt.task.run('folder_list');
grunt.task.run('parse_folders');
grunt.task.run('test');
grunt.task.run('copy:backup');
});
};