wallpapers: use cli args to pass paths, write json array to stdout

This commit is contained in:
Philippe Loctaux 2023-06-03 15:53:46 +02:00
parent cb8126bbdb
commit afd47527ff

View file

@ -1,11 +1,29 @@
import exifr from "npm:exifr@^7.1.3"; import exifr from "npm:exifr@^7.1.3";
import { writeJson } from "https://deno.land/x/jsonfile@1.0.0/mod.ts";
import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts"; import { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
import { parse } from "https://deno.land/std@0.190.0/flags/mod.ts";
const publicPath = "/public"; interface Arguments {
const wallpapersPath = "/wallpapers"; sourceDir?: string;
const imagesPath = `${Deno.cwd()}${publicPath}${wallpapersPath}`; destinationDir?: string;
}
const cli: Arguments = parse(Deno.args, {
string: ["sourceDir", "destinationDir"],
});
if (cli.sourceDir === undefined) {
console.error("Need source folder for images (relative directory where deno script is ran)");
Deno.exit(1);
}
if (cli.destinationDir === undefined) {
console.error("Need destination folder for images");
Deno.exit(1);
}
// Put all wallpapers here
const imagesPath = `${Deno.cwd()}/${cli.sourceDir}`;
interface MyWallpaper { interface MyWallpaper {
file: string; file: string;
@ -23,7 +41,8 @@ for await (const dirEntry of Deno.readDir(imagesPath)) {
continue; continue;
} }
console.log(`Processing ${dirEntry.name}`); // Don't pollute stdout, that's where the json will be put
console.error(`Processing ${dirEntry.name}`);
// Open file // Open file
const path = `${imagesPath}/${dirEntry.name}`; const path = `${imagesPath}/${dirEntry.name}`;
@ -47,7 +66,7 @@ for await (const dirEntry of Deno.readDir(imagesPath)) {
const location = data?.display_name; const location = data?.display_name;
// Final filename // Final filename
const file = `${wallpapersPath}/${dirEntry.name}`; const file = `${cli.destinationDir}/${dirEntry.name}`;
wallpapers.push({ wallpapers.push({
file, file,
@ -57,7 +76,5 @@ for await (const dirEntry of Deno.readDir(imagesPath)) {
} }
// write to /public/wallpapers.json // Put final array to stdout
const jsonPath = `${Deno.cwd()}${publicPath}/wallpapers.json`; console.log(JSON.stringify(wallpapers));
await writeJson(jsonPath, wallpapers);
console.log(`Wrote to ${jsonPath}`);