feat(cli): add create command with interactive block selection
Publish to Gitea Registry / publish (push) Failing after 1m32s
Publish to Gitea Registry / publish (push) Failing after 1m32s
- register create command in Commander program - add @inquirer/prompts dependency for interactive prompts - extend Block type with optional repo field - scaffold create command module with initial implementation
This commit is contained in:
@@ -0,0 +1,343 @@
|
||||
import { Command } from "commander";
|
||||
import { checkbox, select, input } from "@inquirer/prompts";
|
||||
import { resolve, basename } from "node:path";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { resolveSource } from "../lib/config.js";
|
||||
import { loadIndex } from "../lib/index-loader.js";
|
||||
import { inferRepoUrl, cloneBlock, cleanupTemp } from "../lib/git.js";
|
||||
import { applyTemplate } from "../lib/template.js";
|
||||
import { compileAiRules } from "../lib/ai-compiler.js";
|
||||
import type { Block, BrickJson } from "../types/index.js";
|
||||
|
||||
interface SelectedBlock {
|
||||
block: Block;
|
||||
branch: string;
|
||||
variantLabel: string;
|
||||
}
|
||||
|
||||
const STYLES: { value: string; label: string }[] = [
|
||||
{ value: "standard", label: "标准多模块 — 根 pom.xml 聚合所有积木子模块" },
|
||||
{
|
||||
value: "bom",
|
||||
label: "BOM 模式 — 根 pom + bom 子模块统一依赖版本管理",
|
||||
},
|
||||
{ value: "simple", label: "简单模式 — 不生成根 pom,积木模块平铺" },
|
||||
];
|
||||
|
||||
const AI_TOOLS: { value: string; label: string }[] = [
|
||||
{ value: "claude", label: "Claude Code" },
|
||||
{ value: "cursor", label: "Cursor" },
|
||||
{ value: "copilot", label: "GitHub Copilot" },
|
||||
{ value: "aider", label: "Aider" },
|
||||
{ value: "generic", label: "通用(AI_CONTEXT.md)" },
|
||||
];
|
||||
|
||||
export function registerCreate(program: Command): void {
|
||||
program
|
||||
.command("create <project-name>")
|
||||
.description("创建新项目")
|
||||
.action(async (projectName: string) => {
|
||||
try {
|
||||
await runCreate(projectName);
|
||||
} catch (err: unknown) {
|
||||
const msg = (err as Error).message ?? "";
|
||||
if (msg.includes("User force closed") || msg.includes("ExitPromptError")) {
|
||||
console.log("\n已取消。");
|
||||
process.exit(0);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function runCreate(projectName: string): Promise<void> {
|
||||
const cwd = process.cwd();
|
||||
|
||||
// 1. Load index
|
||||
const source = resolveSource();
|
||||
console.log(`[1/7] 拉取积木索引...`);
|
||||
const index = await loadIndex(source);
|
||||
|
||||
if (index.blocks.length === 0) {
|
||||
console.log("没有可用的积木。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Interactive prompts
|
||||
// 2a. Select blocks
|
||||
const chosenNames = await checkbox({
|
||||
message: "选择要集成的积木(空格选择,回车确认):",
|
||||
choices: index.blocks.map((b) => ({
|
||||
name: `${b.name} — ${b.description}`,
|
||||
value: b.name,
|
||||
checked: true,
|
||||
})),
|
||||
});
|
||||
|
||||
if (chosenNames.length === 0) {
|
||||
console.log("未选择任何积木,已取消。");
|
||||
return;
|
||||
}
|
||||
|
||||
const chosenBlocks = index.blocks.filter((b) =>
|
||||
chosenNames.includes(b.name)
|
||||
);
|
||||
|
||||
// 2b. Resolve dependencies (auto-add missing)
|
||||
const resolved = resolveDeps(chosenBlocks, index.blocks);
|
||||
if (resolved.added.length > 0) {
|
||||
console.log(
|
||||
` 自动补全依赖:${resolved.added.map((b) => b.name).join(", ")}`
|
||||
);
|
||||
}
|
||||
|
||||
// 2c. Select variant for blocks with multiple variants
|
||||
const selected: SelectedBlock[] = [];
|
||||
for (const block of resolved.all) {
|
||||
let branch = block.defaultBranch;
|
||||
let variantLabel = block.defaultBranch;
|
||||
|
||||
if (block.variants.length > 1) {
|
||||
const chosen = await select({
|
||||
message: `为 ${block.name} 选择变体:`,
|
||||
choices: block.variants.map((v) => ({
|
||||
name: `${v.label} (${v.branch})`,
|
||||
value: v.branch,
|
||||
})),
|
||||
default: block.defaultBranch,
|
||||
});
|
||||
branch = chosen;
|
||||
variantLabel =
|
||||
block.variants.find((v) => v.branch === chosen)?.label ?? chosen;
|
||||
}
|
||||
|
||||
selected.push({ block, branch, variantLabel });
|
||||
}
|
||||
|
||||
// 2d. Project style
|
||||
const style = await select({
|
||||
message: "选择工程风格:",
|
||||
choices: STYLES,
|
||||
default: "standard",
|
||||
});
|
||||
|
||||
// 2e. Variables
|
||||
const defaultPkg = `com.example.${projectName.replace(/[^a-zA-Z0-9]/g, "")}`;
|
||||
|
||||
const basePackage = await input({
|
||||
message: "基础包名 (basePackage):",
|
||||
default: defaultPkg,
|
||||
});
|
||||
|
||||
const groupId = await input({
|
||||
message: "Maven groupId:",
|
||||
default: basePackage,
|
||||
});
|
||||
|
||||
const javaVersion = await input({
|
||||
message: "Java 版本:",
|
||||
default: "17",
|
||||
});
|
||||
|
||||
// 2f. AI tool
|
||||
const aiTool = await select({
|
||||
message: "选择 AI 工具:",
|
||||
choices: AI_TOOLS,
|
||||
default: "claude",
|
||||
});
|
||||
|
||||
// Summary
|
||||
console.log("\n--- 配置摘要 ---");
|
||||
console.log(` 项目名: ${projectName}`);
|
||||
console.log(` 积木: ${selected.map((s) => `${s.block.name} (${s.variantLabel})`).join(", ")}`);
|
||||
console.log(` 工程风格: ${STYLES.find((s) => s.value === style)?.label}`);
|
||||
console.log(` 基础包名: ${basePackage}`);
|
||||
console.log(` groupId: ${groupId}`);
|
||||
console.log(` Java 版本: ${javaVersion}`);
|
||||
console.log(` AI 工具: ${AI_TOOLS.find((t) => t.value === aiTool)?.label}`);
|
||||
console.log("-----------------\n");
|
||||
|
||||
// 3. Clone blocks
|
||||
console.log("[2/7] 拉取积木仓库...");
|
||||
const blockDirs: { name: string; dir: string }[] = [];
|
||||
|
||||
for (const sel of selected) {
|
||||
const url = inferRepoUrl(sel.block.name, sel.block.repo);
|
||||
console.log(` clone ${url} (branch: ${sel.branch})...`);
|
||||
const dir = await cloneBlock(url, sel.branch);
|
||||
blockDirs.push({ name: sel.block.name, dir });
|
||||
}
|
||||
|
||||
// 4. Apply templates
|
||||
console.log("[3/7] 生成项目源码...");
|
||||
const targetDir = resolve(cwd, projectName);
|
||||
const vars: Record<string, string> = {
|
||||
basePackage,
|
||||
groupId,
|
||||
projectName,
|
||||
javaVersion,
|
||||
};
|
||||
|
||||
for (const bd of blockDirs) {
|
||||
await applyTemplate(bd.dir, targetDir, vars);
|
||||
}
|
||||
|
||||
// 5. Generate AI rules
|
||||
console.log("[4/7] 生成 AI 规则文件...");
|
||||
await compileAiRules(blockDirs, targetDir, aiTool);
|
||||
|
||||
// 6. Generate root pom.xml
|
||||
console.log("[5/7] 生成根 pom.xml...");
|
||||
if (style !== "simple") {
|
||||
const rootPom = buildRootPom(
|
||||
projectName,
|
||||
groupId,
|
||||
javaVersion,
|
||||
selected,
|
||||
style
|
||||
);
|
||||
await writeFile(resolve(targetDir, "pom.xml"), rootPom, "utf-8");
|
||||
}
|
||||
|
||||
// 7. Generate brick.json
|
||||
console.log("[6/7] 生成 brick.json...");
|
||||
const brickJson: BrickJson = {
|
||||
projectName,
|
||||
basePackage,
|
||||
javaVersion,
|
||||
aiTool,
|
||||
style,
|
||||
blocks: selected.map((s) => ({
|
||||
name: s.block.name,
|
||||
variant: s.variantLabel,
|
||||
version: s.block.version,
|
||||
})),
|
||||
};
|
||||
await writeFile(
|
||||
resolve(targetDir, "brick.json"),
|
||||
JSON.stringify(brickJson, null, 2),
|
||||
"utf-8"
|
||||
);
|
||||
|
||||
// 8. Cleanup temp dirs
|
||||
console.log("[7/7] 清理临时文件...");
|
||||
for (const bd of blockDirs) {
|
||||
await cleanupTemp(bd.dir);
|
||||
}
|
||||
|
||||
console.log(`\n项目已创建:${targetDir}`);
|
||||
console.log("Done!");
|
||||
}
|
||||
|
||||
function resolveDeps(
|
||||
chosen: Block[],
|
||||
all: Block[]
|
||||
): { all: Block[]; added: Block[] } {
|
||||
const blockMap = new Map(all.map((b) => [b.name, b]));
|
||||
const result = new Map(chosen.map((b) => [b.name, b]));
|
||||
const added: Block[] = [];
|
||||
|
||||
// Iteratively resolve dependencies
|
||||
let changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
for (const block of [...result.values()]) {
|
||||
for (const depName of block.dependsOn) {
|
||||
if (!result.has(depName)) {
|
||||
const dep = blockMap.get(depName);
|
||||
if (dep) {
|
||||
result.set(depName, dep);
|
||||
added.push(dep);
|
||||
changed = true;
|
||||
} else {
|
||||
process.stderr.write(
|
||||
`Warning: Dependency ${depName} not found in index\n`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { all: [...result.values()], added };
|
||||
}
|
||||
|
||||
function buildRootPom(
|
||||
projectName: string,
|
||||
groupId: string,
|
||||
javaVersion: string,
|
||||
selected: SelectedBlock[],
|
||||
style: string
|
||||
): string {
|
||||
const modules = selected
|
||||
.map((s) => ` <module>${projectName}-common-${s.block.module}</module>`)
|
||||
.join("\n");
|
||||
|
||||
if (style === "bom") {
|
||||
return [
|
||||
'<?xml version="1.0" encoding="UTF-8"?>',
|
||||
'<project xmlns="http://maven.apache.org/POM/4.0.0"',
|
||||
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',
|
||||
' xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">',
|
||||
' <modelVersion>4.0.0</modelVersion>',
|
||||
"",
|
||||
` <groupId>${groupId}</groupId>`,
|
||||
` <artifactId>${projectName}</artifactId>`,
|
||||
" <version>0.0.0</version>",
|
||||
" <packaging>pom</packaging>",
|
||||
"",
|
||||
` <name>${projectName}</name>`,
|
||||
"",
|
||||
" <properties>",
|
||||
` <java.version>${javaVersion}</java.version>`,
|
||||
` <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>`,
|
||||
` <brick.version>0.0.0</brick.version>`,
|
||||
" </properties>",
|
||||
"",
|
||||
" <dependencyManagement>",
|
||||
" <dependencies>",
|
||||
...selected.map(
|
||||
(s) =>
|
||||
` <dependency>\n` +
|
||||
` <groupId>${groupId}</groupId>\n` +
|
||||
` <artifactId>${projectName}-common-${s.block.module}</artifactId>\n` +
|
||||
` <version>${"$"}{brick.version}</version>\n` +
|
||||
` </dependency>`
|
||||
),
|
||||
" </dependencies>",
|
||||
" </dependencyManagement>",
|
||||
"",
|
||||
" <modules>",
|
||||
` <module>${projectName}-common-bom</module>`,
|
||||
modules,
|
||||
" </modules>",
|
||||
"</project>",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
// standard
|
||||
return [
|
||||
'<?xml version="1.0" encoding="UTF-8"?>',
|
||||
'<project xmlns="http://maven.apache.org/POM/4.0.0"',
|
||||
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"',
|
||||
' xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">',
|
||||
' <modelVersion>4.0.0</modelVersion>',
|
||||
"",
|
||||
` <groupId>${groupId}</groupId>`,
|
||||
` <artifactId>${projectName}</artifactId>`,
|
||||
" <version>0.0.0</version>",
|
||||
" <packaging>pom</packaging>",
|
||||
"",
|
||||
` <name>${projectName}</name>`,
|
||||
"",
|
||||
" <properties>",
|
||||
` <java.version>${javaVersion}</java.version>`,
|
||||
` <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>`,
|
||||
" </properties>",
|
||||
"",
|
||||
" <modules>",
|
||||
modules,
|
||||
" </modules>",
|
||||
"</project>",
|
||||
].join("\n");
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { Command } from "commander";
|
||||
import { registerList } from "./commands/list.js";
|
||||
import { registerCreate } from "./commands/create.js";
|
||||
|
||||
const program = new Command();
|
||||
|
||||
@@ -10,5 +11,6 @@ program
|
||||
.description("Brick CLI — 源码级 Spring Boot 脚手架");
|
||||
|
||||
registerList(program);
|
||||
registerCreate(program);
|
||||
|
||||
program.parse();
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
||||
import { join, dirname } from "node:path";
|
||||
import { load } from "js-yaml";
|
||||
import { existsSync } from "node:fs";
|
||||
import type { AiModule } from "../types/index.js";
|
||||
|
||||
const AI_FILE_MAP: Record<string, string> = {
|
||||
claude: "CLAUDE.md",
|
||||
cursor: ".cursorrules",
|
||||
copilot: ".github/copilot-instructions.md",
|
||||
aider: "CONVENTIONS.md",
|
||||
generic: "AI_CONTEXT.md",
|
||||
};
|
||||
|
||||
function parseAiModule(raw: string): AiModule {
|
||||
const data = load(raw) as Record<string, unknown>;
|
||||
return {
|
||||
module: String(data.module ?? ""),
|
||||
summary: String(data.summary ?? ""),
|
||||
dos: Array.isArray(data.dos) ? data.dos.map(String) : [],
|
||||
donts: Array.isArray(data.donts) ? data.donts.map(String) : [],
|
||||
examples: Array.isArray(data.examples)
|
||||
? data.examples.map((e: unknown) => {
|
||||
const ex = e as Record<string, unknown>;
|
||||
return { path: String(ex.path ?? ""), note: String(ex.note ?? "") };
|
||||
})
|
||||
: [],
|
||||
};
|
||||
}
|
||||
|
||||
export async function compileAiRules(
|
||||
blockDirs: { name: string; dir: string }[],
|
||||
targetDir: string,
|
||||
aiTool: string
|
||||
): Promise<void> {
|
||||
const compiled = await buildAiContent(blockDirs);
|
||||
if (!compiled) return;
|
||||
|
||||
const fileName = AI_FILE_MAP[aiTool] ?? AI_FILE_MAP.generic;
|
||||
const filePath = join(targetDir, fileName);
|
||||
|
||||
const dir = dirname(filePath);
|
||||
if (!existsSync(dir)) {
|
||||
await mkdir(dir, { recursive: true });
|
||||
}
|
||||
|
||||
await writeFile(filePath, compiled, "utf-8");
|
||||
}
|
||||
|
||||
async function buildAiContent(
|
||||
blockDirs: { name: string; dir: string }[]
|
||||
): Promise<string | null> {
|
||||
const sections: string[] = [];
|
||||
|
||||
sections.push("# 项目 AI 编码规范\n");
|
||||
sections.push(
|
||||
"本文件由 Brick CLI 自动生成,整合了以下积木的 AI 规范。\n"
|
||||
);
|
||||
|
||||
const moduleList = blockDirs.map((b) => `- ${b.name}`).join("\n");
|
||||
sections.push(`## 项目模块\n\n${moduleList}\n`);
|
||||
|
||||
const allDos: string[] = [];
|
||||
const allDonts: string[] = [];
|
||||
|
||||
for (const block of blockDirs) {
|
||||
const moduleYaml = join(block.dir, "ai", "module.yaml");
|
||||
const conventionsMd = join(block.dir, "ai", "conventions.md");
|
||||
|
||||
if (!existsSync(moduleYaml)) continue;
|
||||
|
||||
try {
|
||||
const raw = await readFile(moduleYaml, "utf-8");
|
||||
const mod = parseAiModule(raw);
|
||||
|
||||
sections.push(`### ${mod.module} — ${mod.summary}\n`);
|
||||
|
||||
if (mod.dos.length > 0) {
|
||||
sections.push("**DO:**\n");
|
||||
for (const d of mod.dos) {
|
||||
sections.push(`- ${d}`);
|
||||
allDos.push(`[${mod.module}] ${d}`);
|
||||
}
|
||||
sections.push("");
|
||||
}
|
||||
|
||||
if (mod.donts.length > 0) {
|
||||
sections.push("**DON'T:**\n");
|
||||
for (const d of mod.donts) {
|
||||
sections.push(`- ${d}`);
|
||||
allDonts.push(`[${mod.module}] ${d}`);
|
||||
}
|
||||
sections.push("");
|
||||
}
|
||||
} catch {
|
||||
// module.yaml is optional
|
||||
}
|
||||
|
||||
// Append conventions.md content if it exists
|
||||
if (existsSync(conventionsMd)) {
|
||||
try {
|
||||
const raw = await readFile(conventionsMd, "utf-8");
|
||||
sections.push(raw);
|
||||
sections.push("");
|
||||
} catch {
|
||||
// conventions.md is optional
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Global summary
|
||||
if (allDos.length > 0 || allDonts.length > 0) {
|
||||
sections.push("## 全局约束 (摘要)\n");
|
||||
if (allDos.length > 0) {
|
||||
sections.push("**必须遵守:**\n");
|
||||
for (const d of allDos) {
|
||||
sections.push(`- ${d}`);
|
||||
}
|
||||
sections.push("");
|
||||
}
|
||||
if (allDonts.length > 0) {
|
||||
sections.push("**严格禁止:**\n");
|
||||
for (const d of allDonts) {
|
||||
sections.push(`- ${d}`);
|
||||
}
|
||||
sections.push("");
|
||||
}
|
||||
}
|
||||
|
||||
return sections.join("\n");
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { exec } from "node:child_process";
|
||||
import { rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
const GITEA_BASE = "https://gitea.synoth.com/synoth";
|
||||
|
||||
export function inferRepoUrl(blockName: string, repo?: string): string {
|
||||
if (repo) return repo;
|
||||
return `${GITEA_BASE}/${blockName}.git`;
|
||||
}
|
||||
|
||||
function execAsync(command: string): Promise<{ stdout: string; stderr: string }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(command, (error, stdout, stderr) => {
|
||||
if (error) reject(error);
|
||||
else resolve({ stdout, stderr });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function cloneBlock(
|
||||
repoUrl: string,
|
||||
branch: string
|
||||
): Promise<string> {
|
||||
const dir = join(tmpdir(), `brick-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`);
|
||||
|
||||
try {
|
||||
await execAsync(
|
||||
`git clone --depth 1 --branch "${branch}" "${repoUrl}" "${dir}"`
|
||||
);
|
||||
} catch (err: unknown) {
|
||||
const msg = (err as Error).message;
|
||||
if (msg.includes("not found") || msg.includes("does not exist")) {
|
||||
process.stderr.write(
|
||||
`Error: Repository or branch not found: ${repoUrl} (branch: ${branch})\n`
|
||||
);
|
||||
} else {
|
||||
process.stderr.write(
|
||||
`Error: Failed to clone ${repoUrl}: ${msg}\n`
|
||||
);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
export async function cleanupTemp(dir: string): Promise<void> {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import { cp, readFile, writeFile, rename, readdir, mkdir } from "node:fs/promises";
|
||||
import { join, dirname } from "node:path";
|
||||
import { existsSync } from "node:fs";
|
||||
|
||||
export async function applyTemplate(
|
||||
sourceDir: string,
|
||||
targetDir: string,
|
||||
vars: Record<string, string>
|
||||
): Promise<void> {
|
||||
const templateDir = join(sourceDir, "template");
|
||||
|
||||
if (!existsSync(templateDir)) {
|
||||
process.stderr.write(`Error: template/ not found in ${sourceDir}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const varsToReplace: Record<string, string> = {
|
||||
...vars,
|
||||
basePkgPath: vars.basePackage.replace(/\./g, "/"),
|
||||
};
|
||||
|
||||
// Step 1: copy template/ to target
|
||||
await cp(templateDir, targetDir, { recursive: true });
|
||||
|
||||
// Step 2: replace variables in all file contents
|
||||
await replaceInFiles(targetDir, varsToReplace);
|
||||
|
||||
// Step 3: rename directories containing {{basePkgPath}}
|
||||
await renamePlaceholders(targetDir, varsToReplace);
|
||||
}
|
||||
|
||||
async function replaceInFiles(
|
||||
dir: string,
|
||||
vars: Record<string, string>
|
||||
): Promise<void> {
|
||||
const entries = await readdir(dir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
const fullPath = join(dir, entry.name);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
// Skip .git
|
||||
if (entry.name === ".git") continue;
|
||||
await replaceInFiles(fullPath, vars);
|
||||
} else if (entry.isFile()) {
|
||||
let content = await readFile(fullPath, "utf-8");
|
||||
let changed = false;
|
||||
|
||||
for (const [key, value] of Object.entries(vars)) {
|
||||
const placeholder = `{{${key}}}`;
|
||||
if (content.includes(placeholder)) {
|
||||
content = content.replaceAll(placeholder, value);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
await writeFile(fullPath, content, "utf-8");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function renamePlaceholders(
|
||||
dir: string,
|
||||
vars: Record<string, string>
|
||||
): Promise<void> {
|
||||
// Walk directory tree bottom-up to rename {{basePkgPath}} segments
|
||||
const dirsToRename = await findPlaceholderDirs(dir);
|
||||
|
||||
// Sort by depth descending (deepest first)
|
||||
dirsToRename.sort((a, b) => b.depth - a.depth);
|
||||
|
||||
for (const { path } of dirsToRename) {
|
||||
const parent = dirname(path);
|
||||
const oldName = path.split(/[/\\]/).pop()!;
|
||||
let newName = oldName;
|
||||
|
||||
for (const [key, value] of Object.entries(vars)) {
|
||||
newName = newName.replaceAll(`{{${key}}}`, value);
|
||||
}
|
||||
|
||||
if (newName !== oldName) {
|
||||
const dest = join(parent, newName);
|
||||
await mkdir(dirname(dest), { recursive: true });
|
||||
await rename(path, dest);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function findPlaceholderDirs(
|
||||
dir: string
|
||||
): Promise<{ path: string; depth: number }[]> {
|
||||
const results: { path: string; depth: number }[] = [];
|
||||
const entries = await readdir(dir, { withFileTypes: true });
|
||||
|
||||
for (const entry of entries) {
|
||||
if (!entry.isDirectory() || entry.name === ".git") continue;
|
||||
|
||||
const fullPath = join(dir, entry.name);
|
||||
const sub = await findPlaceholderDirs(fullPath);
|
||||
results.push(...sub);
|
||||
|
||||
if (entry.name.includes("{{")) {
|
||||
const depth = fullPath.replace(/\\/g, "/").split("/").length;
|
||||
results.push({ path: fullPath, depth });
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ export interface Block {
|
||||
language: string;
|
||||
defaultBranch: string;
|
||||
module: string;
|
||||
repo?: string;
|
||||
variants: BlockVariant[];
|
||||
dependsOn: string[];
|
||||
}
|
||||
@@ -17,3 +18,24 @@ export interface Block {
|
||||
export interface BlockIndex {
|
||||
blocks: Block[];
|
||||
}
|
||||
|
||||
export interface BrickJson {
|
||||
projectName: string;
|
||||
basePackage: string;
|
||||
javaVersion: string;
|
||||
aiTool: string;
|
||||
style: string;
|
||||
blocks: {
|
||||
name: string;
|
||||
variant: string;
|
||||
version: string;
|
||||
}[];
|
||||
}
|
||||
|
||||
export interface AiModule {
|
||||
module: string;
|
||||
summary: string;
|
||||
dos: string[];
|
||||
donts: string[];
|
||||
examples: { path: string; note: string }[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user