feat(cli): implement modular command architecture with build config
Publish to Gitea Registry / publish (push) Failing after 19m9s
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:
@@ -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 };
|
||||
}
|
||||
@@ -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"] };
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user