From 2b3590929b4eb955b0e8b287f9749b5af66cc1a0 Mon Sep 17 00:00:00 2001 From: Raja Sekar Date: Wed, 18 Dec 2019 04:39:36 +0100 Subject: [PATCH] Prevent ignored files in out dir (#10831) Co-Authored-By: Brian Ng --- packages/babel-cli/src/babel/dir.js | 12 ++++++++++-- packages/babel-cli/src/babel/options.js | 6 ++++++ .../out-files/lib/foo/.foo.js | 1 - .../in-files/src/.foorc | 0 .../in-files/src/README.md | 0 .../in-files/src}/foo/bar.js | 0 .../in-files/src}/index.js | 0 .../options.json | 12 ++++++++++++ .../out-files/lib/README.md | 0 .../out-files/lib/foo/bar.js} | 0 .../out-files/lib/index.js | 3 +++ .../stdout.txt | 2 ++ .../--copy-files with only/out-files/lib/index.js | 1 - 13 files changed, 33 insertions(+), 4 deletions(-) delete mode 100644 packages/babel-cli/test/fixtures/babel/--copy-files --include-dotfiles with ignore/out-files/lib/foo/.foo.js create mode 100644 packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/.foorc create mode 100644 packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/README.md rename packages/babel-cli/test/fixtures/babel/{--copy-files with ignore/out-files/lib => --copy-files with ignore with includeIgnored/in-files/src}/foo/bar.js (100%) rename packages/babel-cli/test/fixtures/babel/{--copy-files --include-dotfiles with only/out-files/lib => --copy-files with ignore with includeIgnored/in-files/src}/index.js (100%) create mode 100644 packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/options.json create mode 100644 packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/README.md rename packages/babel-cli/test/fixtures/babel/{--copy-files --include-dotfiles with ignore/out-files/lib/foo/index.js => --copy-files with ignore with includeIgnored/out-files/lib/foo/bar.js} (100%) create mode 100644 packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/index.js create mode 100644 packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/stdout.txt delete mode 100644 packages/babel-cli/test/fixtures/babel/--copy-files with only/out-files/lib/index.js diff --git a/packages/babel-cli/src/babel/dir.js b/packages/babel-cli/src/babel/dir.js index 234eb7c8f60b..62ad36ba5832 100644 --- a/packages/babel-cli/src/babel/dir.js +++ b/packages/babel-cli/src/babel/dir.js @@ -84,8 +84,16 @@ export default async function({ async function handleFile(src: string, base: string): Promise { const written = await write(src, base); - - if (!written && cliOptions.copyFiles) { + const relative = path.relative(base, src); + const isCompilableExtension = util.isCompilableExtension( + relative, + cliOptions.extensions, + ); + if ( + !written && + ((!isCompilableExtension && cliOptions.copyFiles) || + cliOptions.includeIgnored) + ) { const filename = path.relative(base, src); const dest = getDest(filename, base); outputFileSync(dest, fs.readFileSync(src)); diff --git a/packages/babel-cli/src/babel/options.js b/packages/babel-cli/src/babel/options.js index fa09d6b64ed3..45590ae9341e 100644 --- a/packages/babel-cli/src/babel/options.js +++ b/packages/babel-cli/src/babel/options.js @@ -161,6 +161,11 @@ commander.option( "Delete the out directory before compilation.", ); +commander.option( + "--include-ignored", + "Include ignored files when copying non-compilable files.", +); + commander.version(pkg.version + " (@babel/core " + version + ")"); commander.usage("[options] "); // register an empty action handler so that commander.js can throw on @@ -304,6 +309,7 @@ export default function parseArgv(args: Array): CmdOptions | null { quiet: opts.quiet, deleteDirOnStart: opts.deleteDirOnStart, sourceMapTarget: opts.sourceMapTarget, + includeIgnored: opts.includeIgnored, }, }; } diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files --include-dotfiles with ignore/out-files/lib/foo/.foo.js b/packages/babel-cli/test/fixtures/babel/--copy-files --include-dotfiles with ignore/out-files/lib/foo/.foo.js deleted file mode 100644 index 31142aabfe70..000000000000 --- a/packages/babel-cli/test/fixtures/babel/--copy-files --include-dotfiles with ignore/out-files/lib/foo/.foo.js +++ /dev/null @@ -1 +0,0 @@ -a; diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/.foorc b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/.foorc new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/README.md b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/README.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files with ignore/out-files/lib/foo/bar.js b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/foo/bar.js similarity index 100% rename from packages/babel-cli/test/fixtures/babel/--copy-files with ignore/out-files/lib/foo/bar.js rename to packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/foo/bar.js diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files --include-dotfiles with only/out-files/lib/index.js b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/index.js similarity index 100% rename from packages/babel-cli/test/fixtures/babel/--copy-files --include-dotfiles with only/out-files/lib/index.js rename to packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/in-files/src/index.js diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/options.json b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/options.json new file mode 100644 index 000000000000..36b8f50db7b2 --- /dev/null +++ b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/options.json @@ -0,0 +1,12 @@ +{ + "args": [ + "src", + "--out-dir", + "lib", + "--copy-files", + "--ignore", + "src/foo/*", + "--include-ignored", + "--verbose" + ] +} diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/README.md b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/README.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files --include-dotfiles with ignore/out-files/lib/foo/index.js b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/foo/bar.js similarity index 100% rename from packages/babel-cli/test/fixtures/babel/--copy-files --include-dotfiles with ignore/out-files/lib/foo/index.js rename to packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/foo/bar.js diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/index.js b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/index.js new file mode 100644 index 000000000000..cb5e86b12ef3 --- /dev/null +++ b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/out-files/lib/index.js @@ -0,0 +1,3 @@ +"use strict"; + +index; \ No newline at end of file diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/stdout.txt b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/stdout.txt new file mode 100644 index 000000000000..84a430432e47 --- /dev/null +++ b/packages/babel-cli/test/fixtures/babel/--copy-files with ignore with includeIgnored/stdout.txt @@ -0,0 +1,2 @@ +src/index.js -> lib/index.js +Successfully compiled 1 file with Babel. diff --git a/packages/babel-cli/test/fixtures/babel/--copy-files with only/out-files/lib/index.js b/packages/babel-cli/test/fixtures/babel/--copy-files with only/out-files/lib/index.js deleted file mode 100644 index c6788558edcb..000000000000 --- a/packages/babel-cli/test/fixtures/babel/--copy-files with only/out-files/lib/index.js +++ /dev/null @@ -1 +0,0 @@ -index;