feat(create): resolve giget source for examples and URLs

Made-with: Cursor
This commit is contained in:
zuiidea
2026-04-14 22:31:24 +08:00
parent 831aef8a7b
commit 26b002e80a
2 changed files with 59 additions and 0 deletions
@@ -0,0 +1,20 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { resolveExampleSource } from "./resolve-example-source.js";
describe("resolveExampleSource", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("resolves short example with default repo and env ref", () => {
vi.stubEnv("CREATE_ANTD_ADMIN_REF", "main");
const r = resolveExampleSource({ example: "basic" });
expect(r.gigetSource).toBe("github:zuiidea/antd-admin/apps/basic#main");
});
it("passes through https URL", () => {
const url = "https://api.github.com/repos/zuiidea/antd-admin/tarball/main";
const r = resolveExampleSource({ example: url });
expect(r.gigetSource).toBe(url);
});
});
@@ -0,0 +1,39 @@
import { DEFAULT_GITHUB_REPO, ENV_REPO } from "./constants.js";
import { CliError } from "./errors.js";
import { resolveGitRef } from "./resolve-ref.js";
export type ResolvedExampleSource = { gigetSource: string; displayName: string };
function defaultRepo(): string {
return process.env[ENV_REPO]?.trim() || DEFAULT_GITHUB_REPO;
}
export function resolveExampleSource(input: {
example?: string;
examplePath?: string;
}): ResolvedExampleSource {
const example = input.example?.trim();
if (!example) {
throw new CliError("example is required", "INVALID_INPUT");
}
const { examplePath } = input;
const ref = resolveGitRef();
if (/^https?:\/\//i.test(example)) {
return { gigetSource: example, displayName: example };
}
const repo = defaultRepo();
if (!example.includes("/")) {
const sub = examplePath
? `apps/${example}/${examplePath}`.replace(/\/+/g, "/")
: `apps/${example}`;
const gigetSource = `github:${repo}/${sub}#${ref}`;
return { gigetSource, displayName: example };
}
const pathPart = examplePath ? `${example}/${examplePath}` : example;
const gigetSource = `github:${pathPart}#${ref}`;
return { gigetSource, displayName: pathPart };
}