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:
Lukas Geiger 2017-09-10 02:38:06 +02:00 committed by Justin Ridgewell
parent 5565e1b406
commit b6467a68ca
41 changed files with 127 additions and 7 deletions

View File

@ -7,7 +7,7 @@ import fs from "fs";
import * as util from "./util"; import * as util from "./util";
export default function(commander, filenames, opts) { export default function(commander, filenames, opts) {
function write(src, relative) { function write(src, relative, base) {
if (!util.isCompilableExtension(relative, commander.extensions)) { if (!util.isCompilableExtension(relative, commander.extensions)) {
return false; return false;
} }
@ -15,7 +15,7 @@ export default function(commander, filenames, opts) {
// remove extension and then append back on .js // remove extension and then append back on .js
relative = relative.replace(/\.(\w*?)$/, "") + ".js"; relative = relative.replace(/\.(\w*?)$/, "") + ".js";
const dest = path.join(commander.outDir, relative); const dest = getDest(commander, relative, base);
const data = util.compile( const data = util.compile(
src, src,
@ -45,11 +45,16 @@ export default function(commander, filenames, opts) {
return true; return true;
} }
function handleFile(src, filename) { function getDest(commander, filename, base) {
const didWrite = write(src, filename); 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) { if (!didWrite && commander.copyFiles) {
const dest = path.join(commander.outDir, filename); const dest = getDest(commander, filename, base);
outputFileSync(dest, fs.readFileSync(src)); outputFileSync(dest, fs.readFileSync(src));
util.chmod(src, dest); util.chmod(src, dest);
} }
@ -68,10 +73,10 @@ export default function(commander, filenames, opts) {
} }
util.readdir(dirname).forEach(function(filename) { util.readdir(dirname).forEach(function(filename) {
const src = path.join(dirname, filename); const src = path.join(dirname, filename);
handleFile(src, filename); handleFile(src, filename, dirname);
}); });
} else { } else {
write(filename, filename); write(filename, path.basename(filename), path.dirname(filename));
} }
} }

View File

@ -142,6 +142,10 @@ commander.option(
"-d, --out-dir [out]", "-d, --out-dir [out]",
"Compile an input directory of modules into an output directory", "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( commander.option(
"-D, --copy-files", "-D, --copy-files",
"When compiling a directory copy over non-compilable 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"); 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.watch) {
if (!commander.outFile && !commander.outDir) { if (!commander.outFile && !commander.outDir) {
errors.push("--watch requires --out-file or --out-dir"); errors.push("--watch requires --out-file or --out-dir");

View File

@ -0,0 +1 @@
arr.map(x => x * MULTIPLIER);

View File

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

View File

@ -0,0 +1,5 @@
"use strict";
arr.map(function (x) {
return x * MULTIPLIER;
});

View File

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

View File

@ -0,0 +1,3 @@
class Test1 {
}

View File

@ -0,0 +1 @@
arr.map(x => x * MULTIPLIER);

View File

@ -0,0 +1,3 @@
class Test2 {
}

View File

@ -0,0 +1 @@
arr.map(x => x / DIVIDER);

View File

@ -0,0 +1,3 @@
{
"args": ["**/src", "--out-dir", "../lib", "--relative"]
}

View File

@ -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);
};

View File

@ -0,0 +1,5 @@
"use strict";
arr.map(function (x) {
return x * MULTIPLIER;
});

View File

@ -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);
};

View File

@ -0,0 +1,5 @@
"use strict";
arr.map(function (x) {
return x / DIVIDER;
});

View 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

View File

@ -0,0 +1,4 @@
{
"args": ["--out-dir", "lib"],
"stderrContains": true
}

View File

@ -0,0 +1 @@
filenames required for --out-dir

View File

@ -0,0 +1,4 @@
{
"args": ["--out-dir", "lib", "--out-file", "foo.js"],
"stderrContains": true
}

View File

@ -0,0 +1 @@
cannot have --out-file and --out-dir

View File

@ -0,0 +1,4 @@
{
"args": ["--relative"],
"stderrContains": true
}

View File

@ -0,0 +1 @@
output directory required for --relative

View File

@ -0,0 +1,4 @@
{
"args": ["--skip-initial-build"],
"stderrContains": true
}

View File

@ -0,0 +1 @@
--skip-initial-build requires --watch

View File

@ -0,0 +1,4 @@
{
"args": ["--watch"],
"stderrContains": true
}

View File

@ -0,0 +1 @@
--watch requires filenames

View File

@ -0,0 +1,4 @@
{
"args": ["--watch"],
"stderrContains": true
}

View File

@ -0,0 +1 @@
--watch requires --out-file or --out-dir

View File

@ -0,0 +1,4 @@
{
"args": ["foo.json"],
"stderrContains": true
}

View File

@ -0,0 +1 @@
foo.json doesn't exist

View File

@ -0,0 +1 @@
arr.map(x => x * MULTIPLIER);

View File

@ -0,0 +1,3 @@
{
"args": ["src/foo.js", "--out-dir", "../lib", "--relative"]
}

View File

@ -0,0 +1,5 @@
"use strict";
arr.map(function (x) {
return x * MULTIPLIER;
});

View File

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

View File

@ -0,0 +1 @@
arr.map(x => x * MULTIPLIER);

View File

@ -0,0 +1,3 @@
{
"args": ["src/foo.js", "--out-dir", "lib"]
}

View File

@ -0,0 +1,5 @@
"use strict";
arr.map(function (x) {
return x * MULTIPLIER;
});

View File

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

View File

@ -25,6 +25,7 @@ type MergeOptions = {
}; };
const optionNames = new Set([ const optionNames = new Set([
"relative",
"filename", "filename",
"filenameRelative", "filenameRelative",
"inputSourceMap", "inputSourceMap",