我想将整个文件文件夹添加到我的程序包中。除了可以单独添加每个文件之外,还可以使用package.js文件中的api.add_files添加整个文件文件夹吗?也许像这样:

Package.on_use(function(api) {

api.add_files(["files/*","client");

});

最佳答案

我认为公共(public)API中目前没有类似的东西。

但是,您可以使用普通的旧Node.JS来完成您想做的事情。

我们的包结构如下所示:

/packages/my-package
    |-> client
    |   |-> nested
    |   |   |-> file3.js
    |   |-> file1.js
    |   |-> file2.js
    |-> my-package.js
    |-> package.js

我们建立一个辅助函数,如下所示:
function getFilesFromFolder(packageName,folder){
    // local imports
    var _=Npm.require("underscore");
    var fs=Npm.require("fs");
    var path=Npm.require("path");
    // helper function, walks recursively inside nested folders and return absolute filenames
    function walk(folder){
        var filenames=[];
        // get relative filenames from folder
        var folderContent=fs.readdirSync(folder);
        // iterate over the folder content to handle nested folders
        _.each(folderContent,function(filename){
            // build absolute filename
            var absoluteFilename=folder+path.sep+filename;
            // get file stats
            var stat=fs.statSync(absoluteFilename);
            if(stat.isDirectory()){
                // directory case => add filenames fetched from recursive call
                filenames=filenames.concat(walk(absoluteFilename));
            }
            else{
                // file case => simply add it
                filenames.push(absoluteFilename);
            }
        });
        return filenames;
    }
    // save current working directory (something like "/home/user/projects/my-project")
    var cwd=process.cwd();
    // chdir to our package directory
    process.chdir("packages"+path.sep+packageName);
    // launch initial walk
    var result=walk(folder);
    // restore previous cwd
    process.chdir(cwd);
    return result;
}

您可以像这样使用它:
Package.on_use(function(api){
    var clientFiles=getFilesFromFolder("my-package","client");
    // should print ["client/file1.js","client/file2.js","client/nested/file3.js"]
    console.log(clientFiles);
    api.add_files(clientFiles,"client");
});

我们仅使用Node.JS fs utils来处理文件系统。

关于 meteor 包api.add_files添加整个文件夹,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20793505/

10-12 12:35
查看更多