我需要获取所有名为template /的胡须模板目录,并使用hogan对其进行编译。
从理论上讲,假设他们的名字是
file1.mustache file2.mustache file3.mustache
然后,我们对每个视图进行查看,并将结果保存到名为build /的输出目录中。
从理论上讲,结果名称将是
name.file1 name.file2 name.file3
显然,异步是可取的,但是我对您如何有效地做到这一点最感兴趣?我不敢相信唯一的方法就是对每个文件对象和匿名函数进行处理。
您可以将fs-promise模块与之一起使用,Promise.all
以轻松地并行读取,处理和写入文件:
const fsp = require('fs-promise');
function processTemplate(filename) {
return fsp.readFile(filename, 'utf8')
.then((template) => hogan.compile(template))
.then((compiledTemplate) => fsp.writeFile('path/to/compiled', compiledTemplate));
}
fsp.readdir('./templates')
.then((files) => Promise.all(files.map(processTemplate)))
.catch((error) => console.log(error));
尽管我不确定我是否理解“每个文件对象和匿名函数”的含义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句