This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmigrate.js
More file actions
92 lines (76 loc) 路 2.68 KB
/
Copy pathmigrate.js
File metadata and controls
92 lines (76 loc) 路 2.68 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
const fs = require('fs');
require('dotenv').config();
const path = require('path');
const { MongoClient } = require('mongodb');
const mongoose = require('mongoose');
const userSchema = require('./data'); // Import your Mongoose schema definition
// bcrypt
const bcrypt = require('bcrypt');
const uri = process.env.MONGODB_URI || 'mongodb://localhost:27017/';
const dbName = process.env.DATABASE_NAME || 'your_database_name';
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.connect(uri + "hosting-config", { useNewUrlParser: true, useUnifiedTopology: true });
async function processConfigFiles(directoryPath) {
try {
await client.connect();
console.log('Connected to MongoDB');
const db = client.db(dbName);
const folderName = path.basename(directoryPath);
const files = fs.readdirSync(directoryPath);
for (const file of files) {
const filePath = path.join(directoryPath, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// Recurse into subdirectory
await processConfigFiles(filePath);
} else if (file === 'config.json') {
const configData = fs.readFileSync(filePath, 'utf-8');
const configObj = JSON.parse(configData);
let pas = '';
// hash password
if (configObj.ftp_password) {
pas = await bcrypt.hash(configObj.ftp_password, 10);
}
// Extract relevant fields from configObj
const userData = {
domain: folderName,
HashedPassword: pas,
FTP: configObj.ftp,
ACTIVATED: 'true',
};
// Conditionally add HashPagePassword if it exists
if (configObj.HashPagePassword) {
userData.HashPagePassword = configObj.HashPagePassword;
}
if (configObj.SMTP) {
userData.EMAIL = configObj.SMTP;
}
if (configObj.ACTIVATED) {
userData.ACTIVATED = configObj.ACTIVATED;
}
if (configObj.ACTIVATION_CODE) {
userData.ACTIVATION_CODE = configObj.ACTIVATION_CODE;
}
// Create a new Mongoose document
const userDocument = new userSchema(userData);
// Save the document to MongoDB using Mongoose
await userDocument.save();
console.log(`Inserted data from ${filePath} into MongoDB`);
}
}
} catch (error) {
console.error('Error:', error);
} finally {
await client.close();
console.log('Disconnected from MongoDB');
}
}
async function main() {
try {
await processConfigFiles('content/');
} finally {
await mongoose.connection.close();
console.log('Disconnected from MongoDB (Mongoose)');
}
}
main();