Skip to content

Instantly share code, notes, and snippets.

@Ragzouken
Created September 20, 2020 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ragzouken/3e11cf16d68198f20ecc079c7fc6466d to your computer and use it in GitHub Desktop.
Save Ragzouken/3e11cf16d68198f20ecc079c7fc6466d to your computer and use it in GitHub Desktop.
neocities uploader (deno)
import { parse } from "https://deno.land/std/flags/mod.ts";
import { expandGlob } from "https://deno.land/std/fs/expand_glob.ts";
import { relative, join } from "https://deno.land/std/path/mod.ts";
async function run() {
const args = parse(Deno.args, { default: { root: ".", sub: "", "dry-run": false }});
const token = args.token || await Deno.readTextFile("key.txt");
const glob = args._[0] as string;
const root = args.root.toString();
const sub = args.sub.toString();
const form = new FormData();
for await (const file of expandGlob(glob, { root })) {
if (file.isFile) {
const target = join(sub, relative(root, file.path)).replace(/\\/g, '/');
console.log(relative(Deno.cwd(), file.path), "->", target);
const data = await Deno.readFile(file.path);
const blob = new Blob([data], { type: "text/plain" });
form.append(target, blob);
}
}
if (args["dry-run"]) return;
const response = await fetch("https://neocities.org/api/upload", {
method: "POST",
headers: {
"Authorization": `Bearer ${token}`,
},
body: form,
});
const json = await response.json();
console.log(`${json.result}: ${json.message}`);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment