This repository has been archived by the owner on Mar 15, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2,065 changed files
with
360,547 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env node | ||
|
||
function help() { | ||
console.log([ | ||
'Usage:', | ||
' commonplace <command>', | ||
'', | ||
'Commands:', | ||
' install Run this command to instantiate commonplace.', | ||
' update This command will update commonplace modules.' | ||
|
||
].join('\n')); | ||
} | ||
|
||
process.title = 'commonplace'; | ||
|
||
var argv = process.argv.slice(2); | ||
if (!argv.length) { | ||
console.error('Not enough arguments passed.'); | ||
help(); | ||
process.exit(); | ||
} | ||
|
||
switch (argv[0]) { | ||
case 'install': | ||
break; | ||
case 'update': | ||
break; | ||
default: | ||
console.error('Unrecognized command "' + argv[0] + '"'); | ||
help(); | ||
process.exit(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
#!/usr/bin/env node | ||
|
||
var fs = require('fs'); | ||
var http = require('http'); | ||
var stylus = require('stylus'); | ||
|
||
var utils = require('../lib/utils.js'); | ||
var opts = utils.opts; | ||
var glob = utils.glob; | ||
var srcdir = fs.existsSync('./hearth/') ? './hearth' : './src'; // For Fireplace compat | ||
|
||
process.title = 'damper'; | ||
|
||
var mimes = { | ||
'css': 'text/css', | ||
'jpg': 'image/jpeg', | ||
'js': 'application/javascript', | ||
'png': 'image/png', | ||
'svg': 'image/svg+xml', | ||
'webapp': 'application/x-web-app-manifest+json', | ||
'woff': 'application/font-woff' | ||
}; | ||
|
||
var opts = opts(process.argv.slice(2), | ||
{host: '0.0.0.0', port: '6666', l10n: false, compile: false}); | ||
|
||
if (!opts.compile) { | ||
// 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(srcdir + '/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 filePath = srcdir + request.url; | ||
fs.exists(filePath, function(exists) { | ||
if (exists && !fs.statSync(filePath).isDirectory()) { | ||
fs.readFile(filePath, function(error, content) { | ||
if (error) { | ||
response.writeHead(500); | ||
response.end(); | ||
console.error(error); | ||
} else { | ||
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'); | ||
} | ||
}); | ||
} else { | ||
writeIndex(); | ||
} | ||
}); | ||
|
||
}).listen(opts.port, opts.host); | ||
|
||
console.log('Server running at http://' + opts.host + ':' + opts.port); | ||
} else { | ||
console.log('Starting compilation...'); | ||
} | ||
|
||
var watched_filepaths = []; | ||
|
||
|
||
function reload() { | ||
watched_filepaths.forEach(fs.unwatchFile); | ||
watched_filepaths = []; | ||
|
||
// "restart" is a special action keyword | ||
//watch('./damper', null, 'restart'); | ||
watch('../lib/compile.js', null, 'nunjucks'); | ||
//watch('../lib', 'js', 'nunjucks'); | ||
|
||
watch(srcdir + '/media/css', 'styl', 'stylus'); | ||
watch(srcdir + '/templates', 'html', 'nunjucks'); | ||
|
||
// When the builder is updated, recompile the templates. | ||
watch(srcdir + '/js/builder.js', null, 'nunjucks'); | ||
} | ||
|
||
var nunjucks_opts = {}; | ||
if (opts.l10n || opts.compile === true || opts.compile === 'l10n') { | ||
nunjucks_opts.l10n = true; | ||
} | ||
function runCommand(command, filepath) { | ||
switch (command) { | ||
case 'restart': | ||
console.log('Restarting...'); | ||
return reload(); | ||
case 'stylus': | ||
filepath = filepath || opts.path; | ||
|
||
var filepathDir = filepath.split('/').slice(0, -1).join('/'); | ||
fs.readFile(filepath, function (err, data) { | ||
data = data.toString(); | ||
if (err) { | ||
console.error(filepath + ' not found!'); | ||
return; | ||
} | ||
stylus(data) | ||
.set('filename', filepath + '.css') | ||
.set('include css', true) | ||
.include(filepathDir) | ||
.render(function(err, css) { | ||
fs.writeFileSync(filepath + '.css', css); | ||
if (err) console.error(err); | ||
}); | ||
}); | ||
break; | ||
case 'nunjucks': | ||
console.log('Recompiling templates...'); | ||
require('../lib/compile').process( | ||
srcdir + '/templates', | ||
srcdir + '/templates.js', | ||
'locale/templates/LC_MESSAGES/messages.pot', | ||
nunjucks_opts | ||
); | ||
break; | ||
} | ||
} | ||
|
||
function watch(globpath, ext, command) { | ||
var cb = function(err, filepaths) { | ||
filepaths.forEach(function(filepath) { | ||
watched_filepaths.push(filepath); | ||
if (command == 'stylus') { | ||
fs.exists(filepath, function(exists) { | ||
if (exists) { | ||
runCommand(command, filepath); | ||
} | ||
}); | ||
} | ||
|
||
// If we're compiling, we don't want to watch the files. | ||
if (opts.compile) { | ||
return; | ||
} | ||
|
||
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); | ||
} | ||
}); | ||
|
||
}); | ||
if (filepaths.length > 1 && !opts.compile) { | ||
console.log('Watching ' + filepaths.length + ' ' + ext + ' files.'); | ||
} | ||
}; | ||
if (globpath.substr(1).indexOf('.') > -1) { | ||
cb(null, [globpath]); | ||
} else { | ||
glob(globpath, ext, cb); | ||
} | ||
} | ||
|
||
if (opts.compile && opts.compile !== true) { | ||
runCommand(opts.compile); | ||
} else { | ||
runCommand('nunjucks'); | ||
reload(); | ||
} | ||
|
Oops, something went wrong.