mongor-cli 1.0.3 → 1.0.4
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/index.js +127 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1 +1,127 @@
|
|
|
1
|
-
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs'); // Node.js File System
|
|
4
|
+
const path = require('path'); // Node.js Path module
|
|
5
|
+
const yargs = require('yargs/yargs');
|
|
6
|
+
const { hideBin } = require('yargs/helpers');
|
|
7
|
+
|
|
8
|
+
// ⚠️ Paste your official Google Gemini API key here.
|
|
9
|
+
const API_KEY = "AIzaSyCjNEwALBWSR8xmUv-kDIOKZlz40zJgGLE";
|
|
10
|
+
|
|
11
|
+
/**
|
|
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.
|
|
15
|
+
*/
|
|
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
|
+
|
|
34
|
+
const body = {
|
|
35
|
+
contents: [{ parts: [{ text: newPrompt }] }]
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
console.log(`🤖 Generating project structure for "${prompt}"...`);
|
|
40
|
+
|
|
41
|
+
const response = await fetch(url, {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers: { 'Content-Type': 'application/json' },
|
|
44
|
+
body: JSON.stringify(body),
|
|
45
|
+
});
|
|
46
|
+
|
|
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
|
+
}
|
|
52
|
+
|
|
53
|
+
const data = await response.json();
|
|
54
|
+
let responseText = data.candidates[0].content.parts[0].text;
|
|
55
|
+
|
|
56
|
+
// --- NEW JSON PARSING & FILE CREATION LOGIC ---
|
|
57
|
+
console.log("🤖 AI response received. 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(`📁 Created base directory: ${directoryName}`);
|
|
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}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
console.log(`\n🎉 Project "${directoryName}" created successfully!`);
|
|
94
|
+
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.error('❌ An error occurred:', error.message);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// --- NEW YARGS SETUP ---
|
|
101
|
+
yargs(hideBin(process.argv))
|
|
102
|
+
.command(
|
|
103
|
+
'$0 <prompt>', // The default command
|
|
104
|
+
'Generates a full project structure from a text prompt.',
|
|
105
|
+
(yargs) => {
|
|
106
|
+
return yargs
|
|
107
|
+
.positional('prompt', {
|
|
108
|
+
describe: 'The project you want to generate',
|
|
109
|
+
type: 'string',
|
|
110
|
+
})
|
|
111
|
+
.option('directory', { // Replaces the old 'output' flag
|
|
112
|
+
alias: 'd',
|
|
113
|
+
describe: 'The name of the new directory to create the project in',
|
|
114
|
+
type: 'string',
|
|
115
|
+
demandOption: true, // This flag is now required
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
(argv) => {
|
|
119
|
+
if (!API_KEY || API_KEY === "YOUR_GEMINI_API_KEY_HERE") {
|
|
120
|
+
console.error('❌ ERROR: Please add your API key to the index.js file.');
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
generateProject(argv.prompt, argv.directory);
|
|
124
|
+
}
|
|
125
|
+
)
|
|
126
|
+
.demandCommand(1, 'Please provide a prompt.')
|
|
127
|
+
.parse();
|