@thebros/create-benjamin 1.0.12 ā 1.0.13
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.
- package/bin/index.js +36 -32
- package/package.json +1 -1
package/bin/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
2
|
import "dotenv/config";
|
|
4
3
|
import fs from "fs-extra";
|
|
5
4
|
import path from "path";
|
|
@@ -11,6 +10,9 @@ import OpenAI from "openai";
|
|
|
11
10
|
async function main() {
|
|
12
11
|
console.log(chalk.cyan("\nš¤ Create Benjamin AI Generator\n"));
|
|
13
12
|
|
|
13
|
+
// ===== DEBUG: Check if .env loaded =====
|
|
14
|
+
console.log("Key loaded from env:", !!process.env.OPENAI_API_KEY);
|
|
15
|
+
|
|
14
16
|
// ===== GET API KEY =====
|
|
15
17
|
let apiKey = process.env.OPENAI_API_KEY;
|
|
16
18
|
|
|
@@ -23,10 +25,15 @@ async function main() {
|
|
|
23
25
|
},
|
|
24
26
|
]);
|
|
25
27
|
|
|
26
|
-
apiKey = answer.key;
|
|
28
|
+
apiKey = answer.key.trim(); // ā
trim whitespace
|
|
27
29
|
|
|
28
30
|
if (!apiKey) {
|
|
29
|
-
console.log("ā No API key provided");
|
|
31
|
+
console.log(chalk.red("ā No API key provided"));
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!apiKey.startsWith("sk-")) {
|
|
36
|
+
console.log(chalk.red("ā That doesn't look like a valid OpenAI key (should start with sk-)"));
|
|
30
37
|
process.exit(1);
|
|
31
38
|
}
|
|
32
39
|
}
|
|
@@ -52,7 +59,7 @@ async function main() {
|
|
|
52
59
|
const root = path.join(process.cwd(), projectName);
|
|
53
60
|
|
|
54
61
|
if (fs.existsSync(root)) {
|
|
55
|
-
console.log(chalk.red("Project already exists"));
|
|
62
|
+
console.log(chalk.red("ā Project already exists"));
|
|
56
63
|
process.exit(1);
|
|
57
64
|
}
|
|
58
65
|
|
|
@@ -61,40 +68,43 @@ async function main() {
|
|
|
61
68
|
const spinner = ora("Generating project with AI...").start();
|
|
62
69
|
|
|
63
70
|
// ===== PROMPT =====
|
|
64
|
-
const prompt = `
|
|
65
|
-
You are a senior full-stack developer.
|
|
66
|
-
|
|
71
|
+
const prompt = `You are a senior full-stack developer.
|
|
67
72
|
Generate a full project for:
|
|
68
|
-
|
|
69
73
|
${answers.description}
|
|
70
74
|
|
|
71
75
|
Return ONLY valid JSON:
|
|
72
|
-
|
|
73
|
-
{
|
|
74
|
-
"files": {
|
|
75
|
-
"path/to/file": "file content"
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
`;
|
|
76
|
+
{"files": {"path/to/file": "file content"}}`;
|
|
79
77
|
|
|
80
78
|
// ===== AI CALL =====
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
79
|
+
let response;
|
|
80
|
+
try {
|
|
81
|
+
response = await openai.chat.completions.create({
|
|
82
|
+
model: "gpt-4o-mini",
|
|
83
|
+
messages: [{ role: "user", content: prompt }],
|
|
84
|
+
temperature: 0.3,
|
|
85
|
+
});
|
|
86
|
+
} catch (err) {
|
|
87
|
+
spinner.fail("OpenAI API call failed");
|
|
88
|
+
console.log(chalk.red("Error: " + err.message));
|
|
89
|
+
if (err.status === 401) {
|
|
90
|
+
console.log(chalk.yellow("ā Your API key is invalid or expired. Check: https://platform.openai.com/api-keys"));
|
|
91
|
+
} else if (err.status === 429) {
|
|
92
|
+
console.log(chalk.yellow("ā Rate limit or quota exceeded. Check your usage at: https://platform.openai.com/usage"));
|
|
93
|
+
} else if (err.status === 404) {
|
|
94
|
+
console.log(chalk.yellow("ā Model not available on your account. Try changing model to 'gpt-3.5-turbo'"));
|
|
95
|
+
}
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
86
98
|
|
|
87
99
|
let text = response.choices[0].message.content;
|
|
88
|
-
|
|
89
|
-
text = text.replace(/```json/g, "").replace(/```/g, "");
|
|
100
|
+
text = text.replace(/```json/g, "").replace(/```/g, "").trim();
|
|
90
101
|
|
|
91
102
|
let project;
|
|
92
|
-
|
|
93
103
|
try {
|
|
94
104
|
project = JSON.parse(text);
|
|
95
105
|
} catch (err) {
|
|
96
106
|
spinner.fail("AI returned invalid JSON");
|
|
97
|
-
console.log(text);
|
|
107
|
+
console.log(chalk.red("Raw response:"), text);
|
|
98
108
|
process.exit(1);
|
|
99
109
|
}
|
|
100
110
|
|
|
@@ -106,14 +116,8 @@ Return ONLY valid JSON:
|
|
|
106
116
|
}
|
|
107
117
|
|
|
108
118
|
spinner.succeed("Project generated successfully");
|
|
109
|
-
|
|
110
119
|
console.log(chalk.green("\nā
AI project created!"));
|
|
111
|
-
|
|
112
|
-
console.log(`
|
|
113
|
-
cd ${projectName}
|
|
114
|
-
npm install
|
|
115
|
-
npm run dev
|
|
116
|
-
`);
|
|
120
|
+
console.log(`cd ${projectName}\nnpm install\nnpm run dev`);
|
|
117
121
|
}
|
|
118
122
|
|
|
119
|
-
main();
|
|
123
|
+
main();
|