@mesh-tech/mesh-cli
Version:
CLI for Mesh platform development utilities
51 lines (50 loc) • 1.96 kB
JavaScript
import { writeFile, mkdir } from "node:fs/promises";
import { join, dirname } from "node:path";
import { createVcsFolderReader, deriveVcsBaseUrl } from "@mesh-tech/agent-targets";
import { resolveTarget } from "../agent-api-client.js";
import { getValidToken } from "../login.js";
import { resolveToken } from "./common.js";
import { logInfo } from "../../utils/log.js";
export async function getCommand(repo, path, opts) {
let vcsBaseUrl;
let token;
if (opts.target) {
const t = resolveTarget({ target: opts.target });
vcsBaseUrl = deriveVcsBaseUrl(t.apiBaseUrl);
token = t.token ?? (await getValidToken(t.loginContext ?? "mesh.dev")) ?? undefined;
}
else {
vcsBaseUrl = (opts.url ?? process.env.VCS_URL)?.replace(/\/$/, "") ?? null;
token = await resolveToken(opts);
}
if (!vcsBaseUrl)
throw new Error("vcs get requires --target <name> or --url <vcs-url>");
if (!token)
throw new Error("no token: pass --target (after mesh login), or --token / --context");
const reader = createVcsFolderReader({ vcsBaseUrl, token });
try {
const { ref, files } = await reader.readPath(repo, path);
if (files.length === 0) {
logInfo(`no files at ${repo}:${path}`);
return;
}
if (opts.output) {
for (const f of files) {
const abs = join(opts.output, f.path);
await mkdir(dirname(abs), { recursive: true });
await writeFile(abs, f.contents);
}
logInfo(`wrote ${files.length} file(s) from ${repo}:${path} (${ref}) → ${opts.output}`);
}
else {
for (const f of files) {
if (files.length > 1)
process.stdout.write(`\n=== ${f.path} ===\n`);
process.stdout.write(f.contents);
}
}
}
finally {
await reader.cleanup();
}
}