zobro-cli 1.1.8 → 1.2.0

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 (3) hide show
  1. package/index.js +81 -29
  2. package/package.json +1 -1
  3. package/.env +0 -1
package/index.js CHANGED
@@ -1,25 +1,42 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const fs = require('fs'); // <-- Added for file operations
3
+ const fs = require('fs'); // Node.js File System
4
+ const path = require('path'); // Node.js Path module
4
5
  const yargs = require('yargs/yargs');
5
6
  const { hideBin } = require('yargs/helpers');
6
7
 
7
- // ⚠️ Not recommended for public code!
8
- const API_KEY = "YOUR_GEMINI_API_KEY_HERE";
8
+ // ⚠️ Paste your official Google Gemini API key here.
9
+ const API_KEY = "AIzaSyCuqR5sFFQMzso-mwknatXRyH9_-9lav5s";
9
10
 
10
11
  /**
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.
12
+ * Generates a full project structure based on a prompt.
13
+ * @param {string} prompt - The user's project request.
14
+ * @param {string} directoryName - The name of the folder to create the project in.
14
15
  */
15
- async function generateCode(prompt, outputFile) {
16
- const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY}`;
16
+ async function generateProject(prompt, directoryName) {
17
+ const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${API_KEY}`;
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
+
17
34
  const body = {
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}"` }] }]
35
+ contents: [{ parts: [{ text: newPrompt }] }]
19
36
  };
20
37
 
21
38
  try {
22
- console.log("🤖 Generating code...");
39
+ console.log(`Code Running in Expresss`);
23
40
 
24
41
  const response = await fetch(url, {
25
42
  method: 'POST',
@@ -27,39 +44,75 @@ async function generateCode(prompt, outputFile) {
27
44
  body: JSON.stringify(body),
28
45
  });
29
46
 
30
- if (!response.ok) throw new Error(`API request failed with status ${response.status}`);
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
+ }
31
52
 
32
53
  const data = await response.json();
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
54
+ let responseText = data.candidates[0].content.parts[0].text;
55
+
56
+ // --- NEW JSON PARSING & FILE CREATION LOGIC ---
57
+ // console.log("Building project...");
58
+
59
+ // Clean up potential markdown fences from the AI's response
60
+ if (responseText.startsWith("```json")) {
61
+ responseText = responseText.substring(7, responseText.length - 3).trim();
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}`);
41
91
  }
42
92
 
93
+ // console.log(`\n🎉 Project "${directoryName}" created successfully!`);
94
+
43
95
  } catch (error) {
44
96
  console.error('❌ An error occurred:', error.message);
45
97
  }
46
98
  }
47
99
 
48
- // Setup the command-line interface with yargs
100
+ // --- NEW YARGS SETUP ---
49
101
  yargs(hideBin(process.argv))
50
102
  .command(
51
- '$0 <prompt>',
52
- 'Generates code based on a text prompt.',
103
+ '$0 <prompt>', // The default command
104
+ 'Generates a full project structure from a text prompt.',
53
105
  (yargs) => {
54
106
  return yargs
55
107
  .positional('prompt', {
56
- describe: 'The code you want to generate',
108
+ describe: 'The project you want to generate',
57
109
  type: 'string',
58
110
  })
59
- .option('output', { // <-- Add the output option
60
- alias: 'o',
61
- describe: 'File to save the generated code',
111
+ .option('directory', { // Replaces the old 'output' flag
112
+ alias: 'd',
113
+ describe: 'The name of the new directory to create the project in',
62
114
  type: 'string',
115
+ demandOption: true, // This flag is now required
63
116
  });
64
117
  },
65
118
  (argv) => {
@@ -67,9 +120,8 @@ yargs(hideBin(process.argv))
67
120
  console.error('❌ ERROR: Please add your API key to the index.js file.');
68
121
  return;
69
122
  }
70
- // Pass both the prompt and the output file to the function
71
- generateCode(argv.prompt, argv.output);
123
+ generateProject(argv.prompt, argv.directory);
72
124
  }
73
125
  )
74
- .demandCommand(1, 'You must provide a prompt to generate code.')
126
+ .demandCommand(1, 'Please provide a prompt.')
75
127
  .parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zobro-cli",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "main": "index.js",
5
5
  "bin": {
6
6
  "zobro-cli": "./index.js"
package/.env DELETED
@@ -1 +0,0 @@
1
- API_KEY=AIzaSyCzb_DQm2IL5v4reb2YcVxI_IoQ8z9iUEA