reload projects on file change

This commit is contained in:
oleg 2015-06-14 16:40:20 +03:00
parent cc064aafda
commit ec41df4596
4 changed files with 60 additions and 5 deletions

58
app.js
View File

@ -9,7 +9,8 @@ var http = require('http'),
_ = require('underscore'),
reader = require('./lib/reader'),
notifier = require('./lib/notifier'),
project = require('./lib/project');
project = require('./lib/project'),
chokidar = require('chokidar');
var staticServer = new nodeStatic.Server('./static');
var server = http.createServer(function(req, res, next) {
@ -73,11 +74,64 @@ Steppy(
notifier.init(app.config.notify, this.slot());
},
function() {
// load all projects for the first time
project.loadAll(app.config.paths.projects, this.slot());
},
function(err, projects) {
// note that `app.projects` is live variable
app.projects = projects;
console.log('Loaded projects: ', _(projects).pluck('name'));
console.log('Loaded projects: ', _(app.projects).pluck('name'));
// start file watcher for reloading projects on change
var syncProject = function(filename, fileInfo) {
var baseDir = app.config.paths.projects,
projectName = path.relative(
baseDir,
path.dirname(filename)
);
var projectIndex = _(app.projects).findIndex(function(project) {
return project.name === projectName;
});
if (projectIndex !== -1) {
console.log('Unload project: "' + projectName + '"');
app.projects.splice(projectIndex, 1);
}
// on add or change (info is falsy on unlink)
if (fileInfo) {
console.log('Load project "' + projectName + '" on change');
project.load(baseDir, projectName, function(err, project) {
if (err) {
return console.error(
'Error during load project "' + projectName + '": ' +
err.stack || err
);
}
app.projects.push(project);
console.log(
'Project "' + projectName + '" loaded:',
JSON.stringify(project, null, 4)
);
});
}
};
// NOTE: currently after add remove and then add same file events will
// not be emitted
var watcher = chokidar.watch(
path.join(app.config.paths.projects, '*', 'config.*'),
{ignoreInitial: true}
);
watcher.on('add', syncProject);
watcher.on('change', syncProject);
watcher.on('unlink', syncProject);
watcher.on('error', function(err) {
console.error('File watcher error occurred: ', err.stack || err);
});
// init resources
require('./resources')(app);

View File

@ -30,7 +30,7 @@ gulp.task('develop', function() {
return nodemon({
ignore: ['static/**/*.js', 'app/**/*.js', 'node_modules/**'],
script: 'app.js',
ext: 'js,json,yaml'
ext: 'js'
});
});

View File

@ -21,7 +21,7 @@ function Distributor(params) {
callback(null, build);
};
self.projectsHash = _(params.projects).indexBy('name');
self.projects = params.projects;
}
inherits(Distributor, EventEmitter);
@ -163,7 +163,7 @@ Distributor.prototype.run = function(params, callback) {
project;
Steppy(
function() {
project = self.projectsHash[params.projectName];
project = _(self.projects).findWhere({name: params.projectName});
self._updateBuild({}, {
project: project,
params: params.params,

View File

@ -27,6 +27,7 @@
},
"homepage": "https://github.com/okv/nci",
"dependencies": {
"chokidar": "1.0.3",
"data.io": "0.3.0",
"jade": "1.9.2",
"js-yaml": "3.3.1",