我想将整个文件夹添加到我的包中。可以在package.js文件中使用api.add_files添加整个文件夹,而不是单独添加每个文件?也许是这样的事情:
Package.on_use(function(api) {
api.add_files(["files/*","client");
});
答案 0 :(得分:17)
我认为目前公共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来处理文件系统。