--copy-ignored flag added to CLI (#10887)

* prevent ignored files in out dir

* added includeIgnore cli option

* Help text change

* Update packages/babel-cli/src/babel/options.js

Copy review.

Co-Authored-By: Brian Ng <bng412@gmail.com>

* review comments

* throw error if copyIgnored is used without ignore flag

* check for ignored files

* duplicate pathToPattern fn in babel/cli

* change implementation

* removed ignore option from cliOption

* added test case with ignore in config

* added test case with ignore in config

* review

Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
Raja Sekar 2020-01-10 03:03:37 +01:00 committed by Nicolò Ribaudo
parent 3af02f63de
commit 8415065d99
70 changed files with 165 additions and 7 deletions

View File

@ -9,6 +9,13 @@ import fs from "fs";
import * as util from "./util"; import * as util from "./util";
import { type CmdOptions } from "./options"; import { type CmdOptions } from "./options";
const FILE_TYPE = Object.freeze({
NON_COMPILABLE: "NON_COMPILABLE",
COMPILED: "COMPILED",
IGNORED: "IGNORED",
ERR_COMPILATION: "ERR_COMPILATION",
});
function outputFileSync(filePath: string, data: string | Buffer): void { function outputFileSync(filePath: string, data: string | Buffer): void {
makeDirSync(path.dirname(filePath)); makeDirSync(path.dirname(filePath));
fs.writeFileSync(filePath, data); fs.writeFileSync(filePath, data);
@ -20,11 +27,14 @@ export default async function({
}: CmdOptions): Promise<void> { }: CmdOptions): Promise<void> {
const filenames = cliOptions.filenames; const filenames = cliOptions.filenames;
async function write(src: string, base: string): Promise<boolean> { async function write(
src: string,
base: string,
): Promise<$Keys<typeof FILE_TYPE>> {
let relative = path.relative(base, src); let relative = path.relative(base, src);
if (!util.isCompilableExtension(relative, cliOptions.extensions)) { if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
return false; return FILE_TYPE.NON_COMPILABLE;
} }
relative = util.withExtension( relative = util.withExtension(
@ -47,7 +57,7 @@ export default async function({
), ),
); );
if (!res) return false; if (!res) return FILE_TYPE.IGNORED;
// we've requested explicit sourcemaps to be written to disk // we've requested explicit sourcemaps to be written to disk
if ( if (
@ -68,11 +78,11 @@ export default async function({
console.log(src + " -> " + dest); console.log(src + " -> " + dest);
} }
return true; return FILE_TYPE.COMPILED;
} catch (err) { } catch (err) {
if (cliOptions.watch) { if (cliOptions.watch) {
console.error(err); console.error(err);
return false; return FILE_TYPE.ERR_COMPILATION;
} }
throw err; throw err;
@ -89,13 +99,16 @@ export default async function({
async function handleFile(src: string, base: string): Promise<boolean> { async function handleFile(src: string, base: string): Promise<boolean> {
const written = await write(src, base); const written = await write(src, base);
if (!written && cliOptions.copyFiles) { if (
(cliOptions.copyFiles && written === FILE_TYPE.NON_COMPILABLE) ||
(cliOptions.copyIgnored && written === FILE_TYPE.IGNORED)
) {
const filename = path.relative(base, src); const filename = path.relative(base, src);
const dest = getDest(filename, base); const dest = getDest(filename, base);
outputFileSync(dest, fs.readFileSync(src)); outputFileSync(dest, fs.readFileSync(src));
util.chmod(src, dest); util.chmod(src, dest);
} }
return written; return written === FILE_TYPE.COMPILED;
} }
async function handle(filenameOrDir: string): Promise<number> { async function handle(filenameOrDir: string): Promise<number> {

View File

@ -165,6 +165,11 @@ commander.option(
"Use a specific extension for the output files", "Use a specific extension for the output files",
); );
commander.option(
"--copy-ignored",
"Include ignored files when copying non-compilable files.",
);
commander.version(pkg.version + " (@babel/core " + version + ")"); commander.version(pkg.version + " (@babel/core " + version + ")");
commander.usage("[options] <files ...>"); commander.usage("[options] <files ...>");
// register an empty action handler so that commander.js can throw on // register an empty action handler so that commander.js can throw on
@ -315,6 +320,7 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
quiet: opts.quiet, quiet: opts.quiet,
deleteDirOnStart: opts.deleteDirOnStart, deleteDirOnStart: opts.deleteDirOnStart,
sourceMapTarget: opts.sourceMapTarget, sourceMapTarget: opts.sourceMapTarget,
copyIgnored: opts.copyIgnored,
}, },
}; };
} }

View File

@ -0,0 +1,3 @@
{
"ignore": ["src/foo"]
}

View File

@ -0,0 +1,10 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--include-dotfiles",
"--verbose"
]
}

View File

@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.

View File

@ -0,0 +1,10 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--copy-ignored",
"--verbose"
]
}

View File

@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.

View File

@ -0,0 +1,3 @@
{
"ignore": ["src/foo"]
}

View File

@ -0,0 +1,10 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--copy-ignored",
"--verbose"
]
}

View File

@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.

View File

@ -0,0 +1,9 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--verbose"
]
}

View File

@ -0,0 +1,3 @@
"use strict";
index;

View File

@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.

View File

@ -0,0 +1,3 @@
{
"ignore": ["src/foo"]
}

View File

@ -0,0 +1,9 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--verbose"
]
}

View File

@ -0,0 +1,3 @@
"use strict";
index;

View File

@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.

View File

@ -0,0 +1,12 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--ignore",
"src/foo",
"--copy-ignored",
"--verbose"
]
}

View File

@ -0,0 +1,3 @@
"use strict";
index;

View File

@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.

View File

@ -0,0 +1,12 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--only",
"src/foo/*",
"--copy-ignored",
"--verbose"
]
}

View File

@ -0,0 +1,3 @@
"use strict";
bar;

View File

@ -0,0 +1,2 @@
src/foo/bar.js -> lib/foo/bar.js
Successfully compiled 1 file with Babel.

View File

@ -97,6 +97,7 @@ const assertTest = function(stdout, stderr, opts, cwd) {
if ( if (
// saveInFiles always creates an empty .babelrc, so lets exclude for now // saveInFiles always creates an empty .babelrc, so lets exclude for now
filename !== ".babelrc" && filename !== ".babelrc" &&
filename !== ".babelignore" &&
!Object.prototype.hasOwnProperty.call(opts.inFiles, filename) !Object.prototype.hasOwnProperty.call(opts.inFiles, filename)
) { ) {
const expected = opts.outFiles[filename]; const expected = opts.outFiles[filename];
@ -239,9 +240,15 @@ fs.readdirSync(fixtureLoc).forEach(function(binName) {
opts.inFiles = readDir(path.join(testLoc, "in-files"), fileFilter); opts.inFiles = readDir(path.join(testLoc, "in-files"), fileFilter);
const babelrcLoc = path.join(testLoc, ".babelrc"); const babelrcLoc = path.join(testLoc, ".babelrc");
const babelIgnoreLoc = path.join(testLoc, ".babelignore");
if (fs.existsSync(babelrcLoc)) { if (fs.existsSync(babelrcLoc)) {
// copy .babelrc file to tmp directory // copy .babelrc file to tmp directory
opts.inFiles[".babelrc"] = helper.readFile(babelrcLoc); opts.inFiles[".babelrc"] = helper.readFile(babelrcLoc);
opts.inFiles[".babelignore"] = helper.readFile(babelIgnoreLoc);
}
if (fs.existsSync(babelIgnoreLoc)) {
// copy .babelignore file to tmp directory
opts.inFiles[".babelignore"] = helper.readFile(babelIgnoreLoc);
} }
it(testName, buildTest(binName, testName, opts), 20000); it(testName, buildTest(binName, testName, opts), 20000);