|
1 |
+ |
#!/usr/bin/env bun |
|
2 |
+ |
|
|
3 |
+ |
import { parseArgs } from "util"; |
|
4 |
+ |
import { mkdir } from "node:fs/promises"; |
|
5 |
+ |
import { existsSync } from "node:fs"; |
|
6 |
+ |
import { join } from "node:path"; |
|
7 |
+ |
|
|
8 |
+ |
import { fileURLToPath } from "url"; |
|
9 |
+ |
import { dirname } from "path"; |
|
10 |
+ |
|
|
11 |
+ |
const __filename = fileURLToPath(import.meta.url); |
|
12 |
+ |
const __dirname = dirname(__filename); |
|
13 |
+ |
const COMPONENTS_DIR = join(__dirname, "components"); |
|
14 |
+ |
|
|
15 |
+ |
async function init() { |
|
16 |
+ |
console.log("🧙 Initializing norns project..."); |
|
17 |
+ |
|
|
18 |
+ |
const componentsDir = "components"; |
|
19 |
+ |
|
|
20 |
+ |
if (!existsSync(componentsDir)) { |
|
21 |
+ |
await mkdir(componentsDir, { recursive: true }); |
|
22 |
+ |
console.log(`✅ Created ${componentsDir} directory`); |
|
23 |
+ |
} else { |
|
24 |
+ |
console.log(`📁 ${componentsDir} directory already exists`); |
|
25 |
+ |
} |
|
26 |
+ |
|
|
27 |
+ |
console.log("🎉 norns project initialized! You can now add components with:"); |
|
28 |
+ |
console.log(" npx norns@latest add <component-name>"); |
|
29 |
+ |
} |
|
30 |
+ |
|
|
31 |
+ |
async function addComponent(componentName: string | undefined) { |
|
32 |
+ |
if (!componentName) { |
|
33 |
+ |
console.error("❌ Please specify a component name"); |
|
34 |
+ |
console.log("Usage: npx norns@latest add <component-name>"); |
|
35 |
+ |
process.exit(1); |
|
36 |
+ |
} |
|
37 |
+ |
|
|
38 |
+ |
console.log(`🔄 Adding component: ${componentName}`); |
|
39 |
+ |
|
|
40 |
+ |
const componentsDir = "components"; |
|
41 |
+ |
if (!existsSync(componentsDir)) { |
|
42 |
+ |
console.log("📁 Components directory doesn't exist. Creating it..."); |
|
43 |
+ |
await mkdir(componentsDir, { recursive: true }); |
|
44 |
+ |
} |
|
45 |
+ |
|
|
46 |
+ |
try { |
|
47 |
+ |
const sourceComponentPath = join(COMPONENTS_DIR, `${componentName}.js`); |
|
48 |
+ |
|
|
49 |
+ |
if (!existsSync(sourceComponentPath)) { |
|
50 |
+ |
console.error(`❌ Component '${componentName}' not found`); |
|
51 |
+ |
console.log("Available components:"); |
|
52 |
+ |
console.log(" - connect-wallet"); |
|
53 |
+ |
process.exit(1); |
|
54 |
+ |
} |
|
55 |
+ |
|
|
56 |
+ |
const componentCode = await Bun.file(sourceComponentPath).text(); |
|
57 |
+ |
const componentPath = join(componentsDir, `${componentName}.js`); |
|
58 |
+ |
|
|
59 |
+ |
await Bun.write(componentPath, componentCode); |
|
60 |
+ |
|
|
61 |
+ |
console.log(`✅ Added ${componentName} to ${componentPath}`); |
|
62 |
+ |
console.log(`📝 You can now use it in your HTML:`); |
|
63 |
+ |
console.log(` <script src="./components/${componentName}.js"></script>`); |
|
64 |
+ |
console.log(` <${componentName}></${componentName}>`); |
|
65 |
+ |
} catch (error) { |
|
66 |
+ |
console.error(`❌ Failed to add component: ${error}`); |
|
67 |
+ |
process.exit(1); |
|
68 |
+ |
} |
|
69 |
+ |
} |
|
70 |
+ |
|
|
71 |
+ |
function showHelp() { |
|
72 |
+ |
console.log(` |
|
73 |
+ |
🧙 norns - Web Component Library CLI |
|
74 |
+ |
|
|
75 |
+ |
Usage: |
|
76 |
+ |
npx norns@latest init Initialize a new norns project |
|
77 |
+ |
npx norns@latest add <component-name> Add a component to your project |
|
78 |
+ |
npx norns@latest --help Show this help message |
|
79 |
+ |
|
|
80 |
+ |
Examples: |
|
81 |
+ |
npx norns@latest init |
|
82 |
+ |
npx norns@latest add connect-wallet |
|
83 |
+ |
|
|
84 |
+ |
Available Components: |
|
85 |
+ |
- connect-wallet A Web3 wallet connection component |
|
86 |
+ |
`); |
|
87 |
+ |
} |
|
88 |
+ |
|
|
89 |
+ |
// Parse command line arguments using Bun's parseArgs |
|
90 |
+ |
// Bun.argv includes [bun_path, script_path, ...actual_args] |
|
91 |
+ |
// We need to slice from index 2 to get the actual command arguments |
|
92 |
+ |
const { values, positionals } = parseArgs({ |
|
93 |
+ |
args: Bun.argv.slice(2), |
|
94 |
+ |
options: { |
|
95 |
+ |
help: { type: "boolean", short: "h" }, |
|
96 |
+ |
}, |
|
97 |
+ |
strict: false, |
|
98 |
+ |
allowPositionals: true, |
|
99 |
+ |
}); |
|
100 |
+ |
|
|
101 |
+ |
const command = positionals[0]; |
|
102 |
+ |
const componentName = positionals[1]; |
|
103 |
+ |
|
|
104 |
+ |
if (values.help) { |
|
105 |
+ |
showHelp(); |
|
106 |
+ |
} else { |
|
107 |
+ |
switch (command) { |
|
108 |
+ |
case "init": |
|
109 |
+ |
await init(); |
|
110 |
+ |
break; |
|
111 |
+ |
case "add": |
|
112 |
+ |
await addComponent(componentName); |
|
113 |
+ |
break; |
|
114 |
+ |
case "help": |
|
115 |
+ |
showHelp(); |
|
116 |
+ |
break; |
|
117 |
+ |
default: |
|
118 |
+ |
if (!command) { |
|
119 |
+ |
showHelp(); |
|
120 |
+ |
} else { |
|
121 |
+ |
console.error(`❌ Unknown command: ${command}`); |
|
122 |
+ |
showHelp(); |
|
123 |
+ |
process.exit(1); |
|
124 |
+ |
} |
|
125 |
+ |
} |
|
126 |
+ |
} |