-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrun.js
91 lines (71 loc) · 2.24 KB
/
run.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
var _ = require('lodash');
var async = require('async');
var fs = require('fs');
var path = require('path');
var http = require('http');
var pm2 = require('pm2');
module.exports = {
name:'run',
description:'Runs all of the microservices (or subset based on regex pattern) using pm2',
example:'bosco run <pattern>',
cmd:cmd
}
function cmd(bosco, args) {
var repoPattern = args.shift() || '.*';
var repoRegex = new RegExp(repoPattern);
var repos = bosco.config.get('github:repos');
var runningServices = {};
// Connect or launch PM2
pm2.connect(function(err) {
var startRunnableServices = function(running) {
async.map(repos, function(repo, next) {
var pkg, basePath, repoPath = bosco.getRepoPath(repo), packageJson = [repoPath,"package.json"].join("/");
if(repo.match(repoRegex) && bosco.exists(packageJson)) {
pkg = require(packageJson);
if(_.contains(running, repos)) {
bosco.warn(repo + " already running, use 'bosco stop " + repo + "'");
return next();
}
if(pkg.scripts && pkg.scripts.start) {
runService(repo, pkg.scripts, repoPath, next);
} else {
next();
}
} else {
next();
}
}, function(err) {
process.exit(0);
});
}
var getRunningServices = function(next) {
pm2.list(function(err, list) {
next(err, _.pluck(list,'name'));
});
}
var runService = function(repo, scripts, repoPath, next) {
bosco.log("Starting " + repo + " @ " + repoPath + " via " + scripts.start.blue);
run(repo, scripts, repoPath, next);
}
var run = function(repo, scripts, repoPath, next) {
// Remove node from the start script as not req'd for PM2
var start = scripts.start, startArr;
if(start.split(" ")[0] == "node") {
startArr = start.split(" ");
startArr.shift();
start = startArr.join(" ");
}
// If no extension, assume a JS file
var ext = path.extname(start);
if(!path.extname(start)) {
ext = ".js";
start = start + ".js";
}
pm2.start(start, { name: repo, cwd: repoPath, watch: true, executeCommand: ext == ".js" ? false : true }, next);
}
bosco.log("Run each mircoservice " + args);
getRunningServices(function(err, running) {
startRunnableServices(running);
});
});
}