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