coffeescript编译器的--watch指令不适用于node.js 0.4.7。怎么解决?
(目前在Heroku上部署需要0.4.7节点。)
答案 0 :(得分:1)
node.js 0.4.7中没有fs.watch方法。使用以下命令替换command.js中第198行的监视功能:
watch = function(source, base) {
return fs.stat(source, function(err, prevStats) {
if (err) throw err;
return fs.watchFile(source, function(curr, prev) {
return fs.stat(source, function(err, stats) {
if (err) throw err;
if (stats.size === prevStats.size && stats.mtime.getTime() === prevStats.mtime.getTime()) {
return;
}
prevStats = stats;
return fs.readFile(source, function(err, code) {
if (err) throw err;
return compileScript(source, code.toString(), base);
});
});
});
});
};
答案 1 :(得分:0)
如果无法升级到Node 0.6,请尝试Jitter。它是一个单用途的命令行实用程序,可以执行CoffeeScript 1.1.3之前coffee -cw
所做的操作。此外,它还有一些额外功能,例如编译失败时的Growl通知。
答案 2 :(得分:0)
这是我在与coffeescript -w选项无法正常工作相同的经验之后编写的一个watchscript。它查找并监视.coffee,.less,.jade文件以进行更改,并触发相应的编译器作为响应。生成彩色输出,以便在编译失败时轻松检测。
/*
* Tested on node 0.6, Ubuntu
*/
#!/usr/bin/node
COLOR_GREEN="\033[01;32m"
COLOR_RED="\033[01;31m"
COLOR_YELLOW="\033[01;33m"
COLOR_BLUE="\033[01;34m"
COLOR_NORMAL="\033[00m"
var fs = require('fs');
var coffeescript, jade, less;
try {
coffeescript = require('/usr/lib/node_modules/coffee-script'); // after npm -g install coffee-script
} catch(e) {
console.log(COLOR_RED,String(e),COLOR_NORMAL);
}
try {
jade = require('/usr/lib/node_modules/jade'); // after npm -g install jade
} catch(e) {
console.log(COLOR_RED,String(e),COLOR_NORMAL);
}
try {
less = require('/usr/lib/node_modules/less'); // after npm -g install less
} catch(e) {
console.log(COLOR_RED,String(e),COLOR_NORMAL);
}
console.log(
'Watch Support',
(coffeescript ? COLOR_GREEN : COLOR_RED),' coffeescript ',COLOR_NORMAL,
(jade ? COLOR_GREEN : COLOR_RED),' jade ',COLOR_NORMAL,
(less ? COLOR_GREEN : COLOR_RED),' less ',COLOR_NORMAL
);
var compile = function (fname) {
var source = fs.readFileSync(fname).toString();
try {
if(/\.jade$/.test(fname)) {
var html = jade.compile(source,{pretty:true})();
var htmlfname = fname.replace(/\.jade$/,'.html');
fs.writeFileSync(htmlfname, html);
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL)
} else if(/\.coffee$/.test(fname)) {
var js = coffeescript.compile(source);
var jsfname = fname.replace(/\.coffee$/,'.js');
fs.writeFileSync(jsfname, js);
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL)
} else if(/\.less$/.test(fname)) {
var lessparser = new(less.Parser);
var cssfname = fname.replace(/\.less$/,'.css');
lessparser.parse(source, function (e, tree) {
if(e) {
console.log(COLOR_RED,'Failed ',fname,String(e),COLOR_NORMAL);
} else {
fs.writeFileSync(cssfname, tree.toCSS());
console.log(COLOR_GREEN,'Success ',fname,new Date(),COLOR_NORMAL)
}
});
}
} catch(e) {
console.log(COLOR_RED,'Failed ',fname,String(e),COLOR_NORMAL)
}
}
var walk = function(dir, done) {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
};
function main() {
if(process.argv.length > 2) {
dirName = process.argv[2];
} else {
dirName = '.';
}
stats = fs.statSync(dirName);
if(stats.isDirectory()) {
walk(dirName,function (err, files) {
var watching = 0;
files.forEach(function (fname) {
if(/\.coffee$|\.less$|\.jade$/.test(fname)) {
fs.watchFile(fname,{persistent:true,interval:500},
function (cur,prev) {
if(cur.mtime.getTime() != prev.mtime.getTime()) {
compile(fname);
}
}
);
watching++;
}
});
if(watching) {
console.log('Watching '+watching+' file(s) in '+
COLOR_YELLOW+dirName+COLOR_NORMAL);
} else {
console.log('No coffee/jade/less files found to watch');
}
});
} else {
// It's a file
var fname = dirName;
if(/\.coffee$|\.less$|\.jade$/.test(fname)) {
fs.watchFile(fname,{persistent:true,interval:500},
function (cur,prev) {
if(cur.mtime.getTime() != prev.mtime.getTime()) {
compile(fname);
}
}
);
console.log('Watching '+COLOR_YELLOW+fname+COLOR_NORMAL);
} else {
console.log('No coffee/jade/less files found to watch');
}
}
}
main();
如果您更喜欢要点:https://gist.github.com/3083080