Add ability to add root folder to meta.json (only affects upload)

This commit is contained in:
Fabian Stamm 2020-10-14 16:34:18 +02:00
parent 13bc1b07cd
commit f3f6f0d7bc
4 changed files with 25 additions and 14 deletions

View File

@ -42,23 +42,27 @@ async function runHooks(hooks: undefined | string | string[]) {
}
export default async function publish(options: { dry: boolean }) {
const meta: IMeta = await getMeta();
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");
if (meta.hooks) {
log("Running prepublish hooks");
await runHooks(meta.hooks.prepublish);
}
const tmpDir = await Deno.makeTempDir();
const packedFile = (await Deno.makeTempFile()) + ".tar";
try {
const walker = FS.walk(".", {
const walker = FS.walk(meta.root || ".", {
includeDirs: false,
includeFiles: true,
match: meta.files.map((file) => Path.globToRegExp(file)),
@ -66,17 +70,18 @@ export default async function publish(options: { dry: boolean }) {
log("Copying files to package to", tmpDir);
const copy = async (path: string) => {
const dest = Path.join(tmpDir, path);
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");
await copy("meta.json", true);
for await (const file of walker) {
await copy(file.path);
log("Adding file:", file.path);
}
log("Compressing files into", packedFile);
@ -88,9 +93,14 @@ export default async function publish(options: { dry: boolean }) {
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) {
log("Uploading new package version");
await fetch(url, {
method: "POST",
body: await Deno.readFile(packedFile),

View File

@ -9,6 +9,7 @@ export interface IMeta {
contributors?: string[];
deprecated?: boolean;
files: string[];
root?: string;
hooks?: {
prepublish?: string | string[];
postpublish?: string | string[];

View File

@ -1,6 +1,6 @@
{
"name": "@denreg-cli",
"version": "0.2.6",
"version": "0.2.7",
"description": "CLI for the DenReg package registry",
"author": "Fabian Stamm <dev@fabianstamm.de>",
"contributors": [],

View File

@ -1 +1 @@
export const version = "0.2.6"
export const version = "0.2.7"