zobro-cli 1.1.6 → 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.
Files changed (3) hide show
  1. package/.env +1 -0
  2. package/index.js +57 -115
  3. package/package.json +1 -1
package/.env ADDED
@@ -0,0 +1 @@
1
+ API_KEY=AIzaSyCzb_DQm2IL5v4reb2YcVxI_IoQ8z9iUEA
package/index.js CHANGED
@@ -1,133 +1,75 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const fs = require('fs');
4
- const path = require('path');
5
- const os = require('os');
6
- const readline = require('readline');
7
-
8
- // ---------------- CONFIG ----------------
9
- const CONFIG_DIR = path.join(os.homedir(), '.projectgen');
10
- const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
11
-
12
- // ---------------- HELPERS ----------------
13
- function askQuestion(question) {
14
- const rl = readline.createInterface({
15
- input: process.stdin,
16
- output: process.stdout
17
- });
18
- return new Promise(resolve =>
19
- rl.question(question, answer => {
20
- rl.close();
21
- resolve(answer.trim());
22
- })
23
- );
24
- }
25
-
26
- async function getApiKey() {
27
- // 1️⃣ ENV variable (preferred)
28
- if (process.env.GEMINI_API_KEY) {
29
- return process.env.GEMINI_API_KEY;
30
- }
31
-
32
- // 2️⃣ Local config file
33
- if (fs.existsSync(CONFIG_FILE)) {
34
- const saved = JSON.parse(fs.readFileSync(CONFIG_FILE, 'utf8'));
35
- if (saved.apiKey) return saved.apiKey;
36
- }
37
-
38
- // 3️⃣ Ask user once
39
- console.log('🔐 Gemini API key not found.');
40
- const key = await askQuestion('👉 Please enter your Gemini API Key: ');
41
-
42
- if (!key) {
43
- console.error('❌ API key is required.');
44
- process.exit(1);
45
- }
46
-
47
- fs.mkdirSync(CONFIG_DIR, { recursive: true });
48
- fs.writeFileSync(CONFIG_FILE, JSON.stringify({ apiKey: key }, null, 2));
49
-
50
- console.log('✅ API key saved securely for future use.');
51
- return key;
52
- }
53
-
54
- // ---------------- CORE ----------------
55
- async function generateProject(prompt, directoryName) {
56
- const API_KEY = await getApiKey();
57
-
58
- const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent`;
59
-
60
- const newPrompt = `
61
- You are an expert software developer and project scaffolder.
62
- Based on the user's prompt, generate a complete file structure as a JSON array.
63
- Each object in the array must have:
64
- - filename
65
- - code
66
-
67
- Return ONLY raw JSON.
68
- User Prompt: "${prompt}"
69
- `;
70
-
3
+ const fs = require('fs'); // <-- Added for file operations
4
+ const yargs = require('yargs/yargs');
5
+ const { hideBin } = require('yargs/helpers');
6
+
7
+ // ⚠️ Not recommended for public code!
8
+ const API_KEY = "YOUR_GEMINI_API_KEY_HERE";
9
+
10
+ /**
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.
14
+ */
15
+ async function generateCode(prompt, outputFile) {
16
+ const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${API_KEY}`;
71
17
  const body = {
72
- contents: [{ parts: [{ text: newPrompt }] }]
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}"` }] }]
73
19
  };
74
20
 
75
21
  try {
76
- console.log(`🤖 Building project in "${directoryName}"...`);
22
+ console.log("🤖 Generating code...");
77
23
 
78
24
  const response = await fetch(url, {
79
25
  method: 'POST',
80
- headers: {
81
- 'Content-Type': 'application/json',
82
- 'x-goog-api-key': API_KEY // 🔐 SECURE
83
- },
84
- body: JSON.stringify(body)
26
+ headers: { 'Content-Type': 'application/json' },
27
+ body: JSON.stringify(body),
85
28
  });
86
29
 
87
- if (!response.ok) {
88
- const err = await response.text();
89
- throw new Error(err);
90
- }
30
+ if (!response.ok) throw new Error(`API request failed with status ${response.status}`);
91
31
 
92
32
  const data = await response.json();
93
- let responseText = data.candidates[0].content.parts[0].text;
94
- responseText = responseText.replace(/```json|```/g, '').trim();
95
-
96
- const files = JSON.parse(responseText);
97
-
98
- fs.mkdirSync(directoryName, { recursive: true });
99
-
100
- for (const file of files) {
101
- const filePath = path.join(directoryName, file.filename);
102
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
103
- fs.writeFileSync(filePath, file.code);
104
- console.log(`✅ Created: ${file.filename}`);
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
105
41
  }
106
42
 
107
- console.log(`🎉 Project ready in "${directoryName}"`);
108
-
109
- } catch (err) {
110
- console.error('❌ Error:', err.message);
43
+ } catch (error) {
44
+ console.error('❌ An error occurred:', error.message);
111
45
  }
112
46
  }
113
47
 
114
- // ---------------- CLI ----------------
115
- async function startCLI() {
116
- const yargs = (await import('yargs/yargs')).default;
117
- const { hideBin } = await import('yargs/helpers');
118
-
119
- yargs(hideBin(process.argv))
120
- .command(
121
- '$0 <prompt>',
122
- 'Generate a full project',
123
- y => y.option('directory', {
124
- alias: 'd',
125
- type: 'string',
126
- demandOption: true
127
- }),
128
- argv => generateProject(argv.prompt, argv.directory)
129
- )
130
- .parse();
131
- }
132
-
133
- startCLI();
48
+ // Setup the command-line interface with yargs
49
+ yargs(hideBin(process.argv))
50
+ .command(
51
+ '$0 <prompt>',
52
+ 'Generates code based on a text prompt.',
53
+ (yargs) => {
54
+ return yargs
55
+ .positional('prompt', {
56
+ describe: 'The code you want to generate',
57
+ type: 'string',
58
+ })
59
+ .option('output', { // <-- Add the output option
60
+ alias: 'o',
61
+ describe: 'File to save the generated code',
62
+ type: 'string',
63
+ });
64
+ },
65
+ (argv) => {
66
+ if (!API_KEY || API_KEY === "YOUR_GEMINI_API_KEY_HERE") {
67
+ console.error('❌ ERROR: Please add your API key to the index.js file.');
68
+ return;
69
+ }
70
+ // Pass both the prompt and the output file to the function
71
+ generateCode(argv.prompt, argv.output);
72
+ }
73
+ )
74
+ .demandCommand(1, 'You must provide a prompt to generate code.')
75
+ .parse();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zobro-cli",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "main": "index.js",
5
5
  "bin": {
6
6
  "zobro-cli": "./index.js"