feat(create): add constants, CliError, and git ref resolver

Made-with: Cursor
This commit is contained in:
zuiidea
2026-04-14 22:29:10 +08:00
parent 9b8fd96a7a
commit 578f7f5082
3 changed files with 51 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
export const DEFAULT_GITHUB_REPO = "zuiidea/antd-admin";
/** Override full repo slug `owner/name` */
export const ENV_REPO = "CREATE_ANTD_ADMIN_REPO";
/** Override ref: branch name, tag, or commit sha */
export const ENV_REF = "CREATE_ANTD_ADMIN_REF";
export const EXIT_SUCCESS = 0;
export const EXIT_ERROR = 1;
export const EXIT_SIGINT = 130;
+14
View File
@@ -0,0 +1,14 @@
export class CliError extends Error {
constructor(
message: string,
public readonly code:
| "INVALID_INPUT"
| "DOWNLOAD_FAILED"
| "TRANSFORM_FAILED"
| "INSTALL_FAILED",
public readonly exitCode = 1
) {
super(message);
this.name = "CliError";
}
}
+26
View File
@@ -0,0 +1,26 @@
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { ENV_REF } from "./constants.js";
/**
* Priority: CREATE_ANTD_ADMIN_REF env → read sibling package.json version as `v${version}` → `main` on failure.
* When bundled to dist/cli.js, ../package.json is packages/create/package.json.
*/
export function resolveGitRef(): string {
const env = process.env[ENV_REF]?.trim();
if (env) return env;
try {
const pkgPath = path.join(
path.dirname(fileURLToPath(import.meta.url)),
"..",
"package.json"
);
const raw = fs.readFileSync(pkgPath, "utf8");
const { version } = JSON.parse(raw) as { version: string };
return `v${version}`;
} catch {
return "main";
}
}