Allow specifying a custom folder

This commit is contained in:
arne 2023-02-03 09:34:30 +01:00
commit 2a66af3091
2 changed files with 14 additions and 7 deletions

15
main.ts
View file

@ -1,3 +1,5 @@
import * as path from 'https://deno.land/std@0.102.0/path/mod.ts';
const MASTODON_INSTANCE_URL = Deno.env.get("MASTODON_INSTANCE_URL"); // e.g. https://botsin.space
const MASTODON_ACCESS_TOKEN = Deno.env.get("MASTODON_ACCESS_TOKEN"); // you can get this at $INSTANCE_URL/settings/applications
@ -33,17 +35,19 @@ const api = (
// first we need to find out which files we can post, and which ones we already
// have posted
const scriptDir = path.dirname(path.fromFileUrl(Deno.mainModule))
const imgDir = path.resolve(scriptDir, Deno.args[0] || 'posts')
console.log("Picking file to post…");
const alreadyPostedFiles = new Set();
for await (const dirEntry of Deno.readDir("./posts/.posted")) {
for await (const dirEntry of Deno.readDir(`${imgDir}/.posted`)) {
if (dirEntry.isSymlink) {
alreadyPostedFiles.add(dirEntry.name);
}
}
const availableFiles = [];
for await (const dirEntry of Deno.readDir("./posts/")) {
for await (const dirEntry of Deno.readDir(imgDir)) {
if (dirEntry.name === ".gitignore") continue;
if (dirEntry.isFile && !alreadyPostedFiles.has(dirEntry.name)) {
@ -60,7 +64,7 @@ if (availableFiles.length === 0) {
const fileToPost =
availableFiles[Math.floor(Math.random() * availableFiles.length)];
const bytes = await Deno.readFile(`./posts/${fileToPost}`);
const bytes = await Deno.readFile(`${imgDir}/${fileToPost}`);
console.log(`Will post file ${fileToPost}`);
@ -117,7 +121,8 @@ const { data: statusRequest } = await api("/api/v1/statuses", {
console.log("statusRequest", statusRequest);
console.log("Linking posted file so we can skip it in the future…");
// FIXME: This requires unrestricted `--allow-read` and `--allow-write` at the;
// FIXME: This requires unrestricted `--allow-read` and `--allow-write`;
// deno should provide a finer-grained permission prompt here
await Deno.symlink(`../${fileToPost}`, `./posts/.posted/${fileToPost}`);
// see https://github.com/denoland/deno/issues/9607
await Deno.symlink(`../${fileToPost}`, `${imgDir}/.posted/${fileToPost}`);
console.log("Done!");