zobro-cli 1.1.7 → 1.1.8
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/.env +1 -0
- package/index.js +29 -81
- package/package.json +1 -1
package/.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
API_KEY=AIzaSyCzb_DQm2IL5v4reb2YcVxI_IoQ8z9iUEA
|
package/index.js
CHANGED
|
@@ -1,42 +1,25 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
const fs = require('fs'); //
|
|
4
|
-
const path = require('path'); // Node.js Path module
|
|
3
|
+
const fs = require('fs'); // <-- Added for file operations
|
|
5
4
|
const yargs = require('yargs/yargs');
|
|
6
5
|
const { hideBin } = require('yargs/helpers');
|
|
7
6
|
|
|
8
|
-
// ⚠️
|
|
9
|
-
const API_KEY = "
|
|
7
|
+
// ⚠️ Not recommended for public code!
|
|
8
|
+
const API_KEY = "YOUR_GEMINI_API_KEY_HERE";
|
|
10
9
|
|
|
11
10
|
/**
|
|
12
|
-
* Generates a
|
|
13
|
-
* @param {string} prompt - The user's
|
|
14
|
-
* @param {string}
|
|
11
|
+
* Generates code and saves it to a file or prints it to the console.
|
|
12
|
+
* @param {string} prompt - The user's question.
|
|
13
|
+
* @param {string} outputFile - The name of the file to save the code in.
|
|
15
14
|
*/
|
|
16
|
-
async function
|
|
17
|
-
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-
|
|
18
|
-
|
|
19
|
-
// --- This is the new, "smarter" prompt ---
|
|
20
|
-
// We are asking the AI to act as a file structure generator
|
|
21
|
-
// and return a specific JSON format.
|
|
22
|
-
const newPrompt = `
|
|
23
|
-
You are an expert software developer and project scaffolder.
|
|
24
|
-
Based on the user's prompt, generate a complete file structure as a JSON array.
|
|
25
|
-
Each object in the array must have two keys:
|
|
26
|
-
1. "filename": (string) The full relative path for the file (e.g., "index.html", "src/styles.css", "js/app.js").
|
|
27
|
-
2. "code": (string) The complete code for that file.
|
|
28
|
-
|
|
29
|
-
Only return the raw JSON array, with no other text, explanations, or markdown fences.
|
|
30
|
-
|
|
31
|
-
User Prompt: "${prompt}"
|
|
32
|
-
`;
|
|
33
|
-
|
|
15
|
+
async function generateCode(prompt, outputFile) {
|
|
16
|
+
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY}`;
|
|
34
17
|
const body = {
|
|
35
|
-
contents: [{ parts: [{ text:
|
|
18
|
+
contents: [{ parts: [{ text: `You are an expert code generation assistant. Provide only the code for the following prompt, without any extra explanation or markdown fences. Prompt: "${prompt}"` }] }]
|
|
36
19
|
};
|
|
37
20
|
|
|
38
21
|
try {
|
|
39
|
-
console.log(
|
|
22
|
+
console.log("🤖 Generating code...");
|
|
40
23
|
|
|
41
24
|
const response = await fetch(url, {
|
|
42
25
|
method: 'POST',
|
|
@@ -44,75 +27,39 @@ User Prompt: "${prompt}"
|
|
|
44
27
|
body: JSON.stringify(body),
|
|
45
28
|
});
|
|
46
29
|
|
|
47
|
-
if (!response.ok) {
|
|
48
|
-
const errorData = await response.json();
|
|
49
|
-
console.error('❌ API Error Details:', JSON.stringify(errorData.error, null, 2));
|
|
50
|
-
throw new Error(`API request failed with status ${response.status}`);
|
|
51
|
-
}
|
|
30
|
+
if (!response.ok) throw new Error(`API request failed with status ${response.status}`);
|
|
52
31
|
|
|
53
32
|
const data = await response.json();
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// --- NEW
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
let files;
|
|
65
|
-
try {
|
|
66
|
-
files = JSON.parse(responseText);
|
|
67
|
-
if (!Array.isArray(files)) throw new Error("AI did not return a JSON array.");
|
|
68
|
-
} catch (parseError) {
|
|
69
|
-
console.error("❌ ERROR: Failed to parse the AI's response. The response was not valid JSON.");
|
|
70
|
-
console.error("Raw AI Response:", responseText);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Create the main project directory
|
|
75
|
-
fs.mkdirSync(directoryName, { recursive: true });
|
|
76
|
-
console.log(``);
|
|
77
|
-
|
|
78
|
-
// Loop through the files array and create each file
|
|
79
|
-
for (const file of files) {
|
|
80
|
-
const filePath = path.join(directoryName, file.filename);
|
|
81
|
-
const fileDir = path.dirname(filePath);
|
|
82
|
-
|
|
83
|
-
// Create subdirectories if they don't exist
|
|
84
|
-
if (!fs.existsSync(fileDir)) {
|
|
85
|
-
fs.mkdirSync(fileDir, { recursive: true });
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Write the code to the file
|
|
89
|
-
fs.writeFileSync(filePath, file.code);
|
|
90
|
-
//console.log(`Created file: ${filePath}`);
|
|
33
|
+
const code = data.candidates[0].content.parts[0].text;
|
|
34
|
+
|
|
35
|
+
// --- NEW FILE SAVING LOGIC ---
|
|
36
|
+
if (outputFile) {
|
|
37
|
+
fs.writeFileSync(outputFile, code);
|
|
38
|
+
console.log(`✅ Code successfully saved to ${outputFile}`);
|
|
39
|
+
} else {
|
|
40
|
+
console.log(code); // If no file is specified, print to console
|
|
91
41
|
}
|
|
92
42
|
|
|
93
|
-
// console.log(`\n🎉 Project "${directoryName}" created successfully!`);
|
|
94
|
-
|
|
95
43
|
} catch (error) {
|
|
96
44
|
console.error('❌ An error occurred:', error.message);
|
|
97
45
|
}
|
|
98
46
|
}
|
|
99
47
|
|
|
100
|
-
//
|
|
48
|
+
// Setup the command-line interface with yargs
|
|
101
49
|
yargs(hideBin(process.argv))
|
|
102
50
|
.command(
|
|
103
|
-
'$0 <prompt>',
|
|
104
|
-
'Generates
|
|
51
|
+
'$0 <prompt>',
|
|
52
|
+
'Generates code based on a text prompt.',
|
|
105
53
|
(yargs) => {
|
|
106
54
|
return yargs
|
|
107
55
|
.positional('prompt', {
|
|
108
|
-
describe: 'The
|
|
56
|
+
describe: 'The code you want to generate',
|
|
109
57
|
type: 'string',
|
|
110
58
|
})
|
|
111
|
-
.option('
|
|
112
|
-
alias: '
|
|
113
|
-
describe: '
|
|
59
|
+
.option('output', { // <-- Add the output option
|
|
60
|
+
alias: 'o',
|
|
61
|
+
describe: 'File to save the generated code',
|
|
114
62
|
type: 'string',
|
|
115
|
-
demandOption: true, // This flag is now required
|
|
116
63
|
});
|
|
117
64
|
},
|
|
118
65
|
(argv) => {
|
|
@@ -120,8 +67,9 @@ yargs(hideBin(process.argv))
|
|
|
120
67
|
console.error('❌ ERROR: Please add your API key to the index.js file.');
|
|
121
68
|
return;
|
|
122
69
|
}
|
|
123
|
-
|
|
70
|
+
// Pass both the prompt and the output file to the function
|
|
71
|
+
generateCode(argv.prompt, argv.output);
|
|
124
72
|
}
|
|
125
73
|
)
|
|
126
|
-
.demandCommand(1, '
|
|
74
|
+
.demandCommand(1, 'You must provide a prompt to generate code.')
|
|
127
75
|
.parse();
|