зеркало из https://github.com/mozilla/commonplace.git
173 строки
4.9 KiB
JavaScript
Executable File
173 строки
4.9 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
var fs = require('fs');
|
|
var http = require('http');
|
|
var path = require('path');
|
|
|
|
var stylus = require('stylus');
|
|
|
|
var utils = require('../lib/utils.js');
|
|
var info = require('../lib/info.js');
|
|
|
|
var src_dir = info.src_dir();
|
|
|
|
process.title = 'damper';
|
|
|
|
var mimes = {
|
|
'appcache': 'text/cache-manifest',
|
|
'css': 'text/css',
|
|
'jpg': 'image/jpeg',
|
|
'js': 'application/javascript',
|
|
'json': 'application/json',
|
|
'png': 'image/png',
|
|
'svg': 'image/svg+xml',
|
|
'webapp': 'application/x-web-app-manifest+json',
|
|
'woff': 'application/font-woff'
|
|
};
|
|
|
|
info.check_version(
|
|
src_dir,
|
|
null,
|
|
function(them, us) {
|
|
console.warn('Commonplace version does not match: ' + them + ' does not match ' + us);
|
|
},
|
|
function() {
|
|
console.error('No commonplace installation found.');
|
|
process.exit(1);
|
|
}
|
|
);
|
|
|
|
// Get the damperrc data.
|
|
var rc_path = path.normalize(process.env.HOME + '/.damperrc');
|
|
var rc_data = {};
|
|
var app_rc_data;
|
|
if (fs.existsSync(rc_path)) {
|
|
console.log('Loading .damperrc');
|
|
var home_path = src_dir.replace(process.env.HOME, '~');
|
|
rc_data = JSON.parse(fs.readFileSync(rc_path));
|
|
app_rc_data = rc_data[src_dir] ||
|
|
rc_data[home_path] ||
|
|
rc_data[path.dirname(src_dir)] ||
|
|
rc_data[path.dirname(home_path)];
|
|
|
|
if (app_rc_data) {
|
|
console.log('Loaded app settings from .damperrc');
|
|
}
|
|
}
|
|
app_rc_data = app_rc_data || {};
|
|
|
|
// Get the manifest data.
|
|
var manifest_data = info.manifest(src_dir);
|
|
|
|
var default_settings = {
|
|
host: manifest_data.host || app_rc_data.host || '0.0.0.0',
|
|
port: manifest_data.port || app_rc_data.port || '8675'
|
|
};
|
|
|
|
var opts = utils.opts(process.argv.slice(2), default_settings);
|
|
|
|
// Here's the local server.
|
|
http.createServer(function(request, response) {
|
|
|
|
var now = new Date();
|
|
|
|
console.log(
|
|
'[' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds() + '] ' +
|
|
request.url);
|
|
|
|
function writeIndex() {
|
|
fs.readFile(path.resolve(src_dir, 'index.html'), function(error, content) {
|
|
// We'll assume that you don't delete index.html.
|
|
response.writeHead(200, {'Content-Type': 'text/html'});
|
|
response.end(content, 'utf-8');
|
|
});
|
|
}
|
|
|
|
if (request.url == '/') {
|
|
return writeIndex();
|
|
}
|
|
|
|
var qindex;
|
|
if ((qindex = request.url.indexOf('?')) !== -1) {
|
|
request.url = request.url.substr(0, qindex);
|
|
}
|
|
|
|
var url_path = request.url;
|
|
if (url_path[0] === '/') {
|
|
url_path = url_path.substr(1);
|
|
}
|
|
var filePath = path.resolve(src_dir, url_path);
|
|
fs.exists(filePath, function(exists) {
|
|
if (!exists || fs.statSync(filePath).isDirectory()) {
|
|
writeIndex();
|
|
return;
|
|
}
|
|
|
|
fs.readFile(filePath, function(err, content) {
|
|
if (err) {
|
|
response.writeHead(500);
|
|
response.end();
|
|
console.error(err);
|
|
return;
|
|
}
|
|
var dot = request.url.lastIndexOf('.');
|
|
if (dot > -1) {
|
|
var extension = request.url.substr(dot + 1);
|
|
response.writeHead(200, {'Content-Type': mimes[extension]});
|
|
}
|
|
|
|
response.end(content, 'utf-8');
|
|
});
|
|
});
|
|
|
|
}).listen(opts.port, opts.host);
|
|
|
|
console.log('Server running at http://' + opts.host + ':' + opts.port);
|
|
|
|
|
|
function runCommand(command, filepath) {
|
|
switch (command) {
|
|
case 'stylus':
|
|
require('../lib/build').stylus(filepath, function(err, css) {
|
|
fs.writeFileSync(filepath + '.css', css);
|
|
if (err) console.error(err);
|
|
});
|
|
break;
|
|
case 'nunjucks':
|
|
console.log('Recompiling templates...');
|
|
require('../lib/commonplace').compile({silent: true, only: ['nunjucks']});
|
|
break;
|
|
}
|
|
}
|
|
|
|
function watch(globpath, ext, command) {
|
|
fs.exists(globpath, function(exists) {
|
|
if (!exists) {
|
|
console.warn('Skipping absent path: ' + globpath);
|
|
return;
|
|
}
|
|
|
|
var count = 0;
|
|
utils.globEach(globpath, ext, function(filepath) {
|
|
count++;
|
|
if (command == 'stylus') {
|
|
runCommand(command, filepath);
|
|
}
|
|
|
|
fs.watchFile(filepath, {interval: 250}, function(curr, prev) {
|
|
if (curr.mtime.valueOf() !== prev.mtime.valueOf() ||
|
|
curr.ctime.valueOf() !== prev.ctime.valueOf()) {
|
|
console.warn('> ' + filepath + ' changed.');
|
|
runCommand(command, filepath);
|
|
}
|
|
});
|
|
}, function() {
|
|
console.log('Watching ' + count + ' `' + ext + '` files.');
|
|
});
|
|
});
|
|
}
|
|
|
|
runCommand('nunjucks');
|
|
watch(path.resolve(src_dir, 'media/css'), 'styl', 'stylus');
|
|
watch(path.resolve(src_dir, 'templates'), 'html', 'nunjucks');
|