Permalink
Browse files

Merge pull request #53 from kfranqueiro/clean-flag

Add support for `clean` option
  • Loading branch information...
2 parents 80d489c + 3274624 commit 1ef768941e166881b8233088c6e9b4b10d464322 @ianstormtaylor ianstormtaylor committed May 6, 2014
View
@@ -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.
View
@@ -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.
View
@@ -31,6 +31,7 @@ function Metalsmith(dir){
this.data = {};
this.source('src');
this.destination('build');
+ this.clean(true);
}
/**
@@ -85,6 +86,17 @@ Metalsmith.prototype.destination = function(path){
};
/**
+ * 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.
*
* @param {String} strs...
@@ -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];
@@ -0,0 +1 @@
+body
View
@@ -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');
@@ -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');
@@ -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 = {

0 comments on commit 1ef7689

Please sign in to comment.