DenReg/cli/commands/publish.ts
User user 3cda4ee8c8
All checks were successful
continuous-integration/drone/push Build is passing
Improve upgrade script
2021-04-28 12:16:50 +02:00

111 lines
3.5 KiB
TypeScript

import { Colors, Path, FS, Compress, Base64 } from "../deps.ts";
import { getMeta, IMeta, log, getConfig } from "../global.ts";
import { runHooks } from "../helper/run_script.ts";
import { ServerError } from "../helper/server_error.ts";
import { checkPermOrExit } from "../helper/permission.ts";
export default async function publish(options: { dry: boolean }) {
const originalMeta = await getMeta();
if (originalMeta.hooks) {
log("Running prepublish hooks");
await runHooks(originalMeta.hooks.prepublish);
}
const meta = await getMeta();
//TODO: Output Diff between original and result meta
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()) + ".tar";
try {
const walker = FS.walk(meta.root || ".", {
includeDirs: false,
includeFiles: true,
match: meta.files.map((file) => Path.globToRegExp(file)),
});
log("Copying files to package to", tmpDir);
const copy = async (path: string, abs?: boolean) => {
const relative = abs ? path : Path.relative(meta.root || ".", path);
log("Adding file:", path, "as", relative);
const dest = Path.join(tmpDir, relative);
await FS.ensureDir(Path.dirname(dest));
await FS.copy(path, dest);
};
await copy("meta.json", true);
for await (const file of walker) {
await copy(file.path);
}
log("Compressing files into", packedFile);
await Compress.Tar.compress(tmpDir, packedFile, {
excludeSrc: true,
});
const url = new URL(getConfig("registry"));
url.pathname = "/api/package/" + meta.name;
console.log(
"Pushing version",
Colors.blue(meta.version),
"to repository"
);
if (!options.dry) {
await checkPermOrExit("net", "Net permission required for publishing");
log("Uploading new package version");
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()
: Promise.reject(new ServerError(res.statusText))
)
.then((res) => {
if (!res.success) {
throw new ServerError(res.message);
} else {
console.log(Colors.green("Upload successfull"));
}
})
.catch((err) => {
//import { ServerError } from "../helper/server_error.ts";
if (err instanceof ServerError)
console.log(Colors.red("Server Error: " + err.message));
else console.log(Colors.red("Error: " + err.message));
});
} else {
console.log(Colors.yellow("Dry run. Skipping upload"));
}
if (meta.hooks) {
log("Running postpublish hooks");
await runHooks(meta.hooks.postpublish);
}
} finally {
await Deno.remove(tmpDir, { recursive: true });
await Deno.remove(packedFile);
}
}