This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
forked from nyjy85/quick_trip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
77 lines (69 loc) · 2 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
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
var path = require('path');
var mongoose = require('mongoose');
var startDb = require('./server/db');
var Nodemodule = mongoose.model('Nodemodule');
var chalk = require('chalk');
var moduleNamesArray;
startDb.then(function() {
// Get all module names
fs.readdirAsync(__dirname + '/node_modules/')
// Get package.json content for all modules
.then(function(moduleNames) {
moduleNamesArray = moduleNames.filter(function(e) {
return e !== ".bin";
});
return Promise.all(moduleNamesArray.map(function(module){
return fs.readFileAsync(path.join(__dirname, '/node_modules/' + module + '/package.json'), {'encoding': 'utf8'});
}));
})
// Parse json
.then(function(files) {
return Promise.all(files.map(function(file){
return JSON.parse(file);
}));
})
// Get repository urls
.then(function(parsed) {
return Promise.all(parsed.map(function(obj) {
return obj.repository.url;
}));
})
// Create model-compatible objects
.then(function(repos) {
return Promise.all(moduleNamesArray.map(function(e, i) {
// Make url protocols consistent
var url = urlCleaner(repos[i]);
return {
title: e,
repoUrl: url
};
}));
})
// Write to the db and exit
.then(function(moduleObjects) {
moduleObjects.forEach(function(e) {
Nodemodule.create(e)
.then(function(){
console.log(chalk.green('Database seeded. Goodbye!'));
process.exit(0);
});
});
});
});
function urlCleaner(url) {
var start;
var protocol;
if (url.indexOf('https') === 0) return url;
if (url.indexOf("://") > 0) {
start = url.indexOf("://");
protocol = url.slice(0, start);
return "https" + url.slice(start);
}
else {
start = url.indexOf(":") + 1;
protocol = url.slice(0, start);
return "https://" + url.slice(start);
}
}