[英]How do I add auto-generated list of files in a directory to a JS file?
I'm writing an online game in HTML5. 我正在用HTML5编写一个在线游戏。 One of the files contains a list of resources which are all in
resources/img/
folder. 其中一个文件包含资源列表,这些资源都在
resources/img/
文件夹中。 Now I'd like this list to be automatically generated based on contents of this folder, rather than having to update it manually every time I add a new image. 现在我想根据此文件夹的内容自动生成此列表,而不是每次添加新图像时都必须手动更新。 I'm pretty sure that Grunt can do this, but I don't know how to configure it properly.
我很确定Grunt可以做到这一点,但我不知道如何正确配置它。 What should I do to have Grunt generate something like this?
我应该怎么做让Grunt生成这样的东西?
resources = ['a.jpg', 'b.png', 'subfolder/c.png'];
ng-boilerplate does something like it. ng-boilerplate做了类似的事情。
It uses a template for script and stylesheets 它使用脚本和样式表的模板
<!-- compiled CSS --><% styles.forEach( function ( file ) { %>
<link rel="stylesheet" type="text/css" href="<%= file %>" /><% }); %>
<!-- compiled JavaScript --><% scripts.forEach( function ( file ) { %>
<script type="text/javascript" src="<%= file %>"></script><% }); %>
And then a custom grunt task to process template according to files 然后根据文件处理模板的自定义grunt任务
return grunt.template.process( contents, {
data: {
scripts: jsFiles,
styles: cssFiles,
version: grunt.config( 'pkg.version' )
}
});
ng-boilerplate files are listed through a config file, but you could also use a Globbing pattern or even grunt.file API ng-boilerplate文件通过配置文件列出,但您也可以使用Globbing模式甚至grunt.file API
You absolutely can do this. 你绝对可以做到这一点。 It's not really that difficult, but you will need a custom task and to use the Node FileSystem API .
这并不是那么困难,但您需要一个自定义任务并使用Node FileSystem API 。 Try something like this:
尝试这样的事情:
grunt.registerTask("filelist", "Lists files in a directory as JSON string to file", function() {
var list,
done = this.async(),
filename = grunt.config("filelist.images.filename"),
directory = grunt.config("filelist.images.directory");
list = fs.readdirSync(directory);
fs.writeFile(filename, JSON.stringify(list), function (err) {
if (err) { throw err; }
grunt.log.writeln("file list from " + directory + " saved to " + filename);
done();
});
});
In your config section, place this: 在您的配置部分中,放置以下内容:
filelist: {
images: {
filename: "image.json",
directory: "images"
}
}
And at the top of your file place this: 在文件的顶部放置:
var fs = require("fs");
And call it like so: 并称之为:
grunt filelist:images
I did something similar to what jakerella suggested, but I decided to use globbing patterns (as suggested by Florian F.). 我做了类似于jakerella建议的事情,但我决定使用globbing模式(如Florian F.所建议的那样)。 I added a new Grunt task:
我添加了一个新的Grunt任务:
var fs = require("fs");
grunt.registerTask("filelist", "Lists files in a directory as JSON string to file", function() {
var list,
done = this.async(),
output = grunt.config("filelist.images.output"),
outputVariable = grunt.config("filelist.images.outputVariable"),
patterns = grunt.config("filelist.images.patterns"),
headerComment = '/* This file was auto-generated using Grunt task `filelist`. */\n';
list = grunt.file.expand(patterns);
fs.writeFile(output,
headerComment + outputVariable + ' = ' + JSON.stringify(list) + ';',
function (err) {
if (err) {
throw err;
}
grunt.log.writeln("List of " + list.length + " files saved to " + output);
done();
});
});
And I added this to my config: 我把它添加到我的配置中:
filelist: {
images: {
output: "app/generated/image-list.js",
outputVariable: "game.imageList",
patterns: ["app/resources/img/**/*"],
},
},
This way I can include the data with a <script>
tag, just like any other JS. 这样我可以使用
<script>
标记包含数据,就像任何其他JS一样。 Thank you both, guys! 谢谢你们两个,伙计们! :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.