nci/app.js

147 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-04-10 19:23:52 +00:00
'use strict';
2015-05-12 21:07:03 +00:00
var http = require('http'),
nodeStatic = require('node-static'),
jade = require('jade'),
path = require('path'),
2015-05-18 18:25:08 +00:00
fs = require('fs'),
Steppy = require('twostep').Steppy,
_ = require('underscore'),
2015-05-21 21:09:16 +00:00
reader = require('./lib/reader'),
2015-06-13 18:59:32 +00:00
notifier = require('./lib/notifier'),
2015-06-14 13:40:20 +00:00
project = require('./lib/project'),
chokidar = require('chokidar');
2015-04-10 19:23:52 +00:00
var staticServer = new nodeStatic.Server('./static');
var server = http.createServer(function(req, res, next) {
2015-04-10 19:23:52 +00:00
// serve index for all app pages
2015-04-10 20:17:03 +00:00
if (req.url.indexOf('/data.io.js') === -1) {
2015-05-09 20:19:25 +00:00
if (!req.url.match(/(js|css|fonts)/)) {
2015-04-10 20:17:03 +00:00
// Compile a function
var index = jade.compileFile(__dirname + '/views/index.jade');
res.write(index());
res.end();
} else {
2015-04-10 19:23:52 +00:00
staticServer.serve(req, res);
}
}
});
var socketio = require('socket.io')(server);
var dataio = require('./dataio')(socketio);
2015-04-10 19:23:52 +00:00
var app = {
server: server,
dataio: dataio
};
2015-04-10 19:23:52 +00:00
2015-05-18 20:27:02 +00:00
app.lib = {};
app.lib.reader = reader;
2015-05-21 21:09:16 +00:00
app.lib.notifier = notifier;
2015-05-12 21:07:03 +00:00
2015-05-18 18:25:08 +00:00
Steppy(
function() {
app.config = {};
app.config.paths = {};
// path to root dir (with projects, builds etc)
app.config.paths.data = path.join(process.cwd(), 'data');
app.config.paths.projects = path.join(app.config.paths.data, 'projects');
app.config.paths.builds = path.join(app.config.paths.data, 'builds');
var stepCallback = this.slot();
fs.exists(app.config.paths.builds, function(isExists) {
stepCallback(null, isExists);
});
},
2015-05-18 20:27:02 +00:00
function(err, isBuildsDirExists) {
2015-05-18 18:25:08 +00:00
if (!isBuildsDirExists) {
fs.mkdir(app.config.paths.builds, this.slot());
2015-05-18 20:27:02 +00:00
} else {
this.pass(null);
2015-05-18 18:25:08 +00:00
}
2015-05-12 21:07:03 +00:00
2015-05-18 20:27:02 +00:00
// register plugins
require('./lib/reader/yaml').register(app);
2015-05-21 21:09:16 +00:00
require('./lib/notifier/console').register(app);
2015-05-18 20:27:02 +00:00
reader.load(app.config.paths.data, 'config', this.slot());
},
function(err, mkdirResult, config) {
2015-05-18 18:25:08 +00:00
_(app.config).defaults(config);
2015-05-18 20:27:02 +00:00
console.log('Server config:', JSON.stringify(app.config, null, 4));
2015-05-18 18:25:08 +00:00
2015-05-21 21:09:16 +00:00
notifier.init(app.config.notify, this.slot());
2015-06-13 18:59:32 +00:00
},
function() {
2015-06-14 13:40:20 +00:00
// load all projects for the first time
2015-06-13 18:59:32 +00:00
project.loadAll(app.config.paths.projects, this.slot());
},
function(err, projects) {
2015-06-14 13:40:20 +00:00
// note that `app.projects` is live variable
2015-06-13 18:59:32 +00:00
app.projects = projects;
2015-06-14 13:40:20 +00:00
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 + '": ',
2015-06-14 13:40:20 +00:00
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);
});
2015-05-21 21:09:16 +00:00
2015-05-18 20:27:02 +00:00
// init resources
2015-05-18 18:25:08 +00:00
require('./resources')(app);
require('./httpApi').register(app);
2015-05-18 18:25:08 +00:00
},
function(err) {
if (err) throw err;
}
);
app.server.listen(3000);