add a --recursive option to deno cli

This commit is contained in:
Tomáš Mládek 2021-09-18 19:04:29 +02:00
parent 26ceccbde2
commit 944dc9ca5f

View file

@ -7,6 +7,7 @@ import DenoCollageModes, {
} from "./collages.ts";
import { CollageConfig } from "../src/common/types.ts";
import { choice, shuffle } from "../src/common/utils.ts";
import { walkSync } from "https://deno.land/std@0.107.0/fs/mod.ts";
const canvasKit = await init();
@ -16,7 +17,9 @@ const args = parse(Deno.args, {
"h": "height",
"o": "output",
"m": "mode",
"r": "recursive",
},
boolean: ["recursive"]
});
if (args["mode"] === true) {
@ -24,7 +27,7 @@ if (args["mode"] === true) {
Deno.exit(0);
}
const files: string[] = [];
const files: Set<string> = new Set();
const includeExtensions = Array.from(
String(args["include"] || "*.png, *.jpg").matchAll(/\*\.([\w]+)/g),
).map(([_, group]) => group);
@ -32,21 +35,27 @@ const includeExtensions = Array.from(
args["_"].forEach((arg) => {
arg = arg.toString();
if (Deno.statSync(arg).isDirectory) {
Array.from(Deno.readDirSync(arg)).filter((entry) =>
includeExtensions.length == 0 ||
includeExtensions.some((ext) => entry.name.endsWith(ext))
).forEach((entry) => files.push(`${arg}/${entry.name}`));
Array.from(
walkSync(arg, {
maxDepth: args["recursive"]
? Infinity
: 1,
includeDirs: false,
includeFiles: true,
exts: includeExtensions.length ? includeExtensions : undefined,
}),
).forEach((entry) => files.add(entry.path));
} else {
files.push(arg);
files.add(arg);
}
});
if (files.length < 2) {
if (files.size < 2) {
console.error("kollagen needs at least 2 images to work.");
Deno.exit(1);
}
const shuffledFiles = shuffle(files);
const shuffledFiles = shuffle(Array.from(files));
if (shuffledFiles.length > 25) {
console.debug(`Too many files available, shuffling and clamping to 25...`);
@ -67,7 +76,7 @@ const mode = modes.modes[modeKey];
console.log(
`Creating a "${mode.name}" collage...`,
);
console.debug(`Images: ${files.join(", ")}`);
console.debug(`Images: ${shuffledFiles.join(", ")}`);
const canvas = createCanvas(args["width"] || 640, args["height"] || 640);
const context = canvas.getContext("2d");