-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (80 loc) · 2.91 KB
/
Copy pathindex.js
File metadata and controls
97 lines (80 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
// index.js
const fs = require('fs');
const path = require('path');
const readline = require('readline');
require('dotenv').config();
const { GoogleGenerativeAI } = require('@google/generative-ai');
const figlet = require('figlet');
// Function to display the welcome message
function displayWelcomeMessage() {
figlet('CodeGen!', (err, data) => {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return;
}
console.log(data);
});
}
displayWelcomeMessage();
const apiKeyFile = path.join(__dirname, 'apikey.json');
// Function to get or prompt for the API key
async function getApiKey() {
if (fs.existsSync(apiKeyFile)) {
const apiKeyData = fs.readFileSync(apiKeyFile);
const { apiKey } = JSON.parse(apiKeyData);
return apiKey;
} else {
const apiKey = await askQuestion('Please enter your API key: ');
// Store the API key in a JSON file
fs.writeFileSync(apiKeyFile, JSON.stringify({ apiKey }));
return apiKey;
}
}
// Function to ask for user input
function askQuestion(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question(query, (answer) => {
rl.close();
resolve(answer);
});
});
}
// Function to generate code snippet
async function generateSnippet(apiKey, description, language) {
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
try {
// prompt to ensure the right language
const prompt = `Generate a ${language} code snippet for the following description: "${description}". Provide only the code, no explanations or additional text. Make sure the code is syntactically correct and follows best practices for ${language}.`;
const result = await model.generateContent([prompt]);
const snippet = result.response.text();
return snippet;
} catch (error) {
console.error(`Error generating snippet: ${error.message}`);
throw error;
}
}
// Main function to run the CLI
async function main() {
// First, get the API key
const apiKey = await getApiKey();
// Then, ask for snippet details
const description = await askQuestion('Describe the code snippet you need (e.g., "JWT login"): ');
const language = await askQuestion('Enter the programming language (e.g., JavaScript, Python, Java): ');
const fileName = await askQuestion('Enter the file name to save the snippet (e.g., snippet.js): ');
try {
const snippet = await generateSnippet(apiKey, description, language);
// Save the snippet to the specified file
fs.writeFileSync(fileName, snippet);
console.log(`Snippet saved to ${fileName}`);
} catch (error) {
console.error(`Failed to generate a valid snippet. ${error.message}`);
}
}
main();