Add --include-dotfiles option to babel-cli (#6232)

This commit is contained in:
Brian Ng
2017-10-02 08:55:53 -05:00
committed by GitHub
parent 6816b26994
commit 9cd4716cb4
43 changed files with 96 additions and 16 deletions

View File

@@ -71,10 +71,13 @@ export default function(commander, filenames, opts) {
if (commander.deleteDirOnStart) {
util.deleteDir(commander.outDir);
}
util.readdir(dirname).forEach(function(filename) {
const src = path.join(dirname, filename);
handleFile(src, filename, dirname);
});
util
.readdir(dirname, commander.includeDotfiles)
.forEach(function(filename) {
const src = path.join(dirname, filename);
handleFile(src, filename, dirname);
});
} else {
write(filename, path.basename(filename), path.dirname(filename));
}

View File

@@ -130,9 +130,11 @@ export default function(commander, filenames, opts) {
if (stat.isDirectory()) {
const dirname = filename;
util.readdirFilter(filename).forEach(function(filename) {
_filenames.push(path.join(dirname, filename));
});
util
.readdirForCompilable(filename, commander.includeDotfiles)
.forEach(function(filename) {
_filenames.push(path.join(dirname, filename));
});
} else {
_filenames.push(filename);
}

View File

@@ -154,10 +154,14 @@ commander.option(
"-D, --copy-files",
"When compiling a directory copy over non-compilable files",
);
commander.option(
"--include-dotfiles",
"Include dotfiles when compiling and copying non-compilable files",
);
commander.option("-q, --quiet", "Don't log anything");
commander.option(
"--delete-dir-on-start",
"Delete's the out directory before compilation",
"Delete the out directory before compilation",
);
/* eslint-enable max-len */
@@ -233,6 +237,7 @@ delete opts.skipInitialBuild;
delete opts.outFile;
delete opts.outDir;
delete opts.copyFiles;
delete opts.includeDotfiles;
delete opts.quiet;
delete opts.configFile;
delete opts.deleteDirOnStart;

View File

@@ -1,5 +1,5 @@
import commander from "commander";
import readdir from "fs-readdir-recursive";
import readdirRecursive from "fs-readdir-recursive";
import * as babel from "babel-core";
import includes from "lodash/includes";
import path from "path";
@@ -9,13 +9,26 @@ export function chmod(src, dest) {
fs.chmodSync(dest, fs.statSync(src).mode);
}
export function readdirFilter(filename) {
return readdir(filename).filter(function(filename) {
return isCompilableExtension(filename);
});
type ReaddirFilter = (filename: string) => boolean;
export function readdir(
dirname: string,
includeDotfiles: boolean,
filter: ReaddirFilter,
) {
return readdirRecursive(
dirname,
filename =>
(includeDotfiles || filename[0] !== ".") && (!filter || filter(filename)),
);
}
export { readdir };
export function readdirForCompilable(
dirname: string,
includeDotfiles: boolean,
) {
return readdir(dirname, includeDotfiles, isCompilableExtension);
}
/**
* Test if a filename ends with a compilable extension.

View File

@@ -0,0 +1,9 @@
{
"args": [
"src",
"--out-dir", "lib",
"--copy-files",
"--include-dotfiles",
"--ignore", "src/foo"
]
}

View File

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

View File

@@ -0,0 +1 @@
src/index.js -> lib/index.js

View File

@@ -0,0 +1,9 @@
{
"args": [
"src",
"--out-dir", "lib",
"--copy-files",
"--include-dotfiles",
"--only", "src/foo"
]
}

View File

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

View File

@@ -0,0 +1,2 @@
src/foo/.foo.js -> lib/foo/.foo.js
src/foo/bar.js -> lib/foo/bar.js

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,3 @@
src/.foo.js -> lib/.foo.js
src/bar/index.js -> lib/bar/index.js
src/foo/foo.js -> lib/foo/foo.js

View File

@@ -88,10 +88,14 @@ const assertTest = function(stdout, stderr, opts) {
}
if (opts.outFiles) {
const actualFiles = readDir(path.join(tmpLoc));
const actualFiles = readDir(path.join(tmpLoc), fileFilter);
Object.keys(actualFiles).forEach(function(filename) {
if (!opts.inFiles.hasOwnProperty(filename)) {
if (
// saveInFiles always creates an empty .babelrc, so lets exclude for now
filename !== ".babelrc" &&
!opts.inFiles.hasOwnProperty(filename)
) {
const expect = opts.outFiles[filename];
const actual = actualFiles[filename];