diff --git a/packages/babel-cli/src/babel/dir.js b/packages/babel-cli/src/babel/dir.js index d59e4e5844..cc643cb882 100644 --- a/packages/babel-cli/src/babel/dir.js +++ b/packages/babel-cli/src/babel/dir.js @@ -63,6 +63,9 @@ export default function(commander, filenames, opts) { if (stat.isDirectory(filename)) { const dirname = filename; + if (commander.deleteDirOnStart) { + util.deleteDir(commander.outDir); + } util.readdir(dirname).forEach(function(filename) { const src = path.join(dirname, filename); handleFile(src, filename); diff --git a/packages/babel-cli/src/babel/index.js b/packages/babel-cli/src/babel/index.js index caa00f6c6f..c77779c51b 100755 --- a/packages/babel-cli/src/babel/index.js +++ b/packages/babel-cli/src/babel/index.js @@ -147,6 +147,10 @@ commander.option( "When compiling a directory copy over non-compilable files", ); commander.option("-q, --quiet", "Don't log anything"); +commander.option( + "--delete-dir-on-start", + "Delete's the out directory before compilation", +); /* eslint-enable max-len */ commander.version(pkg.version + " (babel-core " + version + ")"); @@ -192,6 +196,9 @@ if (commander.watch) { if (commander.skipInitialBuild && !commander.watch) { errors.push("--skip-initial-build requires --watch"); } +if (commander.deleteDirOnStart && !commander.outDir) { + errors.push("--delete-dir-on-start requires --out-dir"); +} if (errors.length) { console.error(errors.join(". ")); @@ -216,6 +223,7 @@ delete opts.outDir; delete opts.copyFiles; delete opts.quiet; delete opts.configFile; +delete opts.deleteDirOnStart; // Commander will default the "--no-" arguments to true, but we want to leave them undefined so that // babel-core can handle the default-assignment logic on its own. diff --git a/packages/babel-cli/src/babel/util.js b/packages/babel-cli/src/babel/util.js index 3150b3753a..1ab9550a64 100644 --- a/packages/babel-cli/src/babel/util.js +++ b/packages/babel-cli/src/babel/util.js @@ -58,6 +58,22 @@ export function compile(filename, opts) { } } +export function deleteDir(path) { + if (fs.existsSync(path)) { + fs.readdirSync(path).forEach(function(file) { + const curPath = path + "/" + file; + if (fs.lstatSync(curPath).isDirectory()) { + // recurse + deleteDir(curPath); + } else { + // delete file + fs.unlinkSync(curPath); + } + }); + fs.rmdirSync(path); + } +} + function toErrorStack(err) { if (err._babel && err instanceof SyntaxError) { return `${err.name}: ${err.message}\n${err.codeFrame}`;