refactor(cli): migrate to commander framework and simplify config loading
Publish to Gitea Registry / publish (push) Failing after 4s

Replace hand-rolled CLI argument parsing with Commander for command
registration and option handling. Streamline config resolution by
removing the BrickConfig type and accepting a plain source string
with BRICK_SOURCE env var support.

- Replace manual CLI dispatch with Commander-based command tree
- Simplify resolveConfig/resolveSource to a single resolveSource function
- Remove BrickConfig interface in favor of direct source string
- Move isRemote check into index-loader as a local utility
- Update loadIndex signature to accept source string directly
This commit is contained in:
yangzhaohan
2026-06-22 16:46:44 +08:00
parent a258407fae
commit 35c351e716
7 changed files with 59 additions and 122 deletions
+4 -26
View File
@@ -1,30 +1,8 @@
import type { BrickConfig } from "../types/index.js";
const DEFAULT_SOURCE =
"https://gitea.synoth.com/synoth/brick-index/raw/branch/master/blocks.yaml";
function parseSourceFromArgs(args: string[]): string | undefined {
const idx = args.indexOf("--source");
if (idx !== -1 && idx + 1 < args.length) {
return args[idx + 1];
}
return undefined;
}
function isRemoteSource(source: string): boolean {
return source.startsWith("http://") || source.startsWith("https://");
}
export function resolveConfig(args: string[]): BrickConfig {
const cliSource = parseSourceFromArgs(args);
if (cliSource) {
return { source: cliSource, isRemote: isRemoteSource(cliSource) };
}
const envSource = process.env.BRICK_SOURCE;
if (envSource) {
return { source: envSource, isRemote: isRemoteSource(envSource) };
}
return { source: DEFAULT_SOURCE, isRemote: true };
export function resolveSource(cliSource?: string): string {
if (cliSource) return cliSource;
if (process.env.BRICK_SOURCE) return process.env.BRICK_SOURCE;
return DEFAULT_SOURCE;
}
+11 -8
View File
@@ -1,24 +1,27 @@
import { readFile } from "node:fs/promises";
import { load } from "js-yaml";
import type { BlockIndex, BrickConfig } from "../types/index.js";
import type { BlockIndex } from "../types/index.js";
export async function loadIndex(config: BrickConfig): Promise<BlockIndex> {
const isRemote = (source: string): boolean =>
source.startsWith("http://") || source.startsWith("https://");
export async function loadIndex(source: string): Promise<BlockIndex> {
let raw: string;
if (config.isRemote) {
if (isRemote(source)) {
let response: Response;
try {
response = await fetch(config.source);
response = await fetch(source);
} catch {
process.stderr.write(
`Error: Failed to fetch index from "${config.source}"\n`
`Error: Failed to fetch index from "${source}"\n`
);
process.exit(1);
}
if (!response.ok) {
process.stderr.write(
`Error: Failed to fetch index from "${config.source}" (HTTP ${response.status})\n`
`Error: Failed to fetch index from "${source}" (HTTP ${response.status})\n`
);
process.exit(1);
}
@@ -26,12 +29,12 @@ export async function loadIndex(config: BrickConfig): Promise<BlockIndex> {
raw = await response.text();
} else {
try {
raw = await readFile(config.source, "utf-8");
raw = await readFile(source, "utf-8");
} catch (err: unknown) {
const code = (err as NodeJS.ErrnoException)?.code;
if (code === "ENOENT") {
process.stderr.write(
`Error: Index file not found at "${config.source}"\n`
`Error: Index file not found at "${source}"\n`
);
} else {
process.stderr.write(`Error: ${(err as Error).message}\n`);