-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
192 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,51 @@ | ||
"use strict"; | ||
var _clean; | ||
|
||
_clean = function(config, options, next) { | ||
var dir, directories, done, fs, i, path, wrench, _; | ||
fs = require('fs'); | ||
path = require('path'); | ||
_ = require('lodash'); | ||
wrench = require('wrench'); | ||
dir = config.watch.compiledDir; | ||
directories = wrench.readdirSyncRecursive(dir).filter(function(f) { | ||
|
||
// This module removes empty directories after the files | ||
// have been removed from them | ||
|
||
var fs = require( "fs" ) | ||
, path = require( "path" ) | ||
, _ = require( "lodash" ) | ||
, wrench = require( "wrench") | ||
; | ||
|
||
var _clean = function( config, options, next ) { | ||
|
||
var dir = config.watch.compiledDir; | ||
var directories = wrench.readdirSyncRecursive(dir).filter( function( f ) { | ||
return fs.statSync(path.join(dir, f)).isDirectory(); | ||
}); | ||
if (directories.length === 0) { | ||
|
||
if ( directories.length === 0 ) { | ||
return next(); | ||
} | ||
i = 0; | ||
done = function() { | ||
if (++i === directories.length) { | ||
return next(); | ||
|
||
var doneCount = 0; | ||
var done = function(){ | ||
if ( ++doneCount === directories.length ) { | ||
next(); | ||
} | ||
}; | ||
return _.sortBy(directories, 'length').reverse().forEach(function(dir) { | ||
var dirPath, err; | ||
dirPath = path.join(config.watch.compiledDir, dir); | ||
if (fs.existsSync(dirPath)) { | ||
if (config.log.isDebug()) { | ||
config.log.debug("Deleting directory [[ " + dirPath + " ]]"); | ||
} | ||
|
||
_.sortBy( directories, "length" ).reverse().forEach( function( dir ) { | ||
var dirPath = path.join( config.watch.compiledDir, dir ); | ||
if( fs.existsSync( dirPath ) ) { | ||
try { | ||
fs.rmdirSync(dirPath); | ||
config.log.success("Deleted empty directory [[ " + dirPath + " ]]"); | ||
} catch (_error) { | ||
err = _error; | ||
if (err.code === 'ENOTEMPTY') { | ||
config.log.info("Unable to delete directory [[ " + dirPath + " ]] because directory not empty"); | ||
fs.rmdirSync( dirPath ); | ||
config.log.success( "Deleted empty directory [[ " + dirPath + " ]]" ); | ||
} catch ( err ) { | ||
if ( err.code === "ENOTEMPTY" ) { | ||
config.log.info( "Unable to delete directory [[ " + dirPath + " ]] because directory not empty." ); | ||
} else { | ||
config.log.error("Unable to delete directory, [[ " + dirPath + " ]]"); | ||
config.log.error(err); | ||
config.log.error( "Unable to delete directory, [[ " + dirPath + " ]]" ); | ||
config.log.error( err ); | ||
} | ||
} | ||
} | ||
return done(); | ||
done(); | ||
}); | ||
}; | ||
|
||
exports.registration = function(config, register) { | ||
return register(['postClean'], 'complete', _clean); | ||
}; | ||
exports.registration = function( config, register ) { | ||
register( ["postClean"], "complete", _clean ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"use strict"; | ||
|
||
// This module removes empty directories after the files | ||
// have been removed from them | ||
|
||
var fs = require( "fs" ) | ||
, path = require( "path" ) | ||
, _ = require( "lodash" ) | ||
, wrench = require( "wrench") | ||
; | ||
|
||
var _clean = function( config, options, next ) { | ||
|
||
var dir = config.watch.compiledDir; | ||
var directories = wrench.readdirSyncRecursive(dir).filter( function( f ) { | ||
return fs.statSync(path.join(dir, f)).isDirectory(); | ||
}); | ||
|
||
if ( directories.length === 0 ) { | ||
return next(); | ||
} | ||
|
||
var doneCount = 0; | ||
var done = function(){ | ||
if ( ++doneCount === directories.length ) { | ||
next(); | ||
} | ||
}; | ||
|
||
_.sortBy( directories, "length" ).reverse().forEach( function( dir ) { | ||
var dirPath = path.join( config.watch.compiledDir, dir ); | ||
if( fs.existsSync( dirPath ) ) { | ||
try { | ||
fs.rmdirSync( dirPath ); | ||
config.log.success( "Deleted empty directory [[ " + dirPath + " ]]" ); | ||
} catch ( err ) { | ||
if ( err.code === "ENOTEMPTY" ) { | ||
config.log.info( "Unable to delete directory [[ " + dirPath + " ]] because directory not empty." ); | ||
} else { | ||
config.log.error( "Unable to delete directory, [[ " + dirPath + " ]]" ); | ||
config.log.error( err ); | ||
} | ||
} | ||
} | ||
done(); | ||
}); | ||
}; | ||
|
||
exports.registration = function( config, register ) { | ||
register( ["postClean"], "complete", _clean ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require("./units/modules/file/write-test"); | ||
require("./units/modules/file/read-test"); | ||
require("./units/modules/file/init-test"); | ||
require("./units/modules/file/delete-test"); | ||
require("./units/modules/file/delete-test"); | ||
require("./units/modules/file/clean-test"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
var fs = require( "fs" ) | ||
, path = require( "path" ) | ||
, sinon = require( "sinon" ) | ||
, wrench = require( "wrench" ) | ||
, utils = require( path.join(process.cwd(), "test", "utils") ) | ||
, cleanModule = require( path.join(process.cwd(), "lib", "modules", "file", "clean") ) | ||
, fakeMimosaConfig = utils.fakeMimosaConfig(); | ||
; | ||
|
||
describe( "Mimosa file cleaning workflow module", function(){ | ||
var cleanFunction | ||
, spy | ||
, wrenchStub | ||
; | ||
|
||
var createWrenchStub = function( directories ) { | ||
sinon.stub(wrench, "readdirSyncRecursive", function(){ | ||
return directories; | ||
}); | ||
} | ||
|
||
before(function(done) { | ||
spy = sinon.spy(); | ||
utils.testRegistration( cleanModule, function( func ) { | ||
cleanFunction = func; | ||
done(); | ||
}, true); | ||
}); | ||
|
||
afterEach(function() { | ||
spy.reset(); | ||
wrench.readdirSyncRecursive.restore(); | ||
}); | ||
|
||
it("will call lifecycle callback if no directories in project", function() { | ||
createWrenchStub([]); | ||
cleanFunction( fakeMimosaConfig, {}, spy ); | ||
expect(spy.calledOnce).to.be.true; | ||
}); | ||
|
||
var testCallbackWithFiles = function( files, exists ) { | ||
createWrenchStub(files); | ||
var rmdirSyncStub = sinon.stub( fs, "rmdirSync"); | ||
var existsSyncStub = sinon.stub( fs, "existsSync", function(){ | ||
if( exists === true ) { | ||
return true | ||
} else if( exists === false ) { | ||
return false; | ||
} else { | ||
return Math.random() >= 0.5; | ||
} | ||
}); | ||
var statSyncStub = sinon.stub( fs, "statSync", function() { | ||
return { | ||
isDirectory: function() { | ||
return true; | ||
} | ||
} | ||
}); | ||
cleanFunction( fakeMimosaConfig, {}, spy ); | ||
|
||
// if not random, then all directories "do not exist" | ||
// so no attempt to remove the directory will take place | ||
if( exists === false) { | ||
expect(rmdirSyncStub.callCount).to.eql(0); | ||
} else if ( exists === true) { | ||
expect(rmdirSyncStub.callCount).to.eql(files.length); | ||
} | ||
|
||
expect(spy.calledOnce).to.be.true; | ||
expect(existsSyncStub.callCount).to.eql(files.length); | ||
expect(statSyncStub.callCount).to.eql(files.length); | ||
fs.existsSync.restore(); | ||
fs.statSync.restore(); | ||
fs.rmdirSync.restore(); | ||
}; | ||
|
||
it("will call lifecycle callback if one directory in project", function() { | ||
testCallbackWithFiles(["foo"]); | ||
}); | ||
|
||
it("will call lifecycle callback if multiple directories in project", function() { | ||
testCallbackWithFiles(["foo", "foooooo", "bar", "foo/bar", "baarrrrerrrrr"]); | ||
}); | ||
|
||
it("will call lifecycle callback and not attempt to remove directories if files do not exist ", function(){ | ||
testCallbackWithFiles(["foo", "foooooo"], false); | ||
}); | ||
|
||
it("will call lifecycle callback and attempt to remove directories if they do exist", function(){ | ||
testCallbackWithFiles(["foo", "foooooo"], true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters