我尝试编写metalsmith plugin来重建文件更改文件并通过tiny-lr重新加载页面。
观察和重建工作正常,但是当文件发生变化时,又会再次启动tiny-lr服务器 - 这会导致错误,因为服务器已经在运行。
这是插件代码:
module.exports = plugin;
var gaze = require('gaze'),
chalk = require('chalk'),
tinylr = require('tiny-lr');
function plugin(){
return function(files, metalsmith, done){
var port = 35729,
server = tinylr();
server.listen(port, function(){
console.log(chalk.cyan('Listening on port', port));
});
gaze(['templates/*', 'src/**/*'], function(err, watcher) {
console.log(chalk.green('Watching files'));
this.on('changed', function(filepath) {
console.log(chalk.red(filepath) + chalk.blue(' was changed.'));
console.log(chalk.yellow('Rebuilding files...'));
metalsmith.build(function(err, files){
console.log(chalk.blue('Build successful'))
});
});
});
done();
}
}
更改文件时的控制台输出:
Listening on port 35729
Watching files
/Users/benjamin/metal/src/content/index.md was changed.
Rebuilding files...
... Uhoh. Got error listen EADDRINUSE ...
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at Server.listen (/Users/benjamin/Desktop/metal/node_modules/metalsmith-watchall/node_modules/tiny-lr/lib/server.js:145:15)
at /Users/benjamin/Desktop/metal/node_modules/metalsmith-watchall/index.js:26:12
at next (/Users/benjamin/Desktop/metal/node_modules/metalsmith/node_modules/ware/lib/index.js:68:8)
at /Users/benjamin/Desktop/metal/node_modules/metalsmith-templates/node_modules/async/lib/async.js:119:25
at /Users/benjamin/Desktop/metal/node_modules/metalsmith-templates/node_modules/async/lib/async.js:24:16
at convert (/Users/benjamin/Desktop/metal/node_modules/metalsmith-templates/lib/index.js:67:32)
Watching files
Build successful
为什么服务器第二次启动?
如何防止它被启动?
奖金问题:
为什么控制台消息不符合我期望查看代码的顺序?特别是,为什么" Build sucessful"出现在"看文件后#34;?我认为它应该直接出现在"重建文件......"之后。
答案 0 :(得分:1)
仍然无法解释为什么上面的例子不起作用, 但这是我想出的事情(感谢chuck提醒我发布解决方案......)
module.exports = plugin;
var gaze = require('gaze'),
chalk = require('chalk'),
tinylr = require('tiny-lr');
var watchall = {
running: false
};
function startLiveReloadServer(){
var port = 35729,
server = tinylr();
server.listen(port, function(){
console.log(chalk.cyan('Listening on port', port));
});
watchall.server = server;
};
function startWatcher(metalsmith, files, done){
gaze(['templates/*', 'src/**/*'], function(err, watcher) {
console.log(chalk.green('Watching files'));
this.on('changed', function(filepath) {
console.log(chalk.red(filepath) + chalk.blue(' was changed.'));
console.log(chalk.yellow('Rebuilding files...'));
metalsmith.build(function(err, files){
console.log(chalk.blue('Build successful'))
watchall.server.changed({body:{files:Object.keys(files)}});
});
});
});
done();
};
function plugin(){
return function(files, metalsmith, done){
if ( watchall.running === false ) {
startLiveReloadServer();
startWatcher(metalsmith, files, done);
watchall.running = true;
} else {
done();
};
}
}