|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * CLI tool to setup CLAUDE.md for Databricks AppKit packages. |
| 5 | + * |
| 6 | + * This bin is included in both @databricks/app-kit and @databricks/app-kit-ui |
| 7 | + * so it's available regardless of which package the user installs. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * npx appkit-setup # Show detected packages and content |
| 11 | + * npx appkit-setup --write # Create or update CLAUDE.md file |
| 12 | + */ |
| 13 | + |
| 14 | +import fs from "node:fs"; |
| 15 | +import path from "node:path"; |
| 16 | + |
| 17 | +const PACKAGES = [ |
| 18 | + { name: "@databricks/app-kit", description: "Backend SDK" }, |
| 19 | + { |
| 20 | + name: "@databricks/app-kit-ui", |
| 21 | + description: "UI Integration, Charts, Tables, SSE, and more.", |
| 22 | + }, |
| 23 | +]; |
| 24 | + |
| 25 | +const SECTION_START = "<!-- appkit-instructions-start -->"; |
| 26 | +const SECTION_END = "<!-- appkit-instructions-end -->"; |
| 27 | + |
| 28 | +/** |
| 29 | + * Find which AppKit packages are installed by checking for CLAUDE.md |
| 30 | + */ |
| 31 | +function findInstalledPackages() { |
| 32 | + const cwd = process.cwd(); |
| 33 | + const installed = []; |
| 34 | + |
| 35 | + for (const pkg of PACKAGES) { |
| 36 | + const claudePath = path.join(cwd, "node_modules", pkg.name, "CLAUDE.md"); |
| 37 | + if (fs.existsSync(claudePath)) { |
| 38 | + installed.push(pkg); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return installed; |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Generate the AppKit section content |
| 47 | + */ |
| 48 | +function generateSection(packages) { |
| 49 | + const links = packages |
| 50 | + .map((pkg) => { |
| 51 | + const docPath = `./node_modules/${pkg.name}/CLAUDE.md`; |
| 52 | + return `- **${pkg.name}** (${pkg.description}): [${docPath}](${docPath})`; |
| 53 | + }) |
| 54 | + .join("\n"); |
| 55 | + |
| 56 | + return `${SECTION_START} |
| 57 | +## Databricks AppKit |
| 58 | +
|
| 59 | +This project uses Databricks AppKit packages. For AI assistant guidance on using these packages, refer to: |
| 60 | +
|
| 61 | +${links} |
| 62 | +${SECTION_END}`; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Generate standalone CLAUDE.md content (when no existing file) |
| 67 | + */ |
| 68 | +function generateStandalone(packages) { |
| 69 | + const links = packages |
| 70 | + .map((pkg) => { |
| 71 | + const docPath = `./node_modules/${pkg.name}/CLAUDE.md`; |
| 72 | + return `- **${pkg.name}** (${pkg.description}): [${docPath}](${docPath})`; |
| 73 | + }) |
| 74 | + .join("\n"); |
| 75 | + |
| 76 | + return `# AI Assistant Instructions |
| 77 | +
|
| 78 | +${SECTION_START} |
| 79 | +## Databricks AppKit |
| 80 | +
|
| 81 | +This project uses Databricks AppKit packages. For AI assistant guidance on using these packages, refer to: |
| 82 | +
|
| 83 | +${links} |
| 84 | +${SECTION_END} |
| 85 | +`; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Update existing content with AppKit section |
| 90 | + */ |
| 91 | +function updateContent(existingContent, packages) { |
| 92 | + const newSection = generateSection(packages); |
| 93 | + |
| 94 | + // Check if AppKit section already exists |
| 95 | + const startIndex = existingContent.indexOf(SECTION_START); |
| 96 | + const endIndex = existingContent.indexOf(SECTION_END); |
| 97 | + |
| 98 | + if (startIndex !== -1 && endIndex !== -1) { |
| 99 | + // Replace existing section |
| 100 | + const before = existingContent.substring(0, startIndex); |
| 101 | + const after = existingContent.substring(endIndex + SECTION_END.length); |
| 102 | + return before + newSection + after; |
| 103 | + } |
| 104 | + |
| 105 | + // Append section to end |
| 106 | + return `${existingContent.trimEnd()}\n\n${newSection}\n`; |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Main CLI logic |
| 111 | + */ |
| 112 | +function main() { |
| 113 | + const args = process.argv.slice(2); |
| 114 | + const shouldWrite = args.includes("--write") || args.includes("-w"); |
| 115 | + const help = args.includes("--help") || args.includes("-h"); |
| 116 | + |
| 117 | + if (help) { |
| 118 | + console.log(` |
| 119 | +Usage: npx appkit-setup [options] |
| 120 | +
|
| 121 | +Options: |
| 122 | + --write, -w Create or update CLAUDE.md file in current directory |
| 123 | + --help, -h Show this help message |
| 124 | +
|
| 125 | +Examples: |
| 126 | + npx appkit-setup # Show detected packages and preview content |
| 127 | + npx appkit-setup --write # Create or update CLAUDE.md |
| 128 | +`); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + // Find installed packages |
| 133 | + const installed = findInstalledPackages(); |
| 134 | + |
| 135 | + if (installed.length === 0) { |
| 136 | + console.log("No @databricks/app-kit packages found in node_modules."); |
| 137 | + console.log("\nMake sure you've installed at least one of:"); |
| 138 | + PACKAGES.forEach((pkg) => { |
| 139 | + console.log(` - ${pkg.name}`); |
| 140 | + }); |
| 141 | + process.exit(1); |
| 142 | + } |
| 143 | + |
| 144 | + console.log("Detected packages:"); |
| 145 | + installed.forEach((pkg) => { |
| 146 | + console.log(` ✓ ${pkg.name}`); |
| 147 | + }); |
| 148 | + |
| 149 | + const claudePath = path.join(process.cwd(), "CLAUDE.md"); |
| 150 | + const existingContent = fs.existsSync(claudePath) |
| 151 | + ? fs.readFileSync(claudePath, "utf-8") |
| 152 | + : null; |
| 153 | + |
| 154 | + let finalContent; |
| 155 | + let action; |
| 156 | + |
| 157 | + if (existingContent) { |
| 158 | + finalContent = updateContent(existingContent, installed); |
| 159 | + action = existingContent.includes(SECTION_START) ? "Updated" : "Added to"; |
| 160 | + } else { |
| 161 | + finalContent = generateStandalone(installed); |
| 162 | + action = "Created"; |
| 163 | + } |
| 164 | + |
| 165 | + if (shouldWrite) { |
| 166 | + fs.writeFileSync(claudePath, finalContent); |
| 167 | + console.log(`\n✓ ${action} CLAUDE.md`); |
| 168 | + console.log(` Path: ${claudePath}`); |
| 169 | + } else { |
| 170 | + console.log("\nTo create/update CLAUDE.md, run:"); |
| 171 | + console.log(" npx appkit-setup --write\n"); |
| 172 | + |
| 173 | + if (existingContent) { |
| 174 | + console.log( |
| 175 | + `This will ${ |
| 176 | + existingContent.includes(SECTION_START) |
| 177 | + ? "update the existing" |
| 178 | + : "add a new" |
| 179 | + } AppKit section.\n`, |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + console.log("Preview of AppKit section:\n"); |
| 184 | + console.log("─".repeat(50)); |
| 185 | + console.log(generateSection(installed)); |
| 186 | + console.log("─".repeat(50)); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +main(); |
0 commit comments