2020-07-28 17:43:58 +00:00
|
|
|
import { Colors, Path, FS, Compress, Base64 } from "../deps.ts";
|
|
|
|
import { getMeta, IMeta, log, getConfig } from "../global.ts";
|
2021-04-28 09:47:52 +00:00
|
|
|
import { runHooks } from "../helper/run_script.ts";
|
2020-09-14 21:47:48 +00:00
|
|
|
import { ServerError } from "../helper/server_error.ts";
|
2020-07-28 17:43:58 +00:00
|
|
|
|
2020-08-16 09:39:56 +00:00
|
|
|
export default async function publish(options: { dry: boolean }) {
|
2020-10-14 14:34:18 +00:00
|
|
|
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
|
2020-07-28 17:43:58 +00:00
|
|
|
|
|
|
|
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();
|
2020-08-16 09:39:56 +00:00
|
|
|
const packedFile = (await Deno.makeTempFile()) + ".tar";
|
2020-07-28 17:43:58 +00:00
|
|
|
|
|
|
|
try {
|
2020-10-14 14:34:18 +00:00
|
|
|
const walker = FS.walk(meta.root || ".", {
|
2020-07-28 17:43:58 +00:00
|
|
|
includeDirs: false,
|
|
|
|
includeFiles: true,
|
|
|
|
match: meta.files.map((file) => Path.globToRegExp(file)),
|
|
|
|
});
|
|
|
|
|
|
|
|
log("Copying files to package to", tmpDir);
|
|
|
|
|
2020-10-14 14:34:18 +00:00
|
|
|
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);
|
2020-07-28 17:43:58 +00:00
|
|
|
await FS.ensureDir(Path.dirname(dest));
|
|
|
|
await FS.copy(path, dest);
|
|
|
|
};
|
|
|
|
|
2020-10-14 14:34:18 +00:00
|
|
|
await copy("meta.json", true);
|
2020-07-28 17:43:58 +00:00
|
|
|
|
|
|
|
for await (const file of walker) {
|
|
|
|
await copy(file.path);
|
|
|
|
}
|
|
|
|
|
2020-08-16 09:39:56 +00:00
|
|
|
log("Compressing files into", packedFile);
|
2020-07-28 17:43:58 +00:00
|
|
|
|
|
|
|
await Compress.Tar.compress(tmpDir, packedFile, {
|
|
|
|
excludeSrc: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const url = new URL(getConfig("registry"));
|
|
|
|
url.pathname = "/api/package/" + meta.name;
|
|
|
|
|
2020-10-14 14:34:18 +00:00
|
|
|
console.log(
|
|
|
|
"Pushing version",
|
|
|
|
Colors.blue(meta.version),
|
|
|
|
"to repository"
|
|
|
|
);
|
|
|
|
|
2020-08-16 09:39:56 +00:00
|
|
|
if (!options.dry) {
|
|
|
|
log("Uploading new package version");
|
|
|
|
await fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
body: await Deno.readFile(packedFile),
|
|
|
|
headers: {
|
|
|
|
Authorization:
|
|
|
|
"Basic " +
|
|
|
|
Base64.encode(
|
|
|
|
getConfig("username") + ":" + getConfig("password")
|
|
|
|
),
|
|
|
|
},
|
2020-07-31 18:18:28 +00:00
|
|
|
})
|
2020-08-16 09:39:56 +00:00
|
|
|
.then((res) =>
|
|
|
|
res.status === 200
|
|
|
|
? res.json()
|
2020-09-14 21:47:48 +00:00
|
|
|
: Promise.reject(new ServerError(res.statusText))
|
2020-08-16 09:39:56 +00:00
|
|
|
)
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.success) {
|
2020-09-14 21:47:48 +00:00
|
|
|
throw new ServerError(res.message);
|
2020-08-16 09:39:56 +00:00
|
|
|
} else {
|
|
|
|
console.log(Colors.green("Upload successfull"));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2020-09-14 21:47:48 +00:00
|
|
|
//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));
|
2020-08-16 09:39:56 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log(Colors.yellow("Dry run. Skipping upload"));
|
|
|
|
}
|
2020-08-16 17:21:17 +00:00
|
|
|
|
|
|
|
if (meta.hooks) {
|
|
|
|
log("Running postpublish hooks");
|
|
|
|
await runHooks(meta.hooks.postpublish);
|
|
|
|
}
|
2020-07-28 17:43:58 +00:00
|
|
|
} finally {
|
|
|
|
await Deno.remove(tmpDir, { recursive: true });
|
|
|
|
await Deno.remove(packedFile);
|
|
|
|
}
|
|
|
|
}
|