Skip to content

Commit

Permalink
dumb down the cli, remove options
Browse files Browse the repository at this point in the history
  • Loading branch information
ianstormtaylor committed Mar 2, 2014
1 parent f27ed1d commit c1b8297
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ node_modules: package.json
@npm install

test: node_modules
@./node_modules/.bin/mocha --reporter spec
@./node_modules/.bin/mocha --reporter spec --slow 300

.PHONY: test
51 changes: 34 additions & 17 deletions bin/metalsmith
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#!/usr/bin/env node

var colors = require('colors');
var exists = require('fs').existsSync;
var Metalsmith = require('..');
var program = require('commander');
var resolve = require('path').resolve;

/**
* Program.
* Config.
*/

program
.option('-c, --config <file>', 'set an alternate config file')
.parse(process.argv);

/**
* Settings.
*/

var config = resolve(process.cwd(), program.config || 'metalsmith.json');
var json = exists(config) ? require(config) : {};
var config = resolve(process.cwd(), 'metalsmith.json');
if (!exists(config)) console.error('Could not find a "metalsmith.json" configuration file.');
var json = require(config);

/**
* Metalsmith.
Expand All @@ -40,8 +33,7 @@ for (var key in json.plugins) {
try {
plugin = require(key);
} catch (e) {
console.error('failed to require plugin "' + key + '"');
process.exit(1);
fatal('Failed to require plugin "' + key + '".');
}

metalsmith.use(plugin(opts));
Expand All @@ -52,6 +44,31 @@ for (var key in json.plugins) {
*/

metalsmith.build(function(err){
if (err) return console.error(err);
console.log('built');
});
if (err) return fatal(err.message);
log('Successfully built to ' + metalsmith.destination());
});

/**
* Log an error and then exit the process.
*
* @param {String} message
*/

function fatal(msg){
console.error();
console.error(' Error: '.red + msg);
console.error();
process.exit(1);
}

/**
* Log a `message`.
*
* @param {String} message
*/

function log(message){
console.log();
console.log(' Metalsmith: '.gray + message);
console.log();
}
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ Metalsmith.prototype.metadata = function(metadata){
Metalsmith.prototype.join = function(){
var strs = [].slice.call(arguments);
strs.unshift(this.dir);
console.log(strs);
return path.join.apply(path, strs);
};

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"async": "~0.2.10",
"fs-extra": "~0.8.1",
"defaults": "~1.0.0",
"is-utf8": "~0.2.0"
"is-utf8": "~0.2.0",
"colors": "~0.6.2"

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Mar 7, 2014

you don't want to use colors. it clobbers the String.prototype for this module all dependencies.

For a sane alternative there's: chalk or a little heavier cli-colors.

This comment has been minimized.

Copy link
@ianstormtaylor

ianstormtaylor Mar 7, 2014

Author Contributor

nice! good to know ill update myth too. just fixed: af15c9a

},
"devDependencies": {
"mocha": "1.x",
Expand All @@ -40,4 +41,4 @@
"assert-dir-equal": "~0.1.0",
"metalsmith-drafts": "0.0.1"
}
}
}
1 change: 0 additions & 1 deletion test/fixtures/cli-basic/src/index.md

This file was deleted.

4 changes: 0 additions & 4 deletions test/fixtures/cli-config/config.json

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/cli-config/destination/index.md

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/cli-config/expected/index.md

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/cli-config/source/index.md

This file was deleted.

File renamed without changes.
5 changes: 5 additions & 0 deletions test/fixtures/cli-no-plugin/metalsmith.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": {
"metalsmith-non-existant": {}
}
}
5 changes: 5 additions & 0 deletions test/fixtures/cli-no-plugin/src/one.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
draft: true
---

one
1 change: 1 addition & 0 deletions test/fixtures/cli-no-plugin/src/two.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
two
26 changes: 15 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ describe('CLI', function(){
var bin = __dirname + '/../bin/metalsmith';

describe('build', function(){
it('should perform a basic build', function(done){
exec('cd test/fixtures/cli-basic && ' + bin, function(err, stdout){
if (err) return done(err);
equal('test/fixtures/cli-basic/build', 'test/fixtures/cli-basic/expected');
it('should error without a metalsmith.json', function(done){
exec('cd test/fixtures/cli-no-config && ' + bin, function(err, stdout){
assert(err);
assert(~err.message.indexOf('Could not find a "metalsmith.json" configuration file.'));
done();
});
});
Expand All @@ -146,22 +146,26 @@ describe('CLI', function(){
exec('cd test/fixtures/cli-json && ' + bin, function(err, stdout){
if (err) return done(err);
equal('test/fixtures/cli-json/destination', 'test/fixtures/cli-json/expected');
assert(~stdout.indexOf('Successfully built to '));
assert(~stdout.indexOf('test/fixtures/cli-json/destination'));
done();
});
});

it('should grab config from alternate json', function(done){
exec('cd test/fixtures/cli-config && ' + bin + ' -c config.json', function(err, stdout){
it('should require a plugin', function(done){
exec('cd test/fixtures/cli-drafts && ' + bin, function(err, stdout){
if (err) return done(err);
equal('test/fixtures/cli-config/destination', 'test/fixtures/cli-config/expected');
equal('test/fixtures/cli-drafts/build', 'test/fixtures/cli-drafts/expected');
assert(~stdout.indexOf('Successfully built to '));
assert(~stdout.indexOf('test/fixtures/cli-drafts/build'));
done();
});
});

it('should require a plugin', function(done){
exec('cd test/fixtures/cli-drafts && ' + bin, function(err, stdout){
if (err) return done(err);
equal('test/fixtures/cli-drafts/build', 'test/fixtures/cli-drafts/expected');
it('should error when failing to require a plugin', function(done){
exec('cd test/fixtures/cli-no-plugin && ' + bin, function(err, stdout){
assert(err);
assert(~err.message.indexOf('Failed to require plugin "metalsmith-non-existant".'));
done();
});
});
Expand Down

0 comments on commit c1b8297

Please sign in to comment.