Skip to content

Commit

Permalink
Merge pull request #53 from kfranqueiro/clean-flag
Browse files Browse the repository at this point in the history
Add support for `clean` option
  • Loading branch information
ianstormtaylor committed May 6, 2014
2 parents 80d489c + 3274624 commit 1ef7689
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Expand Up @@ -139,6 +139,10 @@ Set the relative `path` to the source directory, or get the full one if no `path

Set the relative `path` to the destination directory, or get the full one if no `path` is provided. The destination directory defaults to `./build`.

#### #clean(boolean)

Set whether to remove the destination directory before writing to it, or get the current setting. Defaults to `true`.

#### #metadata(json)

Get the global metadata. This is useful for plugins that want to set global-level metadata that can be applied to all files.
Expand Down
6 changes: 3 additions & 3 deletions bin/metalsmith
Expand Up @@ -23,9 +23,9 @@ try {
*/

var metalsmith = new Metalsmith(process.cwd());
if (json.source) metalsmith.source(json.source);
if (json.destination) metalsmith.destination(json.destination);
if (json.metadata) metalsmith.metadata(json.metadata);
['source', 'destination', 'metadata', 'clean'].forEach(function(key){
if (json[key]) metalsmith[key](json[key]);
});

/**
* Plugins.
Expand Down
26 changes: 23 additions & 3 deletions lib/index.js
Expand Up @@ -31,6 +31,7 @@ function Metalsmith(dir){
this.data = {};
this.source('src');
this.destination('build');
this.clean(true);
}

/**
Expand Down Expand Up @@ -84,6 +85,17 @@ Metalsmith.prototype.destination = function(path){
return this;
};

/**
* Get or set whether the destination directory will be removed before writing.
* @param {Boolean} clean
* @return {Boolean or Metalsmith}
*/
Metalsmith.prototype.clean = function(clean){
if (!arguments.length) return this._clean;
this._clean = clean;
return this;
};

/**
* Join path `strs` with the working directory.
*
Expand Down Expand Up @@ -182,10 +194,18 @@ Metalsmith.prototype.read = function(fn){
Metalsmith.prototype.write = function(files, fn){
var dest = this.destination();

rm(dest, function(err){
if (err) return fn(err);
if (this.clean()) {
rm(dest, function(err){
if (err) return fn(err);
writeAll();
});
} else {
writeAll();
}

function writeAll(){
each(Object.keys(files), write, fn);
});
}

function write(file, done){
var data = files[file];
Expand Down
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/write-noclean/expected/index.md
@@ -0,0 +1 @@
body
32 changes: 32 additions & 0 deletions test/index.js
Expand Up @@ -35,6 +35,11 @@ describe('Metalsmith', function(){
assert('build' == m._dest);
});

it('should default clean to true', function(){
var m = Metalsmith('test/tmp');
assert(true === m._clean);
});

describe('#use', function(){
it('should add a plugin to the middleware stack', function(){
var m = Metalsmith('test/tmp');
Expand Down Expand Up @@ -69,6 +74,19 @@ describe('Metalsmith', function(){
});
});

describe('#clean', function(){
it('should set the clean option', function(){
var m = Metalsmith('test/tmp');
m.clean(false);
assert(false === m._clean);
});

it('should get the value of the clean option', function(){
var m = Metalsmith('test/tmp');
assert(true === m.clean());
});
});

describe('#metadata', function(){
it('should get metadata', function(){
var m = Metalsmith('test/tmp');
Expand Down Expand Up @@ -147,6 +165,20 @@ describe('Metalsmith', function(){
});
});

it('should not remove existing destination directory if clean is false', function(done){
var m = Metalsmith('test/fixtures/write-noclean');
m.clean(false);
exec('mkdir -p test/fixtures/write-noclean/build && touch test/fixtures/write-noclean/build/empty.md', function(err){
if (err) return done(err);
var files = { 'index.md': { contents: new Buffer('body') }};
m.write(files, function(err){
if (err) return done(err);
equal('test/fixtures/write-noclean/build', 'test/fixtures/write-noclean/expected');
done();
});
});
});

it('should chmod an optional mode from file metadata', function(done){
var m = Metalsmith('test/fixtures/write-mode');
var files = {
Expand Down

0 comments on commit 1ef7689

Please sign in to comment.