import { Colors, Path, FS, Compress, Base64 } from "../deps.ts"; import { getMeta, IMeta, log, getConfig } from "../global.ts"; export default async function publish() { const meta: IMeta = await getMeta(); if (!meta.name) throw new Error("name is not set in meta.json"); if (!meta.version) throw new Error("version is not set in meta.json"); if (!meta.files || !Array.isArray(meta.files) || meta.files.length <= 0) throw new Error("files is not set or empty in meta.json"); const tmpDir = await Deno.makeTempDir(); const packedFile = await Deno.makeTempFile(); try { const walker = FS.walk(".", { includeDirs: false, includeFiles: true, match: meta.files.map((file) => Path.globToRegExp(file)), }); log("Copying files to package to", tmpDir); const copy = async (path: string) => { const dest = Path.join(tmpDir, path); await FS.ensureDir(Path.dirname(dest)); await FS.copy(path, dest); }; await copy("meta.json"); for await (const file of walker) { await copy(file.path); } log("Compressing file"); await Compress.Tar.compress(tmpDir, packedFile, { excludeSrc: true, }); const url = new URL(getConfig("registry")); url.pathname = "/api/package/" + meta.name; log("Uploading new package version"); const res = await fetch(url, { method: "POST", body: await Deno.readFile(packedFile), headers: { Authorization: "Basic " + Base64.encode( getConfig("username") + ":" + getConfig("password") ), }, }).then((res) => (res.status === 200 ? res.json() : res.statusText)); log("Upload finished. Result:", res); if (typeof res === "string" || res.error) { console.log( Colors.red("Error: " + (typeof res == "string" ? res : res.error)) ); } else { if (res.success) { console.log(Colors.green("Upload successfull")); } } } finally { await Deno.remove(tmpDir, { recursive: true }); await Deno.remove(packedFile); } }