简体   繁体   English

如何将目录中自动生成的文件列表添加到JS文件中?

[英]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.

相关问题 如何在Node.js中进行Cragislist电子邮件转发(接收电子邮件到自动生成的电子邮件地址并转发)? - How to do Cragislist email forwarding in Node.js (receive email to auto-generated email address and forward it)? Firebase:如何将自动生成的文档 ID 添加到 svelte 中的文档? - Firebase: how to add auto-generated document id to the document in svelte? 如何使用 add() 获取自动生成的 id? - How to get an auto-generated id by using add()? 如何在ReactJS中制作自动生成的ID - How to make auto-generated ID in ReactJS 将 javascript 自动生成的文件附加到 POST 表单并在 $_FILES 中的 PHP 上处理它 - Append javascript auto-generated file to POST form and process it on PHP in $_FILES Node.js:从PHP URL下载(自动生成的)文件 - Node.js: Download an (auto-generated) file from a PHP URL 从Visual Studio中的TypeScript引用自动生成的JavaScript文件 - Referencing auto-generated JavaScript files from TypeScript in Visual Studio Webpack:无限循环和自动生成的文件 - Webpack: Infinite watch loop and auto-generated files 替代eval()从服务器执行自动生成的JS代码 - Alternate for eval() to execute auto-generated JS code from the server 使用 node.js puppeteer 定位自动生成的元素 - Targeting auto-generated elements with node.js puppeteer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM