forked from ivanyyuk/web-refinery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
101 lines (85 loc) · 2.64 KB
/
seed.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
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
98
99
100
101
/*
This seed file is only a placeholder. It should be expanded and altered
to fit the development of your application.
It uses the same file the server uses to establish
the database connection:
--- server/db/index.js
The name of the database used is set in your environment files:
--- server/env/*
This seed file has a safety check to see if you already have users
in the database. If you are developing multiple applications with the
fsg scaffolding, keep in mind that fsg always uses the same database
name in the environment files.
*/
var chalk = require('chalk');
var db = require('./server/db');
var User = db.model('user');
var fs = require('fs');
var path = require('path');
var Promise = require('sequelize').Promise;
var seedUsers = function () {
var users = [
{
email: '[email protected]',
password: 'password'
},
{
email: '[email protected]',
password: 'potus'
}
];
var creatingUsers = users.map(function (userObj) {
return User.create(userObj);
});
return Promise.all(creatingUsers);
};
var remakeDir = function(dir) {
try {
var list = fs.readdirSync(dir);
for(var i = 0; i < list.length; i++) {
var filename = path.join(dir, list[i]);
var stat = fs.statSync(filename);
if(filename == "." || filename == "..") {
// pass these files
} else if(stat.isDirectory()) {
// remakeDir recursively
remakeDir(filename);
} else {
// rm fiilename
fs.unlinkSync(filename);
}
}
// Remove hosted-projects and sub directories
fs.rmdirSync(dir);
} catch(e) {
if ( e.code != 'ERROR OCCURRED IN REMAKEDIR' ) throw e;
}
};
var hostedProjectsPath = __dirname + '/hosted-projects/';
var mkdirSync = function (path) {
try {
remakeDir(path);
// Recreates the hosted-projects directory
fs.mkdirSync(dir);
// Directories were removed successfully
console.log(chalk.green('hosted-projects directory was remade.'))
} catch(e) {
// If user doesn't have the hosted-projects
// directory, it's made here
fs.mkdirSync(path);
console.log(chalk.green('hosted-projects directory was created.'));
}
}
mkdirSync(hostedProjectsPath);
db.sync({ force: true })
.then(function () {
return seedUsers();
})
.then(function () {
console.log(chalk.green('Seed successful!'));
process.exit(0);
})
.catch(function (err) {
console.error(err);
process.exit(1);
});