cli takes directories as well as files

This commit is contained in:
Tomáš Mládek 2021-09-17 18:56:57 +02:00
parent cd76a36d99
commit 57f32ef643

View file

@ -19,14 +19,27 @@ const args = parse(Deno.args, {
}, },
}); });
if (args["_"].length < 2) { const files: string[] = [];
args["_"].forEach((arg) => {
arg = arg.toString();
if (Deno.statSync(arg).isDirectory) {
Array.from(Deno.readDirSync(arg)).forEach((entry) =>
files.push(`${arg}/${entry.name}`)
);
} else {
files.push(arg);
}
});
if (files.length < 2) {
console.error("kollagen needs at least 2 images to work."); console.error("kollagen needs at least 2 images to work.");
Deno.exit(1); Deno.exit(1);
} }
const images: ProxyImage[] = (await Promise.all( const images: ProxyImage[] = (await Promise.all(
args["_"].map(async (imageURL) => files.map(async (imageURL) =>
canvasKit.MakeImageFromEncoded(await Deno.readFile(imageURL.toString())) canvasKit.MakeImageFromEncoded(await Deno.readFile(imageURL))
), ),
)).map((image) => new ProxyImage(image!)); )).map((image) => new ProxyImage(image!));
@ -38,7 +51,7 @@ const mode = modes.modes[modeKey];
console.log( console.log(
`Creating a "${mode.name}" collage from ${images.length} images...`, `Creating a "${mode.name}" collage from ${images.length} images...`,
); );
console.debug(`Images: ${args["_"].join(", ")}`); console.debug(`Images: ${files.join(", ")}`);
const canvas = createCanvas(args["width"] || 640, args["height"] || 640); const canvas = createCanvas(args["width"] || 640, args["height"] || 640);
const context = canvas.getContext("2d"); const context = canvas.getContext("2d");