-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.js
39 lines (34 loc) · 997 Bytes
/
start.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
/* eslint-disable no-console */
const axios = require('axios');
const program = require('commander');
const config = require('./config');
const isPortAvailable = port =>
axios
.get('/', { baseURL: `http://localhost:${port}` })
.then(res => false)
.catch(err => err.code === 'ECONNREFUSED');
/*
@usage
npm run start -- -s foo,...
*/
program
.version('0.1.0')
.option('-s, --services <services>', 'list of services to start')
.parse(process.argv);
let services = {};
if (program.services) program.services.split(',').forEach(service => (services[service] = true));
else {
services = {
foo: true,
bar: true,
};
}
const startServer = (name, port) =>
isPortAvailable(port).then((isAvailable) => {
if (isAvailable) {
require(`./services/${name}`).listen(port);
console.log(`Mock ${name} service started on port ${port}`);
}
});
if (services.foo) startServer('foo', config.foo.port);
if (services.bar) startServer('bar', config.bar.port);