165 lines
6.1 KiB
TypeScript
165 lines
6.1 KiB
TypeScript
import { fileURLToPath } from "node:url";
|
|
import path from "node:path";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { KRI_REGISTRY_ITEMS, REGISTRY_BASE_URL } from "./constants.js";
|
|
import { copyTemplate, ensureTargetDirectory, removeJenkinsfile, replacePlaceholders } from "./files.js";
|
|
import { composeAgentHarness } from "./harness.js";
|
|
import { run } from "./process.js";
|
|
import type { CliOptions } from "./types.js";
|
|
|
|
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
const templateDir = path.resolve(currentDir, "../templates/base");
|
|
|
|
// 설치 시점에 각 spec을 해석해 npm이 실제 버전 범위(예: ^19.2.1)로 package.json에 기록한다.
|
|
// `@kri/common`은 별도 단계에서 exact 버전으로, `cn`은 `@kri/common`과 같은 exact 버전으로 설치한다.
|
|
const DEPENDENCY_SPECS: Record<string, string> = {
|
|
"@tanstack/react-query": "latest",
|
|
"class-variance-authority": "latest",
|
|
jotai: "latest",
|
|
"lucide-react": "latest",
|
|
next: "latest",
|
|
react: "latest",
|
|
"react-dom": "latest",
|
|
"server-only": "latest",
|
|
sonner: "latest",
|
|
};
|
|
|
|
const DEV_DEPENDENCY_SPECS: Record<string, string> = {
|
|
"@tailwindcss/postcss": "latest",
|
|
// 배포 서버 Node 메이저(24)에 맞춘다. 서버에 없는 API 사용을 타입 검사에서 잡기 위함.
|
|
"@types/node": "24",
|
|
"@types/react": "latest",
|
|
"@types/react-dom": "latest",
|
|
"babel-plugin-react-compiler": "latest",
|
|
// `^`는 Windows cmd.exe 이스케이프 문자라 메이저 버전만 지정한다(9 = 최신 9.x).
|
|
eslint: "9",
|
|
"eslint-config-next": "latest",
|
|
tailwindcss: "latest",
|
|
"tw-animate-css": "latest",
|
|
typescript: "5",
|
|
};
|
|
|
|
const toInstallArgs = (specs: Record<string, string>): string[] =>
|
|
Object.entries(specs).map(([name, spec]) => `${name}@${spec}`);
|
|
|
|
function packageJson(options: CliOptions) {
|
|
const base = {
|
|
name: options.projectName,
|
|
version: "0.1.0",
|
|
private: true,
|
|
scripts: {
|
|
dev: `next dev -p ${options.port}`,
|
|
build: "next build",
|
|
start: `next start -p ${options.port}`,
|
|
lint: "eslint",
|
|
typecheck: "tsc --noEmit",
|
|
},
|
|
};
|
|
|
|
if (options.install) return base;
|
|
|
|
// --skip-install: 설치하지 않으므로 나중에 `npm install`할 수 있도록 spec을 그대로 기록한다.
|
|
return {
|
|
...base,
|
|
dependencies: { "@kri/common": "latest", ...DEPENDENCY_SPECS },
|
|
devDependencies: { ...DEV_DEPENDENCY_SPECS },
|
|
};
|
|
}
|
|
|
|
async function readKriCommonCnVersion(targetDir: string): Promise<string | null> {
|
|
const manifestPath = path.join(targetDir, "node_modules", "@kri", "common", "package.json");
|
|
const manifest: unknown = JSON.parse(await readFile(manifestPath, "utf8"));
|
|
if (typeof manifest !== "object" || manifest === null) return null;
|
|
|
|
const dependencies = (manifest as { dependencies?: unknown }).dependencies;
|
|
if (typeof dependencies !== "object" || dependencies === null) return null;
|
|
|
|
const cnVersion = (dependencies as Record<string, unknown>).cn;
|
|
return typeof cnVersion === "string" && cnVersion.trim() !== "" ? cnVersion.trim() : null;
|
|
}
|
|
|
|
// shadcn 기본 컴포넌트는 `cn` 패키지를 직접 import한다. `@kri/common`과 같은 버전으로 맞춰 중복 설치를 막는다.
|
|
async function syncCnWithKriCommon(targetDir: string): Promise<void> {
|
|
const cnVersion = await readKriCommonCnVersion(targetDir);
|
|
if (!cnVersion) {
|
|
console.warn("[WARN] @kri/common이 cn에 의존하지 않아 cn 버전 동기화를 생략합니다.");
|
|
return;
|
|
}
|
|
|
|
await run("npm", ["install", "--save-exact", `cn@${cnVersion}`], targetDir);
|
|
}
|
|
|
|
export async function createProject(options: CliOptions): Promise<void> {
|
|
await ensureTargetDirectory(options.targetDir, options.force);
|
|
await copyTemplate(templateDir, options.targetDir);
|
|
|
|
if (!options.jenkins) await removeJenkinsfile(options.targetDir);
|
|
|
|
await replacePlaceholders(options.targetDir, {
|
|
"__PROJECT_NAME__": options.projectName,
|
|
"__AGENT_PROJECT_ID__": options.agentProjectId,
|
|
"__AGENT_PROJECT_NAME_YAML__": JSON.stringify(options.projectName),
|
|
"__AGENT_PROJECT_TYPE__": options.projectType,
|
|
"__DISPLAY_NAME__": options.displayName,
|
|
"__DESCRIPTION__": options.description,
|
|
"__PORT__": String(options.port),
|
|
"__REPO_URL__": options.repoUrl,
|
|
"__APP_ROOT__": options.appRoot,
|
|
"__SERVICE_NAME__": options.serviceName,
|
|
"__BRANCH__": options.branch,
|
|
"__AGENT_LABEL__": options.agentLabel,
|
|
});
|
|
|
|
await writeFile(
|
|
path.join(options.targetDir, "package.json"),
|
|
`${JSON.stringify(packageJson(options), null, 2)}
|
|
`,
|
|
"utf8",
|
|
);
|
|
|
|
if (!options.install) {
|
|
console.log("[SKIP] npm install");
|
|
await composeAgentHarness(options);
|
|
return;
|
|
}
|
|
|
|
console.log("\n[1/4] npm 의존성 설치");
|
|
await run("npm", ["install", ...toInstallArgs(DEPENDENCY_SPECS)], options.targetDir);
|
|
await run("npm", ["install", "--save-dev", ...toInstallArgs(DEV_DEPENDENCY_SPECS)], options.targetDir);
|
|
|
|
console.log("\n[2/4] 최신 @kri/common 설치");
|
|
await run("npm", ["install", "--save-exact", "@kri/common@latest"], options.targetDir);
|
|
|
|
if (options.installUi) {
|
|
console.log("\n[3/4] shadcn 및 KRI UI Registry 설치");
|
|
await run("npx", ["--yes", "shadcn@latest", "add", "card", "--yes", "--overwrite"], options.targetDir);
|
|
for (const item of KRI_REGISTRY_ITEMS) {
|
|
await run(
|
|
"npx",
|
|
["--yes", "shadcn@latest", "add", `${REGISTRY_BASE_URL}/${item}.json`, "--yes", "--overwrite"],
|
|
options.targetDir,
|
|
);
|
|
}
|
|
} else {
|
|
console.log("[SKIP] UI Registry 설치");
|
|
}
|
|
|
|
// shadcn 설치가 cn 의존성을 추가·변경할 수 있으므로 UI 설치 뒤에 맞춘다.
|
|
console.log("\ncn 버전을 @kri/common과 동기화");
|
|
await syncCnWithKriCommon(options.targetDir);
|
|
|
|
if (options.build) {
|
|
if (!options.installUi) {
|
|
console.warn("[WARN] --skip-ui 때문에 typecheck/build를 생략합니다.");
|
|
} else {
|
|
console.log("\n[4/4] TypeScript 및 production build 검증");
|
|
await run("npm", ["run", "typecheck"], options.targetDir);
|
|
await run("npm", ["run", "build"], options.targetDir);
|
|
}
|
|
} else {
|
|
console.log("[SKIP] typecheck/build");
|
|
}
|
|
|
|
await composeAgentHarness(options);
|
|
}
|