clone

push a directory of html from the terminal and get a four-character url back. a deno cli on cloudflare

feat/source-stable-push
nzip / cli / commands / where.ts
where.ts 2.1K
1// `nzip site where <target>` — print the local directory this machine pushed a site2// from. Purely local: reads the breadcrumb registry, never touches the server,3// so it stays fast enough for `cd "$(nzip site where personal:plan)"`.45import { resolveCliTarget } from "../lib/api.ts";6import { assertVaultAllowed, type Config } from "../lib/config.ts";7import { lookup } from "../lib/paths.ts";8import { amber, dim, emit, fail } from "../lib/fmt.ts";910export async function cmdWhere(11 config: Config,12 raw: string | undefined,13): Promise<void> {14 if (!raw) fail("usage: nzip site where <target>");1516 const q = (() => {17 try {18 if (/^[0-9a-f]{4}$/.test(raw)) {19 return { address: raw };20 }21 const [vault, alias] = resolveCliTarget(raw, config).split(":");22 return { vault, alias };23 } catch (e) {24 return fail((e as Error).message);25 }26 })();2728 const entry = await lookup(q);29 if (!entry) {30 fail(31 `no local path tracked for "${raw}" on this machine`,32 "it was pushed elsewhere (or before tracking existed) — push it again from its directory to record the path",33 );34 }35 if (q.address && config.allowVaults) {36 if (!entry.vault) {37 fail(`cannot validate the vault for locally tracked address ${q.address}`);38 }39 try {40 assertVaultAllowed(entry.vault, config);41 } catch (e) {42 return fail((e as Error).message);43 }44 }4546 let exists = false;47 try {48 await Deno.stat(entry.path); // dir, or a file for single-file pushes49 exists = true;50 } catch {51 exists = false;52 }5354 emit(55 () => {56 console.log(entry.path); // bare path on stdout for `cd "$(nzip site where …)"`57 if (!exists) {58 console.error(59 dim(` ${amber("↳")} directory no longer exists (moved or deleted)`),60 );61 }62 },63 {64 ok: true,65 target: raw,66 address: entry.address,67 vault: entry.vault ?? null,68 alias: entry.alias ?? null,69 path: entry.path,70 exists,71 url: entry.url,72 expiresAt: entry.expiresAt,73 pushedAt: entry.pushedAt,74 },75 );76}