Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for clean option #53

Merged
merged 1 commit into from
May 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body
32 changes: 32 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
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