Permalink
Browse files

Added centralised logging, made tests quiet

  • Loading branch information...
1 parent d011022 commit eb68be05729c942d425955905220550359906312 Alex Young committed Jul 29, 2011
Showing with 61 additions and 35 deletions.
  1. +0 −1 TODO.textile
  2. +3 −1 bin/pop
  3. +5 −4 lib/cli_tools.js
  4. +4 −3 lib/config.js
  5. +2 −1 lib/file_map.js
  6. +3 −2 lib/generators/default.js
  7. +11 −0 lib/log.js
  8. +24 −19 lib/pop.js
  9. +5 −4 lib/server.js
  10. +4 −0 test/cli_tools.test.js
View
1 TODO.textile
@@ -2,7 +2,6 @@ h3. Features
* nib (to make it easier to share styles?)
* Mock filesystem for tests so I don't need to write to /tmp
-* Shared logging so I can turn it off during tests
* Make template language configurable, default to jade
* If enough people get confused about manually installing Express, I'll make it a dependency for the optional dev server mode
* Autogenerate new files, generate index pages when posts change
View
4 bin/pop
@@ -1,3 +1,5 @@
#!/usr/bin/env node
-require(__dirname + '/../lib/pop')();
+require(__dirname + '/../lib/pop').build();
+
+module.exports.id = 'POP_BIN';
View
9 lib/cli_tools.js
@@ -10,6 +10,7 @@
var yamlish = require('yamlish')
, path = require('path')
, fs = require('fs')
+ , log = require(__dirname + '/log')
, generators = require(__dirname + '/generators');
module.exports = {
@@ -69,10 +70,10 @@ module.exports = {
fs.writeFile(path.join(config.root, fileName), frontMatter, function(err) {
if (err) {
- console.error('Error writing file:', fileName);
+ log.error('Error writing file:', fileName);
throw(err);
}
- console.log('Post created:', fileName);
+ log.info('Post created:', fileName);
fn();
});
},
@@ -177,7 +178,7 @@ module.exports = {
try {
var pluginGenerator = require(generator);
} catch (e) {
- console.error('Error: Unable to find the', generator, 'generator');
+ log.error('Error: Unable to find the', generator, 'generator');
return;
}
@@ -203,7 +204,7 @@ module.exports = {
siteBuilder.fileMap = fileMap;
siteBuilder.build();
siteBuilder.on('ready', function() {
- console.log('%d files rendered.', fileMap.files.length);
+ log.info('%d files rendered.', fileMap.files.length);
});
}
});
View
7 lib/config.js
@@ -32,10 +32,10 @@ function readConfigFile(file) {
return applyDefaults(JSON.parse(data));
} catch(exception) {
if (exception.code === 'EBADF') {
- console.error('No _config.json file in this directory. Is this a Pop site?');
+ log.error('No _config.json file in this directory. Is this a Pop site?');
process.exit(1);
} else {
- console.log('Error reading config:', exception.message);
+ log.info('Error reading config:', exception.message);
throw(exception);
}
}
@@ -44,6 +44,7 @@ function readConfigFile(file) {
/**
* Module dependencies and additional config variables.
*/
-var fs = require(__dirname + '/graceful');
+var fs = require(__dirname + '/graceful')
+ , log = require(__dirname + '/log');
module.exports = readConfigFile;
View
3 lib/file_map.js
@@ -9,6 +9,7 @@
*/
var fs = require(__dirname + '/graceful')
, path = require('path')
+ , log = require(__dirname + '/pop').log
, EventEmitter = require('events').EventEmitter;
/**
@@ -68,7 +69,7 @@ FileMap.prototype.walk = function(dir) {
file = path.join(dir, file);
self.filesLeft++;
fs.stat(file, function(err, stats) {
- if (err) console.log('Error:', err);
+ if (err) log.error('Error:', err);
if (!stats) return;
if (stats.isDirectory(file)) {
self.filesLeft--;
View
5 lib/generators/default.js
@@ -1,5 +1,6 @@
var fs = require('fs')
- , path = require('path');
+ , path = require('path')
+ , log = require(__dirname + '/../log');
var generator = {
run: function(helpers, pathName, fn) {
@@ -18,7 +19,7 @@ var generator = {
fs.writeFileSync(path.join(pathName, 'stylesheets', 'screen.styl'), helpers.defaultStylus());
fs.writeFileSync(path.join(pathName, postFileName), helpers.samplePost());
- console.log('Site created:', pathName);
+ log.info('Site created:', pathName);
fn();
}
};
View
11 lib/log.js
@@ -0,0 +1,11 @@
+module.exports = {
+ enabled: true,
+
+ info: function() {
+ if (this.enabled) console.log.apply(this, arguments);
+ },
+
+ error: function() {
+ if (this.enabled) console.error.apply(this, arguments);
+ }
+};
View
43 lib/pop.js
@@ -13,17 +13,7 @@ var path = require('path')
, SiteBuilder = require(__dirname + '/site_builder')
, cliTools = require(__dirname + '/cli_tools')
, readConfig = require(__dirname + '/config')
- , usage
- , args = process.argv.slice(2);
-
-usage = 'pop is a static site builder.\n\n';
-usage += 'Usage: pop [command] [options]\n';
-usage += 'new path Generates a new site at path/\n';
-usage += 'post "Post Title" Writes a new post file\n';
-usage += 'render pattern Renders files that match "pattern"\n';
-usage += 'server [port] Create a server on port (default: 4000) for _site/\n\n';
-usage += '-v, --version Display version and exit\n';
-usage += '-h, --help Shows this message\n';
+ , log = require(__dirname + '/log');
/**
* Loads the config script and sets the local variable.
@@ -76,9 +66,20 @@ function generateSite(config, useServer) {
return siteBuilder;
}
-module.exports = function() {
+function build() {
+ var args = process.argv.slice(2)
+ , usage;
if (args.length === 0) return loadConfigAndGenerateSite();
+ usage = 'pop is a static site builder.\n\n';
+ usage += 'Usage: pop [command] [options]\n';
+ usage += 'new path Generates a new site at path/\n';
+ usage += 'post "Post Title" Writes a new post file\n';
+ usage += 'render pattern Renders files that match "pattern"\n';
+ usage += 'server [port] Create a server on port (default: 4000) for _site/\n\n';
+ usage += '-v, --version Display version and exit\n';
+ usage += '-h, --help Shows this message\n';
+
while (args.length) {
arg = args.shift();
switch (arg) {
@@ -97,20 +98,24 @@ module.exports = function() {
case '-v':
case '--version':
var version = JSON.parse(fs.readFileSync(__dirname + '/../package.json')).version;
- console.log('pop version:', version);
+ log.info('pop version:', version);
process.exit(0);
break;
case '-h':
case '--help':
- console.log(usage);
+ log.info(usage);
process.exit(1);
default:
loadConfigAndGenerateSite();
}
}
-};
+}
-module.exports.SiteBuilder = SiteBuilder;
-module.exports.FileMap = FileMap;
-module.exports.generateSite = generateSite;
-module.exports.cliTools = cliTools;
+module.exports = {
+ build: build
+, SiteBuilder: SiteBuilder
+, FileMap: FileMap
+, generateSite: generateSite
+, cliTools: cliTools
+, log: log
+};
View
9 lib/server.js
@@ -9,7 +9,8 @@
*/
var path = require('path')
, watch = require('nodewatch')
- , siteBuilder;
+ , siteBuilder
+ , log = require(__dirname + '/log');
/**
* Instantiates and runs the Express server.
@@ -37,7 +38,7 @@ function server() {
// TODO: Config
app.listen(siteBuilder.config.port);
- console.log('Listening on port', siteBuilder.config.port);
+ log.info('Listening on port', siteBuilder.config.port);
}
/**
@@ -46,11 +47,11 @@ function server() {
*/
function watchChanges() {
function buildChange(file) {
- console.log('File changed:', file);
+ log.info('File changed:', file);
try {
siteBuilder.buildChange(file);
} catch (e) {
- console.log('Error building site:', e);
+ log.error('Error building site:', e);
}
}
View
4 test/cli_tools.test.js
@@ -1,10 +1,14 @@
var assert = require('assert')
, path = require('path')
+ , log = require(__dirname + '/../lib/log')
, fs = require(__dirname + '/../lib/graceful')
, pop = require(__dirname + '/../lib/pop')
, cliTools = pop.cliTools
, config;
+// Be quiet when testing
+log.enabled = false;
+
config = {
permalink: '/:year/:month/:day/:title'
, root: '/tmp/pop-tests/'

0 comments on commit eb68be0

Please sign in to comment.