Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--copy-ignored flag added to CLI #10887

Merged
merged 13 commits into from Jan 10, 2020
27 changes: 20 additions & 7 deletions packages/babel-cli/src/babel/dir.js
Expand Up @@ -9,6 +9,13 @@ import fs from "fs";
import * as util from "./util";
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 {
makeDirSync(path.dirname(filePath));
fs.writeFileSync(filePath, data);
Expand All @@ -20,11 +27,14 @@ export default async function({
}: CmdOptions): Promise<void> {
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);

if (!util.isCompilableExtension(relative, cliOptions.extensions)) {
return false;
return FILE_TYPE.NON_COMPILABLE;
}

// remove extension and then append back on .js
Expand All @@ -43,7 +53,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
if (
Expand All @@ -64,11 +74,11 @@ export default async function({
console.log(src + " -> " + dest);
}

return true;
return FILE_TYPE.COMPILED;
} catch (err) {
if (cliOptions.watch) {
console.error(err);
return false;
return FILE_TYPE.ERR_COMPILATION;
}

throw err;
Expand All @@ -85,13 +95,16 @@ export default async function({
async function handleFile(src: string, base: string): Promise<boolean> {
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 dest = getDest(filename, base);
outputFileSync(dest, fs.readFileSync(src));
util.chmod(src, dest);
}
return written;
return written === FILE_TYPE.COMPILED;
}

async function handle(filenameOrDir: string): Promise<number> {
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-cli/src/babel/options.js
Expand Up @@ -161,6 +161,11 @@ commander.option(
"Delete the out directory before compilation.",
);

commander.option(
"--copy-ignored",
"Include ignored files when copying non-compilable files.",
);

commander.version(pkg.version + " (@babel/core " + version + ")");
commander.usage("[options] <files ...>");
// register an empty action handler so that commander.js can throw on
Expand Down Expand Up @@ -304,6 +309,7 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
quiet: opts.quiet,
deleteDirOnStart: opts.deleteDirOnStart,
sourceMapTarget: opts.sourceMapTarget,
copyIgnored: opts.copyIgnored,
},
};
}
Expand Down
@@ -0,0 +1,3 @@
{
"ignore": ["src/foo"]
}
@@ -0,0 +1,10 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--include-dotfiles",
"--verbose"
]
}
@@ -0,0 +1,3 @@
"use strict";

index;
@@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.
@@ -0,0 +1 @@
src/foo
@@ -0,0 +1,10 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--copy-ignored",
"--verbose"
]
}
@@ -0,0 +1 @@
bar;
@@ -0,0 +1,3 @@
"use strict";

index;
@@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.
@@ -0,0 +1,3 @@
{
"ignore": ["src/foo"]
}
@@ -0,0 +1 @@
bar;
@@ -0,0 +1 @@
index;
@@ -0,0 +1,10 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--copy-ignored",
"--verbose"
]
}
@@ -0,0 +1 @@
bar;
@@ -0,0 +1,3 @@
"use strict";

index;
@@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.
@@ -0,0 +1 @@
src/foo
@@ -0,0 +1 @@
bar;
@@ -0,0 +1 @@
index;
@@ -0,0 +1,9 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--verbose"
]
}
@@ -0,0 +1,3 @@
"use strict";

index;
@@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.
@@ -0,0 +1,3 @@
{
"ignore": ["src/foo"]
}
@@ -0,0 +1 @@
bar;
@@ -0,0 +1 @@
index;
@@ -0,0 +1,9 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--verbose"
]
}
@@ -0,0 +1,3 @@
"use strict";

index;
@@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.
@@ -0,0 +1 @@
bar;
@@ -0,0 +1 @@
index;
@@ -0,0 +1,12 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--ignore",
"src/foo",
"--copy-ignored",
"--verbose"
]
}
@@ -0,0 +1 @@
bar;
@@ -0,0 +1,3 @@
"use strict";

index;
@@ -0,0 +1,2 @@
src/index.js -> lib/index.js
Successfully compiled 1 file with Babel.
@@ -0,0 +1 @@
bar;
@@ -0,0 +1 @@
index;
@@ -0,0 +1,12 @@
{
"args": [
"src",
"--out-dir",
"lib",
"--copy-files",
"--only",
"src/foo/*",
"--copy-ignored",
"--verbose"
]
}
@@ -0,0 +1,3 @@
"use strict";

bar;
@@ -0,0 +1 @@
index;
@@ -0,0 +1,2 @@
src/foo/bar.js -> lib/foo/bar.js
Successfully compiled 1 file with Babel.
7 changes: 7 additions & 0 deletions packages/babel-cli/test/index.js
Expand Up @@ -97,6 +97,7 @@ const assertTest = function(stdout, stderr, opts, cwd) {
if (
// saveInFiles always creates an empty .babelrc, so lets exclude for now
filename !== ".babelrc" &&
filename !== ".babelignore" &&
!Object.prototype.hasOwnProperty.call(opts.inFiles, filename)
) {
const expected = opts.outFiles[filename];
Expand Down Expand Up @@ -239,9 +240,15 @@ fs.readdirSync(fixtureLoc).forEach(function(binName) {
opts.inFiles = readDir(path.join(testLoc, "in-files"), fileFilter);

const babelrcLoc = path.join(testLoc, ".babelrc");
const babelIgnoreLoc = path.join(testLoc, ".babelignore");
if (fs.existsSync(babelrcLoc)) {
// copy .babelrc file to tmp directory
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);
Expand Down