我目前正在用Windows中的node.js构建一个项目。我正在使用一个批处理文件通过命令行组装资源和构建jade模板。对于Jade,我使用开关-o来定义一个JS对象,该对象填充模板中的本地化内容
有一段时间,一切都运行得很好。但是,对JSON查找的更改导致了一个错误:“输入行太长”
研究这个错误时,我发现windows shell对行的长度是有限制的。不幸的是,我需要我的项目的整个查找对象。但是,我开始怀疑jade是否可以接受我的查找文件的路径,而不是包含文件内容的字符串。目前,我正在将内容构建到一个变量中,并使用该ala调用jade:
SetLocal EnableDelayedExpansion
set content=
for /F "delims=" %%i in (%sourcedir%\assets\english.json) do set content=!content! %%i
::use the json file as a key for assembling the jade templates
call jade %sourcedir% --out %destdir% -o"%content%"
EndLocal如果我可以使用查找文件的路径,就会容易得多。然而,我习惯于如何做到这一点(如果它甚至是可能的)。而且Jade的文档也有些不足。
因此,简而言之,Jade是否可以接受JS对象的文件路径,而不是包含该对象的字符串?有没有更好的方法来构造jade call,而不会超过限制呢?
发布于 2012-08-16 14:56:53
编写一个node.js脚本,它将读取您的“资产”并调用jade。类似于:
var fs = require('fs'),
_ = require('underscore'),
async = require('async');
var sourceDir = 'path to the directory with your jade templates',
destinationDir = 'path to the directory where you want the result html files to be contained in';
async.waterfall([
async.parallel.bind(null, {
serializedData: fs.readFile.bind(null, 'assets/english.json'),
files: fs.readDir.bind(null, sourceDir),
}),
function (result, callback) {
var data = JSON.parse(result.serializedData),
files = result.files;
async.parallel(_.map(files, function (file) {
return async.waterfall.bind(null, [
fs.readFile.bind(null, sourceDir + file),
function (jadeSource, callback) {
process.nextTick(callback.bind(null, jade.compile(jadeSource)(data)));
},
fs.writeFile.bind(null, destinationDir + file)
]);
}), callback);
}
], function (err) {
if (err) {
console.log("An error occured: " + err);
} else {
console.log("Done!");
}
});然后在您的批处理文件中直接调用此脚本,而不是枚举目录并手动调用jade。
它不仅能解决你的问题,还能更快地工作,因为:
在parallel;
https://stackoverflow.com/questions/11889194
复制相似问题