2016-03-18 11:12:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Node.js core.
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
// Public node modules.
|
|
|
|
const _ = require('lodash');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const reportback = require('reportback')();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a folder
|
|
|
|
*/
|
|
|
|
module.exports = function (options, cb) {
|
|
|
|
|
|
|
|
// Provide default values for cb.
|
|
|
|
cb = reportback.extend(cb, {
|
|
|
|
alreadyExists: 'error',
|
|
|
|
invalid: 'error'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Provide defaults and validate required options.
|
|
|
|
_.defaults(options, {
|
|
|
|
force: false,
|
|
|
|
gitkeep: false
|
|
|
|
});
|
|
|
|
|
|
|
|
const missingOpts = _.difference([
|
|
|
|
'rootPath'
|
|
|
|
], Object.keys(options));
|
|
|
|
|
|
|
|
if (missingOpts.length) {
|
|
|
|
return cb.invalid(missingOpts);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rootPath = path.resolve(process.cwd(), options.rootPath);
|
|
|
|
|
|
|
|
// Only override an existing folder if `options.force` is true.
|
2016-11-07 16:31:34 +01:00
|
|
|
fs.lstat(rootPath, err => {
|
2016-03-18 11:12:50 +01:00
|
|
|
const exists = !(err && err.code === 'ENOENT');
|
|
|
|
if (exists && err) {
|
|
|
|
return cb.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists && !options.force) {
|
|
|
|
return cb.alreadyExists('Something else already exists at `' + rootPath + '`.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists) {
|
2016-11-07 16:31:34 +01:00
|
|
|
fs.remove(rootPath, err => {
|
2016-03-18 11:12:50 +01:00
|
|
|
if (err) {
|
|
|
|
return cb.error(err);
|
|
|
|
}
|
|
|
|
_afterwards_();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_afterwards_();
|
|
|
|
}
|
|
|
|
|
|
|
|
function _afterwards_() {
|
|
|
|
|
|
|
|
// Don't actually write the directory if this is a dry run.
|
|
|
|
if (options.dry) {
|
|
|
|
return cb.success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the directory.
|
2016-11-07 16:31:34 +01:00
|
|
|
fs.mkdirs(rootPath, err => {
|
2016-03-18 11:12:50 +01:00
|
|
|
if (err) {
|
|
|
|
return cb.error(err);
|
|
|
|
}
|
|
|
|
return cb.success();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|