Skip to content

Commit 7441f38

Browse files
committed
add convenience command create-env from config json
1 parent 481e66c commit 7441f38

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

bin/commands/config/create-env.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const { formatSuccess, formatError } = require('../../utils/formatting');
4+
const { handleError } = require('../../utils/error-handling');
5+
6+
async function createEnvFile() {
7+
try {
8+
// Check if .mongodb-rag.json exists
9+
const configPath = path.join(process.cwd(), '.mongodb-rag.json');
10+
if (!fs.existsSync(configPath)) {
11+
throw new Error('Configuration file .mongodb-rag.json not found. Please run "npx mongodb-rag init" first.');
12+
}
13+
14+
// Read the config file
15+
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
16+
17+
// Create .env content
18+
const envContent = [
19+
`MONGODB_URI="${config.mongodbUri}"`,
20+
`EMBEDDING_PROVIDER="${config.embeddingProvider}"`,
21+
`EMBEDDING_API_KEY="${config.embeddingApiKey}"`,
22+
`EMBEDDING_MODEL="${config.embeddingModel}"`,
23+
`VECTOR_INDEX="${config.vectorSearchIndex}"`,
24+
`MONGODB_DATABASE_NAME="${config.databaseName}"`,
25+
`MONGODB_COLLECTION_NAME="${config.collectionName}"`,
26+
].join('\n');
27+
28+
// Write to .env file
29+
const envPath = path.join(process.cwd(), '.env');
30+
fs.writeFileSync(envPath, envContent);
31+
32+
console.log(formatSuccess('.env file created successfully!'));
33+
} catch (error) {
34+
handleError(error);
35+
}
36+
}
37+
38+
module.exports = createEnvFile;

bin/mongodb-rag.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ program
6969
.description('MongoDB RAG CLI for managing vector search and RAG operations')
7070
.version(packageJson.version);
7171

72-
// Create RAG App
72+
// Wrap your commands with error handling
73+
const wrappedCreateRagApp = wrapCommand(createRagApp);
74+
75+
// Use the wrapped version in your command handling
7376
program
7477
.command('create-rag-app <projectName>')
7578
.description('Scaffold a new CRUD RAG application with MongoDB and Vector Search')
76-
.action(wrapCommand(createRagApp));
79+
.action(wrappedCreateRagApp);
7780

7881
// Initialize configuration
7982
program

bin/utils/error-handling.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
// bin/utils/error-handling.js
22
import chalk from 'chalk';
33

4+
/**
5+
* Wraps a command function with error handling
6+
* @param {Function} commandFn The command function to wrap
7+
* @returns {Function} Wrapped command function with error handling
8+
*/
9+
export const wrapCommand = (commandFn) => {
10+
return async (...args) => {
11+
try {
12+
await commandFn(...args);
13+
} catch (error) {
14+
console.error('\x1b[31m%s\x1b[0m', 'Error:', error.message);
15+
process.exit(1);
16+
}
17+
};
18+
};
19+
20+
/**
21+
* Generic error handler for async operations
22+
* @param {Error} error The error to handle
23+
*/
24+
export const handleError = (error) => {
25+
console.error('\x1b[31m%s\x1b[0m', 'Error:', error.message);
26+
process.exit(1);
27+
};
28+
429
export function withErrorHandling(command) {
530
return async (...args) => {
631
try {

mongodb-rag-docs/docs/cli-reference.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,19 @@ npx mongodb-rag reset-config
112112

113113
Resets the configuration by re-running the setup.
114114

115+
### **Create Environment File**
116+
117+
```
118+
npx mongodb-rag create-env
119+
```
120+
121+
Creates a `.env` file from your `.mongodb-rag.json` configuration. This command generates environment variables needed for your RAG application:
122+
- MONGODB_URI
123+
- EMBEDDING_PROVIDER
124+
- EMBEDDING_API_KEY
125+
- EMBEDDING_MODEL
126+
- VECTOR_INDEX
127+
- MONGODB_DATABASE_NAME
128+
- MONGODB_COLLECTION_NAME
129+
115130
For more details, refer to the MongoDB-RAG API Reference.

0 commit comments

Comments
 (0)