forked from CoderDojo/cp-local-development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
103 lines (87 loc) · 3 KB
/
init.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
102
103
var util = require('util');
var debug = require('debug')('localdev:init');
var async = require('async');
var mkdirp = require('mkdirp');
var command = require('./command.js');
var fs = require('fs');
module.exports = function (argv, systems, cb) {
debug(system);
var usage = 'Usage: init <system-name>\n e.g. init phase3';
var sysName = argv._[1];
if (!sysName) return cb(usage);
if (!systems[sysName]) return cb('System not found: ' + sysName);
var system = systems[sysName];
var workspace = 'workspace-' + sysName;
console.log('System:', sysName, util.inspect(system.stringify(), true, null), 'workspace: ' + workspace);
// do all the setup
async.series([
createWorkspace,
initRepos,
npmInstalls,
createDatabases
], cb);
function createWorkspace (cb) {
mkdirp(workspace, cb);
}
function initRepos (cb) {
async.mapSeries(system.services, initRepo, cb);
}
function npmInstalls (cb) {
async.mapSeries(system.services, npmInstall, cb);
}
function initRepo (service, cb) {
fs.exists(workspace + '/' + service.name, function (exists) {
if (exists === true) return cb();
async.series([
function (cb) { cloneRepo(service, cb); },
function (cb) { checkoutBranch(service, cb); }
], cb);
});
}
function cloneRepo (service, cb) {
var cmd = 'git clone ' + service.repo + ' ' + service.name;
debug('cloneRepo', workspace, cmd);
command(cmd, workspace, service.env, cb);
}
function checkoutBranch (service, cb) {
var cmd = 'git checkout ' + service.branch;
var dir = workspace + '/' + service.name;
debug('checkoutBranch', dir, cmd);
command(cmd, dir, service.env, cb);
}
function npmInstall (service, cb) {
var dir = workspace + '/' + service.name;
var cmd = 'npm install .';
debug('npmInstall', dir, cmd);
command(cmd, dir, service.env, cb);
}
function createDatabases (cb) {
var pg = require('pg');
var conString = util.format('postgres://%s:%s@%s/postgres',
system.env.POSTGRES_USERNAME,
system.env.POSTGRES_PASSWORD,
system.env.POSTGRES_HOST);
var client = new pg.Client(conString);
client.connect(function (err) {
if (err) return cb('Postgres connection error: ' + err);
async.map(system.services, createDatabase, function (err) {
client.end();
cb(err);
});
function createDatabase (service, cb) {
if (!service.database) return cb();
var q = 'CREATE DATABASE "' + service.database + '"';
client.query(q, function (err, result) {
if (err) {
// TODO - cheap and cheerful - do this instead:
// SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower('dbname');
if (err.toString().indexOf('already exists') === -1) {
return cb('Error creating database: ' + err);
}
}
return cb();
});
}
});
}
};