lib/archivist/vaults/file/directories.js
function purgeEmptySubFolders(rootPath, subfolderPath, logger, cb) {
	subfolderPath = subfolderPath || '';
	var fullPath = path.join(rootPath, subfolderPath);

	fs.readdir(fullPath, function (error, files) {
		if (error) {
			return cb();
		}

		async.eachLimit(files, MAX_PARALLEL, function (file, callback) {
			var filepath = path.join(fullPath, file);
			fs.stat(filepath, function (error, stat) {
				if (error || !stat.isDirectory()) {
					return callback();
				}

				// Recurse inwards
				purgeEmptySubFolders(rootPath, path.join(subfolderPath, file), logger, callback);
			});
		}, function () {
			// Do nothing if root path
			if (!subfolderPath) {
				return cb();
			}

			// Otherwise attempt to remove directory
			fs.rmdir(fullPath, function (error) {
				if (error) {
					return cb();
				}

				logger.verbose('Purged empty subfolder:', fullPath);

				return cb();
			});
		});
	});
}
function purgeEmptySubFolders(rootPath, subfolderPath, logger, cb) {
	subfolderPath = subfolderPath || '';
	var fullPath = path.join(rootPath, subfolderPath);

	fs.readdir(fullPath, function (error, files) {
		if (error) {
			return cb();
		}

		async.eachLimit(files, MAX_PARALLEL, function (file, callback) {
			var filepath = path.join(fullPath, file);
			fs.stat(filepath, function (error, stat) {
				if (error || !stat.isDirectory()) {
					return callback();
				}

				// Recurse inwards
				purgeEmptySubFolders(rootPath, path.join(subfolderPath, file), logger, callback);
			});
		}, function () {
			// Do nothing if root path
			if (!subfolderPath) {
				return cb();
			}

			// Otherwise attempt to remove directory
			fs.rmdir(fullPath, function (error) {
				if (error) {
					return cb();
				}

				logger.verbose('Purged empty subfolder:', fullPath);

				return cb();
			});
		});
	});
}
function purgeEmptySubFolders ( rootPath , subfolderPath , logger , cb ) { subfolderPath = subfolderPath || '' ; var fullPath = path . join ( rootPath , subfolderPath ) ; fs . readdir ( fullPath , function ( error , files ) { if ( error ) { return cb ( ) ; } async . eachLimit ( files , MAX_PARALLEL , function ( file , callback ) { var filepath = path . join ( fullPath , file ) ; fs . stat ( filepath , function ( error , stat ) { if ( error || ! stat . isDirectory ( ) ) { return callback ( ) ; } // Recurse inwards purgeEmptySubFolders ( rootPath , path . join ( subfolderPath , file ) , logger , callback ) ; } ) ; } , function ( ) { // Do nothing if root path if ( ! subfolderPath ) { return cb ( ) ; } // Otherwise attempt to remove directory fs . rmdir ( fullPath , function ( error ) { if ( error ) { return cb ( ) ; } logger . verbose ( 'Purged empty subfolder:' , fullPath ) ; return cb ( ) ; } ) ; } ) ; } ) ; }
Scans all subdirectories for a given root path and attempts to delete any empty subfolders. It
will traverse to the deepest child of each branch and work backwards deleting any empty folders.
If there is an error during directory removal, we just ignore it and call the callback.

@param {String} rootPath - root path for given subfolder path
@param {String} subfolderPath - internal recursion string (pass in null)
@param {Object} logger
@param {Function} cb
Scans all subdirectories for a given root path and attempts to delete any empty subfolders . It will traverse to the deepest child of each branch and work backwards deleting any empty folders . If there is an error during directory removal we just ignore it and call the callback .