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

View File

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