|
| 1 | +const argv = require('minimist')(process.argv.slice(2)) |
| 2 | +const chalk = require('chalk') |
| 3 | +const express = require('express') |
| 4 | +const fs = require('fs') |
| 5 | +const https = require('https') |
| 6 | +const ipc = require('node-ipc') |
| 7 | +const ora = require('ora') |
| 8 | +const path = require('path') |
| 9 | + |
| 10 | +const config = require('../lib/config')() |
| 11 | + |
| 12 | +const port = 8443 |
| 13 | + |
| 14 | +module.exports = async () => { |
| 15 | + let client = argv['_'][1] || null |
| 16 | + let instance = argv['_'][2] || null |
| 17 | + let selected = null |
| 18 | + let remote |
| 19 | + let spinner |
| 20 | + |
| 21 | + const output = fn => { |
| 22 | + spinner.stop() |
| 23 | + fn() |
| 24 | + spinner.start() |
| 25 | + } |
| 26 | + |
| 27 | + // Get Client & Instance, or check for Default |
| 28 | + if (client && instance) { |
| 29 | + selected = config.get(client, instance) |
| 30 | + } else { |
| 31 | + const defaultConfig = config.get(client, instance, true) |
| 32 | + |
| 33 | + if (defaultConfig) { |
| 34 | + client = defaultConfig.client |
| 35 | + instance = defaultConfig.instance |
| 36 | + selected = defaultConfig.config |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (selected) { |
| 41 | + const sslKey = path.resolve(__dirname, '../remote/ssl/sfcc-cli-ca.pvk') |
| 42 | + const sslCrt = path.resolve(__dirname, '../remote/ssl/sfcc-cli-ca.cer') |
| 43 | + |
| 44 | + // Start Message Bus |
| 45 | + ipc.config.id = 'remote' |
| 46 | + ipc.config.retry = 1500 |
| 47 | + ipc.config.silent = true |
| 48 | + ipc.serve(() => { |
| 49 | + ipc.server.on('message', data => { |
| 50 | + if (remote && typeof remote.emit !== 'undefined') { |
| 51 | + remote.emit('message', data) |
| 52 | + } else { |
| 53 | + output(() => console.log(chalk.red.bold(`✖ Failed Remote Message`, data))) |
| 54 | + } |
| 55 | + }) |
| 56 | + ipc.server.on('watch', data => { |
| 57 | + if (remote && typeof remote.emit !== 'undefined') { |
| 58 | + remote.emit('watch', data) |
| 59 | + } else { |
| 60 | + output(() => console.log(chalk.red.bold(`✖ Failed Remote Watch`, data))) |
| 61 | + } |
| 62 | + }) |
| 63 | + ipc.server.on('log', data => { |
| 64 | + if (remote && typeof remote.emit !== 'undefined') { |
| 65 | + remote.emit('log', data) |
| 66 | + } else { |
| 67 | + output(() => console.log(chalk.red.bold(`✖ Failed Remote Log`, data))) |
| 68 | + } |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + ipc.server.start() |
| 73 | + |
| 74 | + // Check that SSL files exist |
| 75 | + if (!fs.existsSync(sslKey)) { |
| 76 | + console.log(chalk.red.bold(`✖ Missing ${sslKey}`)) |
| 77 | + console.log('See ./docs/cmd-remote.md for setup instructions.') |
| 78 | + process.exit() |
| 79 | + } |
| 80 | + |
| 81 | + if (!fs.existsSync(sslCrt)) { |
| 82 | + console.log(chalk.red.bold(`✖ Missing ${sslCrt}`)) |
| 83 | + console.log('See ./docs/cmd-remote.md for setup instructions.') |
| 84 | + process.exit() |
| 85 | + } |
| 86 | + |
| 87 | + // Start Socket Server |
| 88 | + const app = express() |
| 89 | + const server = https.createServer( |
| 90 | + { |
| 91 | + key: fs.readFileSync(sslKey), |
| 92 | + cert: fs.readFileSync(sslCrt) |
| 93 | + }, |
| 94 | + app |
| 95 | + ) |
| 96 | + |
| 97 | + const io = require('socket.io')(server) |
| 98 | + |
| 99 | + // Handle connections from remote script |
| 100 | + io.on('connection', socket => { |
| 101 | + remote = socket |
| 102 | + |
| 103 | + socket.on('message', message => { |
| 104 | + console.log('MESSAGE', message) |
| 105 | + }) |
| 106 | + |
| 107 | + socket.on('get-config', () => { |
| 108 | + io.emit('set-config', config.getAll()) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + server.listen(port, function() { |
| 113 | + spinner = ora( |
| 114 | + `${chalk.bold('REMOTE')} ${chalk.cyan.bold(client)} ${chalk.magenta.bold(instance)} [Ctrl-C to Cancel]\n` |
| 115 | + ).start() |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments