|
29 | 29 | const program = require('commander'), |
30 | 30 | express = require("express"), |
31 | 31 | server = express(), |
32 | | - fkill = require('fkill'), |
| 32 | + fkill = require('fkill'), |
33 | 33 | http = require('http').Server(server), |
34 | 34 | exec = require('child_process').exec, |
35 | 35 | argz = process.argv.slice(2), |
36 | 36 | Profile = require('../utils/Profile'); |
37 | 37 |
|
38 | 38 | let profileId = process.env.__LPACK_PROFILE__ || "default"; |
39 | 39 |
|
40 | | -if ( argz[0] && /^\:.*$/.test(argz[0]) ) |
41 | | - profileId = argz.shift().replace(/^\:(.*)$/, '$1'); |
| 40 | +if ( argz[ 0 ] && /^\:.*$/.test(argz[ 0 ]) ) |
| 41 | + profileId = argz.shift().replace(/^\:(.*)$/, '$1'); |
42 | 42 |
|
43 | 43 | program |
44 | | - .option('-l, --local', 'Limit Build control web api to localhost') |
45 | | - .option('-p, --port [port=9090]', 'Build control') |
46 | | - .parse(process.argv); |
| 44 | + .option('-l, --local', 'Limit Build control web api to localhost') |
| 45 | + .option('-p, --port [port=9090]', 'Build control') |
| 46 | + .parse(process.argv); |
47 | 47 |
|
48 | | -let port = program.port || 9090, |
| 48 | +let port = program.port === true ? 9090 : program.port, |
49 | 49 | pDir = program.source || process.cwd(); |
50 | 50 |
|
51 | 51 | let profile = new Profile(profileId); |
52 | 52 |
|
53 | 53 | profile.start(); |
54 | 54 | profile.onComplete(e => process.exit()); |
55 | | - |
56 | | - |
57 | | -server.use(express.json()); // to support JSON-encoded bodies |
58 | | -server.use(express.urlencoded()); // to support URL-encoded bodies |
59 | | - |
60 | | -server.use( |
61 | | - "/status", |
62 | | - ( req, res ) => { |
63 | | - res.header("Access-Control-Allow-Origin", "*"); |
64 | | - res.json({ status: profile.getStatus() }) |
65 | | - } |
66 | | -); |
67 | | - |
68 | | -server.use( |
69 | | - "/restart", |
70 | | - ( req, res ) => { |
71 | | - res.header("Access-Control-Allow-Origin", "*"); |
72 | | - profile.start(); |
73 | | - |
74 | | - res.json({ success: true }) |
75 | | - } |
76 | | -); |
77 | | -server.use( |
78 | | - "/forceKill8080", |
79 | | - ( req, res ) => { |
80 | | - res.header("Access-Control-Allow-Origin", "*"); |
81 | | - fkill(":8080", { tree: true, force: true, silent: true }) |
82 | | - |
83 | | - res.json({ success: true }) |
84 | | - } |
85 | | -); |
86 | | - |
87 | | -server.use( |
88 | | - "/switch", |
89 | | - ( req, res ) => { |
90 | | - res.header("Access-Control-Allow-Origin", "*"); |
91 | | - profileId = req.query.to || "prod"; |
92 | | - profile.stop().then( |
93 | | - e => { |
94 | | - profile = new Profile(profileId); |
95 | | - profile.start(); |
96 | | - res.json({ success: true, profileId }) |
97 | | - } |
98 | | - ); |
99 | | - |
100 | | - |
101 | | - } |
102 | | -); |
103 | | - |
104 | | -server.use( |
105 | | - "/kill", |
106 | | - ( req, res ) => { |
107 | | - res.header("Access-Control-Allow-Origin", "*"); |
108 | | - res.json({ success: true }); |
109 | | - |
110 | | - profile.stop().then(e => process.exit()); |
111 | | - } |
112 | | -); |
113 | | - |
114 | | -let server_instance = http.listen(parseInt(port), function () { |
115 | | - console.info('Build manager running on ', server_instance.address(), server_instance.address().port) |
116 | | -}); |
117 | | - |
118 | 55 | process.on('SIGINT', e => profile.stop()); // catch ctrl-c |
119 | 56 | process.on('SIGTERM', e => profile.stop()); // catch kill |
| 57 | + |
| 58 | +if ( port ) { |
| 59 | + server.use(express.json()); // to support JSON-encoded bodies |
| 60 | + server.use(express.urlencoded()); // to support URL-encoded bodies |
| 61 | + |
| 62 | + server.use( |
| 63 | + "/status", |
| 64 | + ( req, res ) => { |
| 65 | + res.header("Access-Control-Allow-Origin", "*"); |
| 66 | + res.json({ status: profile.getStatus() }) |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + server.use( |
| 71 | + "/restart", |
| 72 | + ( req, res ) => { |
| 73 | + res.header("Access-Control-Allow-Origin", "*"); |
| 74 | + profile.start(); |
| 75 | + |
| 76 | + res.json({ success: true }) |
| 77 | + } |
| 78 | + ); |
| 79 | + |
| 80 | + server.use( |
| 81 | + "/switch", |
| 82 | + ( req, res ) => { |
| 83 | + res.header("Access-Control-Allow-Origin", "*"); |
| 84 | + profileId = req.query.to || "prod"; |
| 85 | + profile.stop().then( |
| 86 | + e => { |
| 87 | + profile = new Profile(profileId); |
| 88 | + profile.start(); |
| 89 | + res.json({ success: true, profileId }) |
| 90 | + } |
| 91 | + ); |
| 92 | + |
| 93 | + |
| 94 | + } |
| 95 | + ); |
| 96 | + |
| 97 | + server.use( |
| 98 | + "/kill", |
| 99 | + ( req, res ) => { |
| 100 | + res.header("Access-Control-Allow-Origin", "*"); |
| 101 | + res.json({ success: true }); |
| 102 | + |
| 103 | + profile.stop().then(e => process.exit()); |
| 104 | + } |
| 105 | + ); |
| 106 | + |
| 107 | + let server_instance = http.listen(parseInt(port), function () { |
| 108 | + console.info('Build manager running on ', server_instance.address(), server_instance.address().port) |
| 109 | + }); |
| 110 | +} |
0 commit comments