From f7109658f9975aa3a4bf77c7de6c6c9a9d627471 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Fri, 1 Sep 2017 17:45:13 -0400 Subject: [PATCH] Added --delete-dir-on-start option for babel (#6187) * added --delete-dir-on-start option added --delete-dir-on-start-option to delete dir on start of compilation to remove deleted files from the orignial files from the --out-dir * added option --delete-dir-on-start added --delete-dir-on-start that option will delete the --out-dir before the compilation of code to remove the deleted files from the source from the out directory * added --delete-dir-on-start option added --delete-dir-on-start-option to delete dir on start of compilation to remove deleted files from the orignial files from the --out-dir * bug removed deleting the correct dir in the previous code, the source dir was deleted each time rather than deleting the out dir * Remove shorthand * Prevent babel-cli option from reaching babel-core * Lint --- packages/babel-cli/src/babel/dir.js | 3 +++ packages/babel-cli/src/babel/index.js | 8 ++++++++ packages/babel-cli/src/babel/util.js | 16 ++++++++++++++++ 3 files changed, 27 insertions(+) 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}`;