Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit a185552

Browse files
committed
Extract running processes to a separated module.
Signed-off-by: David Calavera <[email protected]>
1 parent b73149d commit a185552

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/commands/dev/index.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { flags } = require('@oclif/command')
2-
const { spawn } = require('child_process')
32
const http = require('http')
43
const httpProxy = require('http-proxy')
54
const waitPort = require('wait-port')
@@ -10,6 +9,7 @@ const openBrowser = require('../../utils/openBrowser')
109
const Command = require('@netlify/cli-utils')
1110
const { getAddons } = require('netlify/src/addons')
1211
const { createTunnel, connectTunnel } = require('../../live-tunnel')
12+
const { runProcess } = require('../../run-process')
1313

1414
function isFunction(settings, req) {
1515
return settings.functionsPort && req.url.match(/^\/.netlify\/functions\/.+/)
@@ -95,19 +95,7 @@ function startDevServer(settings, log, error) {
9595
return
9696
}
9797

98-
const ps = spawn(settings.cmd, settings.args, { env: settings.env })
99-
100-
ps.stdout.on('data', data => {
101-
log(`${data}`.replace(settings.urlRegexp, `$1$2${settings.port}$3`))
102-
})
103-
104-
ps.stderr.on('data', data => {
105-
log(`Error reading data: ${data}`)
106-
})
107-
108-
ps.on('close', code => process.exit(code))
109-
ps.on('SIGINT', () => process.exit())
110-
ps.on('SIGTERM', () => process.exit())
98+
runProcess(settings, log, error)
11199
}
112100

113101
class DevCommand extends Command {

src/live-tunnel.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const {spawn} = require('child_process')
21
const fetch = require('node-fetch')
32
const fs = require('fs')
43
const os = require('os')
54
const path = require('path')
6-
const {fetchLatest} = require('gh-release-fetch')
5+
const { fetchLatest } = require('gh-release-fetch')
6+
const { runProcess } = require('./run-process')
77

88
async function createTunnel(siteId, netlifyApiToken) {
99
await installTunnelClient()
@@ -32,19 +32,12 @@ async function createTunnel(siteId, netlifyApiToken) {
3232
async function connectTunnel(session, netlifyApiToken, localPort, log, error) {
3333
const execPath = path.join(os.homedir(), '.netlify', 'tunnel', 'bin', 'live-tunnel-client')
3434

35-
const ps = spawn(execPath, ['connect', '-s', session.id, '-t', netlifyApiToken, '-l', localPort]);
36-
37-
ps.stdout.on('data', data => {
38-
log(`INFO: ${data}`)
39-
})
40-
41-
ps.stderr.on('data', data => {
42-
error(`ERROR: ${data}`)
43-
})
35+
const proc = {
36+
cmd: execPath,
37+
args: ['connect', '-s', session.id, '-t', netlifyApiToken, '-l', localPort]
38+
}
4439

45-
ps.on('close', code => process.exit(code))
46-
ps.on('SIGINT', () => process.exit())
47-
ps.on('SIGTERM', () => process.exit())
40+
runProcess(cmd, log, error)
4841
}
4942

5043
async function installTunnelClient() {

src/run-process.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const {spawn} = require('child_process')
2+
3+
module.exports.runProcess = ({ cmd, args, env = {} }, log, error) => {
4+
const ps = spawn(cmd, args, env);
5+
6+
ps.stdout.on('data', data => {
7+
log(`INFO: ${data}`)
8+
})
9+
10+
ps.stderr.on('data', data => {
11+
error(`ERROR: ${data}`)
12+
})
13+
14+
ps.on('close', code => process.exit(code))
15+
ps.on('SIGINT', () => process.exit())
16+
ps.on('SIGTERM', () => process.exit())
17+
}

0 commit comments

Comments
 (0)