Created
September 20, 2020 16:54
-
-
Save Ragzouken/3e11cf16d68198f20ecc079c7fc6466d to your computer and use it in GitHub Desktop.
neocities uploader (deno)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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