forked from jaruba/PowderWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.js
112 lines (83 loc) · 2 KB
/
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
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
104
105
106
107
108
109
110
111
112
var frontPort
var backPort
var maybePort
var getPort = require('get-port')
var child = require('child_process')
var opn = require('opn')
var masterKey
var serverLink
var checkOpen = function() {
if (serverLink && masterKey)
opn(serverLink + 'auth?token=' + masterKey)
}
var gotPorts = function() {
var isWin = process.platform === 'win32'
var newEnv = JSON.parse(JSON.stringify(process.env))
newEnv.PWFRONTPORT = frontPort
newEnv.PWBACKPORT = backPort
var frontProc = child.spawn('npm' + (isWin ? '.cmd' : ''), ['run', 'start-front'],
{
cwd: process.cwd(),
env: newEnv
})
frontProc.stdout.on('data', (data) => {
if (data) {
var sData = String(data).replace(/(\r\n\t|\n|\r\t)/gm,'')
console.log(sData)
if (sData.indexOf('http') > -1 && (sData.startsWith('Compiled successfully') || sData.startsWith('The app is running at'))) {
serverLink = sData.substr(sData.indexOf('http'))
checkOpen()
}
}
})
frontProc.stderr.on('data', (data) => {
console.log(String(data))
})
frontProc.on('exit', (code) => {
console.log('web server exit')
})
var backProc = child.spawn('npm' + (isWin ? '.cmd' : ''), ['run', 'start-back'],
{
cwd: process.cwd(),
env: newEnv
})
backProc.stdout.on('data', (data) => {
if (data) {
var sData = String(data).replace(/(\r\n\t|\n|\r\t)/gm,'')
console.log(sData)
if (sData.startsWith('master key: ')) {
masterKey = sData.replace('master key: ', '')
checkOpen()
}
}
})
backProc.stderr.on('data', (data) => {
console.log(String(data))
})
backProc.on('exit', (code) => {
console.log('back end exit')
})
}
var getPorts = function(cb) {
var fail = function() {
maybePort++
getPorts(cb)
}
getPort({ port: maybePort }).then(function(newPort) {
if (newPort == maybePort) {
cb(newPort)
} else
fail()
}).catch(function(e) {
fail()
})
}
maybePort = 11485
getPorts(function(newPort) {
frontPort = newPort
maybePort = newPort -1
getPorts(function(newPort) {
backPort = newPort
gotPorts()
})
})