Skip to content

Commit b043d1e

Browse files
More support for Browser Extension
1 parent 7b05d1f commit b043d1e

File tree

6 files changed

+100
-37
lines changed

6 files changed

+100
-37
lines changed

bin/cli.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,11 @@ const argv = yargs
112112
default: false
113113
}
114114
})
115-
.command('remote [client] [instance]', 'Remote Control Sandbox Storefront', {
116-
'live-reload': {
117-
describe: 'Enable Live Reload on Code Change',
118-
type: 'boolean',
119-
default: false
120-
}
121-
})
115+
.command('remote [client] [instance]', 'Enable Support for Browser Extension')
122116
.example('sfcc delete my-client sandbox', 'Delete my-client sandbox config')
123117
.example('sfcc watch my-client sandbox', 'Watch for my-client sandbox changes')
124118
.example('sfcc log -i customerror --latest', 'Watch Latest Custom Error Logs')
125-
.example('sfcc remote --live-reload', 'Live Reload Sandbox Storefront on Changes')
119+
.example('sfcc remote', 'Live Reload Sandbox Storefront on Changes')
126120
.demand(1)
127121
.help()
128122
.version().argv

commands/log.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const keys = require('lodash/keys')
66
const pickBy = require('lodash/pickBy')
77
const sortBy = require('lodash/sortBy')
88

9+
const config = require('../lib/config')()
910
const find = require('../lib/find')
1011
const search = require('../lib/search')
1112
const tail = require('../lib/tail')
12-
const config = require('../lib/config')()
1313

1414
module.exports = async options => {
1515
let client = argv['_'][1] || null
@@ -98,7 +98,9 @@ module.exports = async options => {
9898

9999
try {
100100
// Start log output
101-
options.search ? search(selected, groups, options) : tail(selected, logs, groups, options)
101+
options.search
102+
? search(selected, client, instance, groups, options)
103+
: tail(selected, client, instance, logs, groups, options)
102104
} catch (err) {
103105
console.log(err)
104106
}

commands/remote.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@ const express = require('express')
44
const fs = require('fs')
55
const https = require('https')
66
const ipc = require('node-ipc')
7+
const ora = require('ora')
78
const path = require('path')
89

910
const config = require('../lib/config')()
1011

1112
const port = 8443
1213

13-
module.exports = async options => {
14+
module.exports = async () => {
1415
let client = argv['_'][1] || null
1516
let instance = argv['_'][2] || null
1617
let selected = null
1718
let remote
19+
let spinner
20+
21+
const output = fn => {
22+
spinner.stop()
23+
fn()
24+
spinner.start()
25+
}
1826

1927
// Get Client & Instance, or check for Default
2028
if (client && instance) {
@@ -32,20 +40,32 @@ module.exports = async options => {
3240
if (selected) {
3341
const sslKey = path.resolve(__dirname, '../remote/ssl/sfcc-cli-ca.pvk')
3442
const sslCrt = path.resolve(__dirname, '../remote/ssl/sfcc-cli-ca.cer')
35-
const jsFile = path.resolve(__dirname, '../remote/sfcc-cli-remote.js')
3643

3744
// Start Message Bus
3845
ipc.config.id = 'remote'
3946
ipc.config.retry = 1500
4047
ipc.config.silent = true
4148
ipc.serve(() => {
42-
ipc.server.on('message', message => {
43-
console.log('MESSAGE', message)
44-
remote.emit('message', message)
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+
}
4555
})
4656
ipc.server.on('watch', data => {
47-
console.log('WATCH', data)
48-
remote.emit('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+
}
4969
})
5070
})
5171

@@ -76,19 +96,9 @@ module.exports = async options => {
7696

7797
const io = require('socket.io')(server)
7898

79-
// https://localhost:8443/sfcc-cli-remote.js
80-
app.get('/sfcc-cli-remote.js', (req, res) => {
81-
res.set('Content-Type', 'application/javascript')
82-
res.send(fs.readFileSync(jsFile))
83-
})
84-
8599
// Handle connections from remote script
86100
io.on('connection', socket => {
87101
remote = socket
88-
// Check if Live Reload flag was enabled
89-
if (options.liveReload) {
90-
remote.emit('message', 'Live Reload Enabled')
91-
}
92102

93103
socket.on('message', message => {
94104
console.log('MESSAGE', message)
@@ -100,12 +110,9 @@ module.exports = async options => {
100110
})
101111

102112
server.listen(port, function() {
103-
console.log(`\n${chalk.bold('IMPORTANT')}: Make sure your sandbox has the following script tag:`)
104-
console.log('<script src="https://localhost:8443/sfcc-cli-remote.js" id="sfcc-cli-remote"></script>\n')
105-
106-
// ora(
107-
// `${chalk.bold('REMOTE')} ${chalk.cyan.bold(client)} ${chalk.magenta.bold(instance)} [Ctrl-C to Cancel]\n`
108-
// ).start()
113+
spinner = ora(
114+
`${chalk.bold('REMOTE')} ${chalk.cyan.bold(client)} ${chalk.magenta.bold(instance)} [Ctrl-C to Cancel]\n`
115+
).start()
109116
})
110117
}
111118
}

commands/watch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1+
const {exec} = require('child_process')
12
const argv = require('minimist')(process.argv.slice(2))
23
const chalk = require('chalk')
34
const chokidar = require('chokidar')
45
const ipc = require('node-ipc')
56
const ora = require('ora')
67
const path = require('path')
7-
const {exec} = require('child_process')
88

9+
const config = require('../lib/config')()
910
const logger = require('../lib/logger')()
1011
const notify = require('../lib/notify')()
11-
const config = require('../lib/config')()
1212
const upload = require('../lib/upload')
1313

1414
module.exports = options => {

lib/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const truncate = require('lodash/truncate')
66

77
const read = require('./read')
88

9-
module.exports = async (selected, groups, options) => {
9+
module.exports = async (selected, client, instance, groups, options) => {
1010
const including = options.include && options.include.length > 0 ? ` '${options.include.join(', ')}'` : ''
1111
const excluding = options.exclude && options.exclude.length > 0 ? ` '${options.exclude.join(', ')}'` : ''
1212
const filters = options.filter && options.filter.length > 0 ? ` containing '${options.filter}'` : ''

lib/tail.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const chalk = require('chalk')
22
const compact = require('lodash/compact')
3+
const ipc = require('node-ipc')
34
const map = require('lodash/map')
45
const ora = require('ora')
56
const truncate = require('lodash/truncate')
67

78
const read = require('./read')
89

9-
module.exports = async (selected, logs, groups, options) => {
10+
module.exports = async (selected, client, instance, logs, groups, options) => {
1011
const including = options.include && options.include.length > 0 ? ` '${options.include.join(', ')}'` : ''
1112
const excluding = options.exclude && options.exclude.length > 0 ? ` excluding '${options.exclude.join(', ')}'` : ''
1213
const filters = options.filter && options.filter.length > 0 ? ` containing '${options.filter}'` : ''
@@ -18,6 +19,30 @@ module.exports = async (selected, logs, groups, options) => {
1819
spinner.start()
1920
}
2021

22+
// Connect to Remote Message Bus
23+
let remote = null
24+
25+
ipc.config.id = 'log'
26+
ipc.config.retry = 1500
27+
ipc.config.silent = true
28+
29+
ipc.connectTo('remote', () => {
30+
ipc.of.remote.on('connect', () => {
31+
remote = ipc.of.remote
32+
})
33+
ipc.of.remote.on('disconnect', () => {
34+
remote = null
35+
})
36+
})
37+
38+
const sendLog = data => {
39+
if (remote && typeof remote.emit !== 'undefined') {
40+
remote.emit('log', data)
41+
} else {
42+
output(() => console.log('REMOTE NOT CONNECTED'))
43+
}
44+
}
45+
2146
const tail = async () => {
2247
const promises = map(groups, async (files, name) => {
2348
const displayname = files[0].displayname
@@ -34,6 +59,13 @@ module.exports = async (selected, logs, groups, options) => {
3459
name
3560
}
3661
} catch (err) {
62+
sendLog({
63+
type: 'error',
64+
client: client,
65+
instance: instance,
66+
message: err.toString(),
67+
timestamp: new Date().toString()
68+
})
3769
output(() => console.log(err))
3870
}
3971
})
@@ -46,9 +78,29 @@ module.exports = async (selected, logs, groups, options) => {
4678
for (let line of lines) {
4779
if (line && !logs[name].includes(line)) {
4880
logs[name].push(line)
81+
4982
if (options.filter && !new RegExp(options.filter).test(line)) {
5083
continue
5184
}
85+
86+
let messageClass = 'log'
87+
88+
if (name.includes('error')) {
89+
messageClass = 'error'
90+
} else if (name.includes('warn')) {
91+
messageClass = 'warn'
92+
} else if (name.includes('info')) {
93+
messageClass = 'info'
94+
}
95+
96+
let message = `<strong class="${messageClass}">${name}</strong> ${line}`
97+
let parseDate = line.match(/\[(.+)\sGMT\]/)
98+
let timestamp = new Date()
99+
100+
if (parseDate && parseDate.length > 1) {
101+
timestamp = new Date(Date.parse(parseDate[1] + 'Z'))
102+
}
103+
52104
if (options.truncate > 0) {
53105
line = truncate(line.trim(), {
54106
length: options.truncate,
@@ -67,6 +119,14 @@ module.exports = async (selected, logs, groups, options) => {
67119
return chalk.magenta(`[${date.toLocaleDateString()} ${date.toLocaleTimeString()}]`)
68120
})
69121

122+
sendLog({
123+
type: 'log',
124+
client: client,
125+
instance: instance,
126+
message: message,
127+
timestamp: timestamp.toString()
128+
})
129+
70130
output(() => console.log(`${chalk.cyan.bold(name)} ${line}`))
71131
}
72132
}

0 commit comments

Comments
 (0)