-
Notifications
You must be signed in to change notification settings - Fork 1
/
createSuperman.js
69 lines (63 loc) · 2.14 KB
/
createSuperman.js
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
const SuperUser = require("./models/SuperUser");
const mongoose= require ("mongoose");
const config= require("./utils/config");
const del = require ('del');
const fs= require('fs')
// const { delete } = require("./app");
// if(process.argv.length<3) console.log("enter username and password");
async function dropDB(){
const connection=mongoose.createConnection(config.MONGODB_URL,{
useNewUrlParser:true,
useUnifiedTopology:true,
useFindAndModify:false,
useCreateIndex:true
});
connection.dropDatabase().then(async()=>{
console.log("db droped");
await deleteData();
createSuperUser();
});
}
async function createSuperUser(){
mongoose.connect(config.MONGODB_URL,{
useNewUrlParser:true,
useUnifiedTopology:true,
useFindAndModify:false,
useCreateIndex:true
});
const username=process.env.SUPER_NAME;
const password=process.env.SUPER_PASS;
if(!username || !password ){
throw Error ("SUPER_NAME and SUPER_PASS should be added to env variables")
}
const newSuper= new SuperUser({
username:username,
password:password,
});
newSuper.save().then(result=>{
console.log("superuser created");
mongoose.disconnect();
}).catch(error=> console.log(error));
}
async function deleteData(){
const directories = await fs.readdirSync('./data/questions', { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
directories.forEach(directory=>{
del('./data/questions/'+directory);
});
const submits = fs.readdirSync('./data/user-submits', { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
submits.forEach(submit=>{
del('./data/user-submits/'+submit);
});
const userData = fs.readdirSync('./data/user-data', { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
userData.forEach(data=>{
del('./data/user-data/'+data);
});
console.log('deleted userdata')
}
dropDB();