Add option to define output directory relative to the input (#5421)
* Fix output directory if filename is given * Add test for relative output path * Add option to define output dir relative to input * Add tests for --copy-files * Test error handling for wrong arguments
This commit is contained in:
parent
5565e1b406
commit
b6467a68ca
@ -7,7 +7,7 @@ import fs from "fs";
|
||||
import * as util from "./util";
|
||||
|
||||
export default function(commander, filenames, opts) {
|
||||
function write(src, relative) {
|
||||
function write(src, relative, base) {
|
||||
if (!util.isCompilableExtension(relative, commander.extensions)) {
|
||||
return false;
|
||||
}
|
||||
@ -15,7 +15,7 @@ export default function(commander, filenames, opts) {
|
||||
// remove extension and then append back on .js
|
||||
relative = relative.replace(/\.(\w*?)$/, "") + ".js";
|
||||
|
||||
const dest = path.join(commander.outDir, relative);
|
||||
const dest = getDest(commander, relative, base);
|
||||
|
||||
const data = util.compile(
|
||||
src,
|
||||
@ -45,11 +45,16 @@ export default function(commander, filenames, opts) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function handleFile(src, filename) {
|
||||
const didWrite = write(src, filename);
|
||||
function getDest(commander, filename, base) {
|
||||
if (commander.relative) return path.join(base, commander.outDir, filename);
|
||||
return path.join(commander.outDir, filename);
|
||||
}
|
||||
|
||||
function handleFile(src, filename, base) {
|
||||
const didWrite = write(src, filename, base);
|
||||
|
||||
if (!didWrite && commander.copyFiles) {
|
||||
const dest = path.join(commander.outDir, filename);
|
||||
const dest = getDest(commander, filename, base);
|
||||
outputFileSync(dest, fs.readFileSync(src));
|
||||
util.chmod(src, dest);
|
||||
}
|
||||
@ -68,10 +73,10 @@ export default function(commander, filenames, opts) {
|
||||
}
|
||||
util.readdir(dirname).forEach(function(filename) {
|
||||
const src = path.join(dirname, filename);
|
||||
handleFile(src, filename);
|
||||
handleFile(src, filename, dirname);
|
||||
});
|
||||
} else {
|
||||
write(filename, filename);
|
||||
write(filename, path.basename(filename), path.dirname(filename));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -142,6 +142,10 @@ commander.option(
|
||||
"-d, --out-dir [out]",
|
||||
"Compile an input directory of modules into an output directory",
|
||||
);
|
||||
commander.option(
|
||||
"--relative",
|
||||
"Compile into an output directory relative to input directory or file. Requires --out-dir [out]",
|
||||
);
|
||||
commander.option(
|
||||
"-D, --copy-files",
|
||||
"When compiling a directory copy over non-compilable files",
|
||||
@ -183,6 +187,10 @@ if (commander.outFile && commander.outDir) {
|
||||
errors.push("cannot have --out-file and --out-dir");
|
||||
}
|
||||
|
||||
if (commander.relative && !commander.outDir) {
|
||||
errors.push("output directory required for --relative");
|
||||
}
|
||||
|
||||
if (commander.watch) {
|
||||
if (!commander.outFile && !commander.outDir) {
|
||||
errors.push("--watch requires --out-file or --out-dir");
|
||||
|
||||
1
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/in-files/src/bar.json
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/in-files/src/bar.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{}
|
||||
1
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/in-files/src/foo.js
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/in-files/src/foo.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
arr.map(x => x * MULTIPLIER);
|
||||
3
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/options.json
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"args": ["src", "--out-dir", "lib", "--copy-files"]
|
||||
}
|
||||
1
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/out-files/lib/bar.json
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/out-files/lib/bar.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{}
|
||||
5
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/out-files/lib/foo.js
vendored
Normal file
5
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/out-files/lib/foo.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
arr.map(function (x) {
|
||||
return x * MULTIPLIER;
|
||||
});
|
||||
1
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/stdout.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/dir --out-dir --copy-files/stdout.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
src/foo.js -> lib/foo.js
|
||||
@ -0,0 +1,3 @@
|
||||
class Test1 {
|
||||
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
arr.map(x => x * MULTIPLIER);
|
||||
@ -0,0 +1,3 @@
|
||||
class Test2 {
|
||||
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
arr.map(x => x / DIVIDER);
|
||||
3
packages/babel-cli/test/fixtures/babel/dir --out-dir --relative/options.json
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/dir --out-dir --relative/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"args": ["**/src", "--out-dir", "../lib", "--relative"]
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var Test1 = function Test1() {
|
||||
_classCallCheck(this, Test1);
|
||||
};
|
||||
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
arr.map(function (x) {
|
||||
return x * MULTIPLIER;
|
||||
});
|
||||
@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var Test2 = function Test2() {
|
||||
_classCallCheck(this, Test2);
|
||||
};
|
||||
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
arr.map(function (x) {
|
||||
return x / DIVIDER;
|
||||
});
|
||||
4
packages/babel-cli/test/fixtures/babel/dir --out-dir --relative/stdout.txt
vendored
Normal file
4
packages/babel-cli/test/fixtures/babel/dir --out-dir --relative/stdout.txt
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
package1/src/bar/bar1.js -> package1/lib/bar/bar1.js
|
||||
package1/src/foo1.js -> package1/lib/foo1.js
|
||||
package2/src/bar/bar2.js -> package2/lib/bar/bar2.js
|
||||
package2/src/foo2.js -> package2/lib/foo2.js
|
||||
4
packages/babel-cli/test/fixtures/babel/error --out-dir --out-file/options.json
vendored
Normal file
4
packages/babel-cli/test/fixtures/babel/error --out-dir --out-file/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"args": ["--out-dir", "lib"],
|
||||
"stderrContains": true
|
||||
}
|
||||
1
packages/babel-cli/test/fixtures/babel/error --out-dir --out-file/stderr.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/error --out-dir --out-file/stderr.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
filenames required for --out-dir
|
||||
4
packages/babel-cli/test/fixtures/babel/error --out-dir/options.json
vendored
Normal file
4
packages/babel-cli/test/fixtures/babel/error --out-dir/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"args": ["--out-dir", "lib", "--out-file", "foo.js"],
|
||||
"stderrContains": true
|
||||
}
|
||||
1
packages/babel-cli/test/fixtures/babel/error --out-dir/stderr.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/error --out-dir/stderr.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
cannot have --out-file and --out-dir
|
||||
4
packages/babel-cli/test/fixtures/babel/error --relative/options.json
vendored
Normal file
4
packages/babel-cli/test/fixtures/babel/error --relative/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"args": ["--relative"],
|
||||
"stderrContains": true
|
||||
}
|
||||
1
packages/babel-cli/test/fixtures/babel/error --relative/stderr.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/error --relative/stderr.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
output directory required for --relative
|
||||
4
packages/babel-cli/test/fixtures/babel/error --skip-initial-build/options.json
vendored
Normal file
4
packages/babel-cli/test/fixtures/babel/error --skip-initial-build/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"args": ["--skip-initial-build"],
|
||||
"stderrContains": true
|
||||
}
|
||||
1
packages/babel-cli/test/fixtures/babel/error --skip-initial-build/stderr.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/error --skip-initial-build/stderr.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
--skip-initial-build requires --watch
|
||||
4
packages/babel-cli/test/fixtures/babel/error --watch no filename/options.json
vendored
Normal file
4
packages/babel-cli/test/fixtures/babel/error --watch no filename/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"args": ["--watch"],
|
||||
"stderrContains": true
|
||||
}
|
||||
1
packages/babel-cli/test/fixtures/babel/error --watch no filename/stderr.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/error --watch no filename/stderr.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
--watch requires filenames
|
||||
4
packages/babel-cli/test/fixtures/babel/error --watch/options.json
vendored
Normal file
4
packages/babel-cli/test/fixtures/babel/error --watch/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"args": ["--watch"],
|
||||
"stderrContains": true
|
||||
}
|
||||
1
packages/babel-cli/test/fixtures/babel/error --watch/stderr.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/error --watch/stderr.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
--watch requires --out-file or --out-dir
|
||||
4
packages/babel-cli/test/fixtures/babel/error wrong filename/options.json
vendored
Normal file
4
packages/babel-cli/test/fixtures/babel/error wrong filename/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"args": ["foo.json"],
|
||||
"stderrContains": true
|
||||
}
|
||||
1
packages/babel-cli/test/fixtures/babel/error wrong filename/stderr.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/error wrong filename/stderr.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
foo.json doesn't exist
|
||||
1
packages/babel-cli/test/fixtures/babel/filename --out-dir --relative/in-files/src/foo.js
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/filename --out-dir --relative/in-files/src/foo.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
arr.map(x => x * MULTIPLIER);
|
||||
3
packages/babel-cli/test/fixtures/babel/filename --out-dir --relative/options.json
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/filename --out-dir --relative/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"args": ["src/foo.js", "--out-dir", "../lib", "--relative"]
|
||||
}
|
||||
5
packages/babel-cli/test/fixtures/babel/filename --out-dir --relative/out-files/lib/foo.js
vendored
Normal file
5
packages/babel-cli/test/fixtures/babel/filename --out-dir --relative/out-files/lib/foo.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
arr.map(function (x) {
|
||||
return x * MULTIPLIER;
|
||||
});
|
||||
1
packages/babel-cli/test/fixtures/babel/filename --out-dir --relative/stdout.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/filename --out-dir --relative/stdout.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
src/foo.js -> lib/foo.js
|
||||
1
packages/babel-cli/test/fixtures/babel/filename --out-dir/in-files/src/foo.js
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/filename --out-dir/in-files/src/foo.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
arr.map(x => x * MULTIPLIER);
|
||||
3
packages/babel-cli/test/fixtures/babel/filename --out-dir/options.json
vendored
Normal file
3
packages/babel-cli/test/fixtures/babel/filename --out-dir/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"args": ["src/foo.js", "--out-dir", "lib"]
|
||||
}
|
||||
5
packages/babel-cli/test/fixtures/babel/filename --out-dir/out-files/lib/foo.js
vendored
Normal file
5
packages/babel-cli/test/fixtures/babel/filename --out-dir/out-files/lib/foo.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
arr.map(function (x) {
|
||||
return x * MULTIPLIER;
|
||||
});
|
||||
1
packages/babel-cli/test/fixtures/babel/filename --out-dir/stdout.txt
vendored
Normal file
1
packages/babel-cli/test/fixtures/babel/filename --out-dir/stdout.txt
vendored
Normal file
@ -0,0 +1 @@
|
||||
src/foo.js -> lib/foo.js
|
||||
@ -25,6 +25,7 @@ type MergeOptions = {
|
||||
};
|
||||
|
||||
const optionNames = new Set([
|
||||
"relative",
|
||||
"filename",
|
||||
"filenameRelative",
|
||||
"inputSourceMap",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user