This repository has been archived by the owner on Aug 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var sys = require('sys') | ||
var fs = require('fs') | ||
var exec = require('child_process').exec; | ||
var program = require('commander'); | ||
var databases = [] | ||
program | ||
.version('0.0.2') | ||
.parse(process.argv); | ||
if (!program.args[0]) return console.log('No CouchDB URL provided. Quiting now.') | ||
var couchUrl = program.args[0] | ||
var nano = require('nano')(couchUrl) | ||
|
||
function start() { | ||
getListOfDatabases() | ||
} | ||
|
||
function getListOfDatabases() { | ||
fs.readdir('./databases', function doneReadDir(err, files) { | ||
files.forEach(function addFile(element, index, array) { | ||
databases.push(element.substr(0, element.length - 3)) | ||
}) | ||
installDatabases() | ||
}) | ||
} | ||
|
||
var a = 0 | ||
|
||
function installDatabases() { | ||
var database = databases[a] | ||
if (a !== databases.length) { | ||
console.log("creating database " + database) | ||
nano.db.create(database, function(err, body) { | ||
if (err && err.error !== "file_exists") console.log(err) | ||
a++ | ||
return installDatabases() | ||
}) | ||
} else { | ||
return installDesignDocs() | ||
} | ||
} | ||
|
||
var b = 0 | ||
|
||
function installDesignDocs() { | ||
var database = databases[b] | ||
if (b !== databases.length) { | ||
if (database != "communities" && database != "languages" && database != "configurations") { | ||
console.log("Inserting design docs for the " + database + " database"); | ||
var docToPush = 'databases\\' + database + '.js'; | ||
var targetDb = couchUrl + '/' + database; | ||
exec('pushDocToDb.bat "' + docToPush + '" "' + targetDb + '"', function(error, stdout, stderr) { | ||
if (error) console.log(error); | ||
if (stderr) console.log(stderr); | ||
console.log(stdout) | ||
b++ | ||
installDesignDocs() | ||
}) | ||
} else { | ||
b++ | ||
installDesignDocs() | ||
} | ||
} else { | ||
createDefaultAdminMember() | ||
} | ||
} | ||
|
||
function createDefaultAdminMember() { | ||
nano.db.get('members', function(err, body) { | ||
if (err) console.log(err) | ||
// Cancel if there are already members in the database | ||
if (body.doc_count > 1) return done() | ||
console.log('Creating a default admin member of admin:password') | ||
var members = nano.db.use('members') | ||
var defaultAdminMember = { | ||
"login": "admin", | ||
"kind": "Member", | ||
"roles": ["Manager", "SuperManager"], | ||
"firstName": "Default", | ||
"lastName": "Admin", | ||
"password": "password", | ||
"Gender": "Male", | ||
"status": "active", | ||
"email": "[email protected]", | ||
"visits": 0 | ||
} | ||
members.insert(defaultAdminMember, function (err, res) { | ||
if (err) | ||
return console.log(err) | ||
else { | ||
var configurations = nano.db.use('configurations'); | ||
var configsDocPath = 'init_docs/ConfigurationsDoc-Nation.txt'; | ||
fs.readFile(configsDocPath, function (err, data) { | ||
configurations.insert(data, function (err, res) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log("******************Going to insert document into languages database*******************************"); | ||
insertLanguageDocs(); | ||
} | ||
done() | ||
}) | ||
}) | ||
} | ||
}); | ||
|
||
}); | ||
} | ||
|
||
function insertLanguageDocs() { | ||
var languages = nano.db.use('languages'); | ||
fs.readdir('./init_docs/languages', function doneReadDir(err, files) { | ||
files.forEach(function (element) { | ||
var langList = ['Arabic.txt', 'English.txt', 'Nepali.txt', 'Spanish.txt', 'Urdu (Pakistan).txt']; | ||
if (langList.indexOf(element) > -1) { | ||
var langDocPath = './init_docs/languages/' + element; | ||
console.log("Inserting " + element); | ||
fs.readFile(langDocPath, function (err, data) { | ||
languages.insert(data, function (err, res) { | ||
if (err) { | ||
console.log(err); | ||
} else { | ||
console.log(element + " doc inserted successfully."); | ||
} | ||
}); | ||
}); | ||
} | ||
}) | ||
}); | ||
} | ||
|
||
function done() { | ||
console.log('done!') | ||
} | ||
|
||
// | ||
// Trigger Start | ||
// | ||
|
||
start(); |