feat(cli): implement modular command architecture with build config
Publish to Gitea Registry / publish (push) Failing after 19m9s

- Refactor CLI entry point to use modular command structure
- Add list command with placeholder implementation
- Set up type definitions for command interface
- Configure TypeScript build and package files for distribution
- Add dist output to .gitignore
This commit is contained in:
yangzhaohan
2026-06-22 16:23:22 +08:00
parent c6f4311360
commit a258407fae
9 changed files with 338 additions and 4 deletions
+41
View File
@@ -0,0 +1,41 @@
import { resolveConfig } from "../lib/config.js";
import { loadIndex } from "../lib/index-loader.js";
import { formatTable } from "../lib/table.js";
import type { Command } from "../types/index.js";
export const listCommand: Command = {
name: "list",
description: "显示可用积木列表",
run: async (args: string[]) => {
const config = resolveConfig(args);
const index = await loadIndex(config);
if (index.blocks.length === 0) {
console.log("(no blocks found)");
return;
}
const columns = [
{ key: "name", header: "Name", minWidth: 20, maxWidth: 30 },
{ key: "module", header: "Module", minWidth: 8, maxWidth: 12 },
{ key: "variants", header: "Variants", minWidth: 15, maxWidth: 45 },
{ key: "dependsOn", header: "Dependencies", minWidth: 15, maxWidth: 30 },
{ key: "description", header: "Description", minWidth: 15, maxWidth: 55 },
];
const rows = index.blocks.map((block) => ({
name: block.name,
module: block.module,
variants:
block.variants.length > 0
? block.variants.map((v) => v.label).join(", ")
: "-",
dependsOn:
block.dependsOn.length > 0 ? block.dependsOn.join(", ") : "-",
description: block.description,
}));
console.log(formatTable(columns, rows));
},
};
+26 -1
View File
@@ -1,5 +1,8 @@
#!/usr/bin/env node
import { listCommand } from "./commands/list.js";
import type { Command } from "./types/index.js";
const USAGE = `
Brick CLI — 源码级 Spring Boot 脚手架
@@ -14,4 +17,26 @@ Examples:
brick list
`;
console.log(USAGE);
const commands = new Map<string, Command>();
commands.set(listCommand.name, listCommand);
async function main(): Promise<void> {
const args = process.argv.slice(2);
const commandName = args[0];
if (!commandName) {
console.log(USAGE);
process.exit(0);
}
const command = commands.get(commandName);
if (!command) {
console.error(`Unknown command: ${commandName}`);
console.log(USAGE);
process.exit(1);
}
await command.run(args.slice(1));
}
main();
+30
View File
@@ -0,0 +1,30 @@
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 };
}
+62
View File
@@ -0,0 +1,62 @@
import { readFile } from "node:fs/promises";
import { load } from "js-yaml";
import type { BlockIndex, BrickConfig } from "../types/index.js";
export async function loadIndex(config: BrickConfig): Promise<BlockIndex> {
let raw: string;
if (config.isRemote) {
let response: Response;
try {
response = await fetch(config.source);
} catch {
process.stderr.write(
`Error: Failed to fetch index from "${config.source}"\n`
);
process.exit(1);
}
if (!response.ok) {
process.stderr.write(
`Error: Failed to fetch index from "${config.source}" (HTTP ${response.status})\n`
);
process.exit(1);
}
raw = await response.text();
} else {
try {
raw = await readFile(config.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`
);
} else {
process.stderr.write(`Error: ${(err as Error).message}\n`);
}
process.exit(1);
}
}
let result: unknown;
try {
result = load(raw);
} catch (err: unknown) {
process.stderr.write(
`Error: Failed to parse index YAML: ${(err as Error).message}\n`
);
process.exit(1);
}
const data = result as Record<string, unknown>;
if (!data || !Array.isArray(data.blocks)) {
process.stderr.write(
'Error: Invalid index format — expected a "blocks" array\n'
);
process.exit(1);
}
return { blocks: data.blocks as BlockIndex["blocks"] };
}
+103
View File
@@ -0,0 +1,103 @@
interface ColumnDef {
key: string;
header: string;
minWidth?: number;
maxWidth?: number;
width?: number;
}
function visualWidth(str: string): number {
let width = 0;
for (const ch of str) {
const code = ch.codePointAt(0) ?? 0;
// CJK Unified, Compatibility, Extension ranges, plus fullwidth forms
if (
(code >= 0x4e00 && code <= 0x9fff) ||
(code >= 0x3400 && code <= 0x4dbf) ||
(code >= 0x20000 && code <= 0x2a6df) ||
(code >= 0xf900 && code <= 0xfaff) ||
(code >= 0xff01 && code <= 0xff60) ||
(code >= 0xffe0 && code <= 0xffe6)
) {
width += 2;
} else {
width += 1;
}
}
return width;
}
function padToWidth(str: string, targetWidth: number): string {
let padded = str;
let current = visualWidth(str);
while (current < targetWidth) {
padded += " ";
current += 1;
}
return padded;
}
function truncateToWidth(str: string, maxWidth: number): string {
let result = "";
let vw = 0;
for (const ch of str) {
const charW = visualWidth(ch);
if (vw + charW > maxWidth - 3) {
result += "...";
break;
}
result += ch;
vw += charW;
}
if (visualWidth(result) <= maxWidth && result.length < str.length) {
result += "...";
}
return result;
}
export function formatTable(columns: ColumnDef[], rows: Record<string, string>[]): string {
const minW = 8;
const maxW = 60;
const resolved = columns.map((col) => {
const headerW = visualWidth(col.header);
let dataW = 0;
for (const row of rows) {
const val = row[col.key] ?? "-";
dataW = Math.max(dataW, visualWidth(val));
}
const natural = Math.max(headerW, dataW);
const lo = col.minWidth ?? minW;
const hi = col.maxWidth ?? maxW;
const width = Math.max(lo, Math.min(hi, natural));
return { ...col, width };
});
const padCell = (col: ColumnDef, text: string): string => {
if (visualWidth(text) > col.width!) {
return truncateToWidth(text, col.width!);
}
return padToWidth(text, col.width!);
};
const lines: string[] = [];
const headerLine = resolved
.map((col) => padCell(col, col.header))
.join(" ");
lines.push(headerLine);
const sepLine = resolved
.map((col) => "-".repeat(col.width!))
.join(" ");
lines.push(sepLine);
for (const row of rows) {
const line = resolved
.map((col) => padCell(col, row[col.key] ?? "-"))
.join(" ");
lines.push(line);
}
return lines.join("\n");
}
+30
View File
@@ -0,0 +1,30 @@
export interface BlockVariant {
branch: string;
label: string;
}
export interface Block {
name: string;
version: string;
description: string;
language: string;
defaultBranch: string;
module: string;
variants: BlockVariant[];
dependsOn: string[];
}
export interface BlockIndex {
blocks: Block[];
}
export interface BrickConfig {
source: string;
isRemote: boolean;
}
export interface Command {
name: string;
description: string;
run: (args: string[]) => Promise<void>;
}