@brightsideoy/kama 0.1.0-alpha.17 → 0.1.0-alpha.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/cli.js +53 -4
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ import { execSync } from 'node:child_process';
2
3
  import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
3
4
  import { join } from 'node:path';
4
5
  import * as readline from 'node:readline/promises';
@@ -35,10 +36,54 @@ async function createFullWorkspace() {
35
36
  const siteDomain = (await rl.question('First Website Domain (e.g., www.example.com): ')) || 'www.example.com';
36
37
 
37
38
  const rootDir = join(process.cwd(), projName);
39
+ const dbName = `${projName}-db`.replace(/[^a-zA-Z0-9_-]/g, '_'); // safe DB name
38
40
 
39
41
  console.log(`\nšŸ“ Creating workspace folder at ./${projName}...`);
40
42
  mkdirSync(rootDir, { recursive: true });
41
43
 
44
+ // --- AUTO D1 CREATION MAGIC ---
45
+ let d1DatabaseId = 'REPLACE_WITH_YOUR_D1_ID';
46
+ let isAuthenticated = false;
47
+
48
+ console.log(`\nšŸ”‘ Checking Cloudflare authentication...`);
49
+ try {
50
+ execSync('npx wrangler whoami', { stdio: 'pipe' });
51
+ isAuthenticated = true;
52
+ console.log(`āœ… Logged in to Cloudflare.`);
53
+ } catch (error) {
54
+ console.log(`āš ļø You are not logged in to Cloudflare.`);
55
+ console.log(`🌐 Launching browser for Cloudflare login...`);
56
+ try {
57
+ execSync('npx wrangler login', { stdio: 'inherit' });
58
+ isAuthenticated = true;
59
+ console.log(`āœ… Successfully logged in!`);
60
+ } catch (loginError) {
61
+ console.log(`āŒ Login failed or was cancelled.`);
62
+ }
63
+ }
64
+
65
+ if (isAuthenticated) {
66
+ console.log(`\nšŸ—„ļø Attempting to auto-create Cloudflare D1 Database '${dbName}'...`);
67
+ try {
68
+ const d1Output = execSync(`npx wrangler d1 create ${dbName}`, { encoding: 'utf-8', stdio: 'pipe' });
69
+
70
+ const uuidRegex = /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/;
71
+ const match = d1Output.match(uuidRegex);
72
+
73
+ if (match) {
74
+ d1DatabaseId = match[0];
75
+ console.log(`āœ… Success! Database ID bound: ${d1DatabaseId}`);
76
+ } else {
77
+ console.log(`āš ļø Could not parse the database ID from Wrangler. You may need to update wrangler.json manually.`);
78
+ }
79
+ } catch (error) {
80
+ console.log(`āš ļø Auto-creation failed. We'll leave a placeholder.`);
81
+ }
82
+ } else {
83
+ console.log(`āš ļø Skipping database creation. We'll leave a placeholder in wrangler.json.`);
84
+ }
85
+ // ------------------------------
86
+
42
87
  // 1. Root package.json
43
88
  writeFileSync(
44
89
  join(rootDir, 'package.json'),
@@ -92,8 +137,8 @@ async function createFullWorkspace() {
92
137
  vars: { CDN_DOMAIN: cmsDomain },
93
138
  d1_databases: [{
94
139
  binding: 'DB',
95
- database_name: 'kama_cms_db',
96
- database_id: 'REPLACE_WITH_YOUR_D1_ID',
140
+ database_name: dbName,
141
+ database_id: d1DatabaseId,
97
142
  }],
98
143
  r2_buckets: [{
99
144
  binding: 'MEDIA_BUCKET',
@@ -103,13 +148,17 @@ async function createFullWorkspace() {
103
148
  );
104
149
 
105
150
  // 3. Setup First Astro Site
106
- const siteFolderName = siteDomain.split('.')[0]; // e.g. "www" or "demo"
151
+ const siteFolderName = siteDomain.split('.')[0];
107
152
  await generateAstroSite(rootDir, siteFolderName, siteDomain, cmsDomain);
108
153
 
109
154
  console.log(`\n✨ Workspace ready! Next steps:`);
110
155
  console.log(` 1. cd ${projName}`);
111
156
  console.log(` 2. npm install`);
112
- console.log(` 3. Update D1 ID in cms-worker/wrangler.json`);
157
+ if (d1DatabaseId === 'REPLACE_WITH_YOUR_D1_ID') {
158
+ console.log(` 3. Run 'npx wrangler login', create a D1 database, and update cms-worker/wrangler.json`);
159
+ } else {
160
+ console.log(` 3. You are fully configured! Run 'npm run dev:cms' from the workspace root to start the backend.`);
161
+ }
113
162
  }
114
163
 
115
164
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brightsideoy/kama",
3
- "version": "0.1.0-alpha.17",
3
+ "version": "0.1.0-alpha.18",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",