-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.js
77 lines (72 loc) · 2.21 KB
/
launch.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
const findFreePort = require('find-free-port')
const path = require('path')
module.exports = { startSonarAndRemix }
if (process.argv[2] === 'start') {
startSonarAndRemix().catch(err => {
console.error(err)
process.exit(1)
})
}
if (process.argv[2] === 'dev') {
startSonarAndRemix({ dev: true }).catch(err => {
console.error(err)
process.exit(1)
})
}
async function startSonarAndRemix ({ dev } = {}) {
process.chdir(__dirname)
let remixURL
const closing = []
try {
const [sonarServer, sonarOpts] = await startSonar()
closing.push(() => sonarServer.close())
remixURL = await startRemix({ sonar: sonarOpts, dev })
} catch (err) {
console.error(`initializing failed: ${err}`)
}
return { remixURL, close }
function close () {
for (const close of closing) close()
}
}
async function startRemix (opts = {}) {
if (opts.sonar?.url) process.env.SONAR_URL = opts.sonar.url
if (opts.sonar?.collection) process.env.SONAR_COLLECTION = opts.sonar.collection
const port = opts.port || process.env.PORT || (await findFreePort(3000))[0]
if (!opts.dev) {
require('@remix-run/serve/env.js')
const { createApp } = require('@remix-run/serve/index.js')
const hostname = 'localhost'
const buildPath = path.resolve(process.cwd(), 'build')
const url = `http://${hostname}:${port}`
await new Promise((resolve, reject) => {
createApp(buildPath).listen(port, hostname, () => {
console.log(`Remix App Server started at ${url}`)
resolve()
})
})
return url
} else {
const commands = require('@remix-run/dev/cli/commands')
commands.dev(__dirname)
}
}
async function startSonar () {
const Server = require('@arsonar/server/server.js')
const port = (await findFreePort(15000))[0]
const dataDir = process.env.SONAR_STORAGE || path.resolve(path.join(__dirname, '.data'))
const opts = {
port,
hostname: 'localhost',
storage: dataDir,
disableAuthentication: true
}
const server = new Server(opts)
await server.start()
const sonarOpts = {
url: `http://localhost:${port}/api/v1/default`,
collection: 'default'
}
console.log(`Sonar running on ${sonarOpts.url} with storage at ${dataDir}`)
return [server, sonarOpts]
}