|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import readline from "node:readline"; |
| 6 | +import { spawnSync } from "node:child_process"; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | +const templateDir = path.resolve(__dirname, "..", "template"); |
| 11 | + |
| 12 | +function ask(rl, question, fallback = "") { |
| 13 | + return new Promise((resolve) => { |
| 14 | + rl.question( |
| 15 | + fallback ? `${question} (${fallback}): ` : `${question}: `, |
| 16 | + (answer) => resolve(answer.trim() || fallback), |
| 17 | + ); |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +function copyDir(src, dest) { |
| 22 | + fs.mkdirSync(dest, { recursive: true }); |
| 23 | + for (const entry of fs.readdirSync(src, { withFileTypes: true })) { |
| 24 | + const srcPath = path.join(src, entry.name); |
| 25 | + const destPath = path.join(dest, entry.name); |
| 26 | + if (entry.isDirectory()) { |
| 27 | + copyDir(srcPath, destPath); |
| 28 | + } else { |
| 29 | + fs.copyFileSync(srcPath, destPath); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function replaceInFile(filePath, replacements) { |
| 35 | + let content = fs.readFileSync(filePath, "utf8"); |
| 36 | + for (const [from, to] of Object.entries(replacements)) { |
| 37 | + content = content.replaceAll(from, to); |
| 38 | + } |
| 39 | + fs.writeFileSync(filePath, content); |
| 40 | +} |
| 41 | + |
| 42 | +function getScripts(locale, apiStyle) { |
| 43 | + const base = { |
| 44 | + "method:stations": `node ./node_modules/apiops-cycles-method-data/.agents/skills/new-api-guide/scripts/get-core-stations.cjs ${locale}`, |
| 45 | + "method:resource:audit": `node ./node_modules/apiops-cycles-method-data/.agents/skills/new-api-guide/scripts/get-resource-metadata.cjs api-audit-checklist ${locale}` |
| 46 | + }; |
| 47 | + |
| 48 | + if (apiStyle === "REST" || apiStyle === "Not sure yet") { |
| 49 | + base["method:canvas:rest"] = |
| 50 | + `node ./node_modules/apiops-cycles-method-data/.agents/skills/new-api-guide/scripts/get-canvas-metadata.cjs restCanvas ${locale}`; |
| 51 | + } |
| 52 | + if (apiStyle === "Event" || apiStyle === "Not sure yet") { |
| 53 | + base["method:canvas:event"] = |
| 54 | + `node ./node_modules/apiops-cycles-method-data/.agents/skills/new-api-guide/scripts/get-canvas-metadata.cjs eventCanvas ${locale}`; |
| 55 | + } |
| 56 | + if (apiStyle === "GraphQL" || apiStyle === "Not sure yet") { |
| 57 | + base["method:canvas:graphql"] = |
| 58 | + `node ./node_modules/apiops-cycles-method-data/.agents/skills/new-api-guide/scripts/get-canvas-metadata.cjs graphqlCanvas ${locale}`; |
| 59 | + } |
| 60 | + |
| 61 | + return base; |
| 62 | +} |
| 63 | + |
| 64 | +function createStarterCanvas(targetDir, locale = "en", canvasId = "domainCanvas") { |
| 65 | + const canvasDataPath = path.join( |
| 66 | + targetDir, |
| 67 | + "node_modules", |
| 68 | + "apiops-cycles-method-data", |
| 69 | + "canvasData.json" |
| 70 | + ); |
| 71 | + |
| 72 | + if (!fs.existsSync(canvasDataPath)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const canvasData = JSON.parse(fs.readFileSync(canvasDataPath, "utf8")); |
| 77 | + const canvas = canvasData[canvasId]; |
| 78 | + |
| 79 | + if (!canvas) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const starter = { |
| 84 | + templateId: canvasId, |
| 85 | + locale, |
| 86 | + metadata: { |
| 87 | + source: "APIOps Cycles method", |
| 88 | + license: "CC-BY-SA 4.0", |
| 89 | + authors: ["Project team"], |
| 90 | + website: "www.apiopscycles.com", |
| 91 | + date: new Date().toISOString() |
| 92 | + }, |
| 93 | + sections: canvas.sections.map((section) => ({ |
| 94 | + sectionId: section.id, |
| 95 | + stickyNotes: [ |
| 96 | + { |
| 97 | + content: "Placeholder", |
| 98 | + size: 80, |
| 99 | + color: "#FFF399" |
| 100 | + } |
| 101 | + ] |
| 102 | + })) |
| 103 | + }; |
| 104 | + |
| 105 | + const outPath = path.join(targetDir, "specs", "canvases", "example.json"); |
| 106 | + fs.mkdirSync(path.dirname(outPath), { recursive: true }); |
| 107 | + fs.writeFileSync(outPath, `${JSON.stringify(starter, null, 2)}\n`); |
| 108 | +} |
| 109 | + |
| 110 | +async function main() { |
| 111 | + const rl = readline.createInterface({ |
| 112 | + input: process.stdin, |
| 113 | + output: process.stdout |
| 114 | + }); |
| 115 | + |
| 116 | + const projectName = await ask(rl, "Project name", "my-api-project"); |
| 117 | + const locale = await ask(rl, "Default locale", "en"); |
| 118 | + const apiStyle = await ask(rl, "API style focus [REST/Event/GraphQL/Not sure yet]", "REST"); |
| 119 | + const installNow = await ask(rl, "Install dependencies now? [yes/no]", "yes"); |
| 120 | + rl.close(); |
| 121 | + |
| 122 | + const targetDir = path.resolve(process.cwd(), projectName); |
| 123 | + if (fs.existsSync(targetDir)) { |
| 124 | + console.error(`Target already exists: ${targetDir}`); |
| 125 | + process.exit(1); |
| 126 | + } |
| 127 | + |
| 128 | + copyDir(templateDir, targetDir); |
| 129 | + |
| 130 | + const replacements = { |
| 131 | + "__PROJECT_NAME__": projectName, |
| 132 | + "__LOCALE__": locale, |
| 133 | + "__API_TITLE__": projectName.replace(/[-_]/g, " ") |
| 134 | + }; |
| 135 | + |
| 136 | + replaceInFile(path.join(targetDir, "README.md"), replacements); |
| 137 | + replaceInFile(path.join(targetDir, "specs", "openapi", "api.yaml"), replacements); |
| 138 | + |
| 139 | + const packageJsonPath = path.join(targetDir, "package.json"); |
| 140 | + const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")); |
| 141 | + pkg.name = projectName; |
| 142 | + pkg.scripts = { |
| 143 | + ...pkg.scripts, |
| 144 | + ...getScripts(locale, apiStyle) |
| 145 | + }; |
| 146 | + fs.writeFileSync(packageJsonPath, `${JSON.stringify(pkg, null, 2)}\n`); |
| 147 | + |
| 148 | + console.log(`Created ${projectName}`); |
| 149 | + |
| 150 | + if (installNow.toLowerCase() === "yes") { |
| 151 | + const result = spawnSync("npm", ["install"], { |
| 152 | + cwd: targetDir, |
| 153 | + stdio: "inherit", |
| 154 | + shell: true |
| 155 | + }); |
| 156 | + if (result.status !== 0) { |
| 157 | + console.error("npm install failed"); |
| 158 | + process.exit(result.status || 1); |
| 159 | + } |
| 160 | + |
| 161 | + createStarterCanvas(targetDir, locale, "domainCanvas"); |
| 162 | + } else { |
| 163 | + console.log("\nStarter canvas was not generated yet."); |
| 164 | + console.log("Run `npm install` first, then regenerate it with the scaffolder or a helper command."); |
| 165 | + } |
| 166 | + |
| 167 | + console.log("\nNext steps:"); |
| 168 | + console.log(` cd ${projectName}`); |
| 169 | + console.log(" npm run method:stations"); |
| 170 | +} |
| 171 | + |
| 172 | +main().catch((err) => { |
| 173 | + console.error(err); |
| 174 | + process.exit(1); |
| 175 | +}); |
0 commit comments