Skip to content

Commit da4033e

Browse files
committed
fix(serve): remove junk terminal output from electron, closes #60
1 parent d2ecc38 commit da4033e

File tree

6 files changed

+145
-8
lines changed

6 files changed

+145
-8
lines changed

__tests__/commands.spec.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@ const mockYargsCommand = jest.fn(() => ({ parse: mockYargsParse }))
1616
jest.mock('yargs', () => ({ command: mockYargsCommand }))
1717
jest.mock('@vue/cli-service/lib/commands/build')
1818
jest.mock('fs-extra')
19+
jest.mock('../lib/removeJunk.js', () => jest.fn(() => 'removeJunk'))
1920
jest.mock('electron-builder')
2021
const mockInstallAppDeps = jest.fn()
2122
jest.mock('electron-builder/out/cli/install-app-deps.js', () => ({
2223
installAppDeps: mockInstallAppDeps
2324
}))
2425
jest.mock('../lib/webpackConfig.js')
26+
const mockPipe = jest.fn()
2527
const mockExeca = {
2628
on: jest.fn(),
2729
removeAllListeners: jest.fn(),
28-
kill: jest.fn()
30+
kill: jest.fn(),
31+
stdout: {
32+
pipe: jest.fn(() => ({ pipe: mockPipe }))
33+
},
34+
stderr: {
35+
pipe: jest.fn(() => ({ pipe: mockPipe }))
36+
}
2937
}
3038
jest.mock('execa', () => jest.fn(() => mockExeca))
3139
jest.mock('@vue/cli-service/lib/commands/serve', () => ({
@@ -419,6 +427,30 @@ describe('serve:electron', () => {
419427
// Electron was re-launched
420428
expect(execa).toHaveBeenCalledTimes(3)
421429
})
430+
431+
test('Junk output is stripped from electron child process', async () => {
432+
await runCommand('serve:electron')
433+
434+
// Junk is removed
435+
expect(mockExeca.stderr.pipe).toBeCalledWith('removeJunk')
436+
expect(mockExeca.stdout.pipe).toBeCalledWith('removeJunk')
437+
// Output is piped to console
438+
expect(mockPipe).toBeCalledWith(process.stderr)
439+
expect(mockPipe).toBeCalledWith(process.stdout)
440+
})
441+
442+
test('Junk output is not stripped from electron child process if removeElectronJunk is set to false', async () => {
443+
await runCommand('serve:electron', {
444+
pluginOptions: { electronBuilder: { removeElectronJunk: false } }
445+
})
446+
447+
// Junk is not removed
448+
expect(mockPipe).not.toBeCalled()
449+
expect(mockPipe).not.toBeCalled()
450+
// Output is still piped to console
451+
expect(mockExeca.stderr.pipe).toBeCalledWith(process.stderr)
452+
expect(mockExeca.stdout.pipe).toBeCalledWith(process.stdout)
453+
})
422454
})
423455

424456
describe('Custom webpack chain', () => {

docs/guide/configuration.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,37 @@ module.exports = {
106106

107107
:::tip Tip <Badge text="1.0.0-rc.1+" type="info"/>
108108
If you decide to add the `@vue/typescript` plugin to your app later on, make sure to re-invoke the generator of VCP-Electron-Builder with `vue invoke electron-builder`. This will automatically insert missing type definitions to your `background.ts` file.
109-
:::
109+
:::
110+
111+
## Electron's Junk Terminal Output
112+
113+
Electron will sometimes produce a bunch of junk output like so:
114+
115+
```
116+
2018-08-10 22:52:14.068 Electron[90710:4891777] *** WARNING: Textured window <AtomNSWindow: 0x7fd508e75020> is getting an implicitly transparent titlebar. This will break when linking against newer SDKs. Use NSWindow's -titlebarAppearsTransparent=YES instead.
117+
2018-08-10 22:52:37.919 Electron Helper[90714:4892173] Couldn't set selectedTextBackgroundColor from default ()
118+
[90789:0810/225757.360355:ERROR:CONSOLE(0)] "Failed to load https://chrome-devtools-frontend.appspot.com/serve_file/@7accc8730b0f99b5e7c0702ea89d1fa7c17bfe33/product_registry_impl/product_registry_impl_module.js: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'chrome-devtools://devtools' is therefore not allowed access. The response had HTTP status code 404.", source: chrome-devtools://devtools/bundled/inspector.html?remoteBase=https://chrome-devtools-frontend.appspot.com/serve_file/@7accc8730b0f99b5e7c0702ea89d1fa7c17bfe33/&can_dock=true&toolbarColor=rgba(223,223,223,1)&textColor=rgba(0,0,0,1)&experiments=true (0)
119+
[90789:0810/225757.360445:ERROR:CONSOLE(22)] "Empty response arrived for script 'https://chrome-devtools-frontend.appspot.com/serve_file/@7accc8730b0f99b5e7c0702ea89d1fa7c17bfe33/product_registry_impl/product_registry_impl_module.js'", source: chrome-devtools://devtools/bundled/inspector.js (22)
120+
[90789:0810/225757.361236:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)
121+
[90789:0810/225757.361293:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)
122+
App logging
123+
[90789:0810/225802.898597:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)
124+
[90789:0810/225803.872738:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)
125+
[90789:0810/225803.898240:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)
126+
[90789:0810/225803.898297:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)
127+
...
128+
```
129+
130+
VCP Electron Builder will automatically remove this for you (powered by [run-electron](https://github.com/sindresorhus/run-electron)). If you don't want this removed, set `electronJunkOutput` to `false` in plugin options:
131+
132+
```javascript
133+
// vue.config.js
134+
135+
module.exports = {
136+
pluginOptions: {
137+
electronBuilder: {
138+
removeElectronJunk: false // True by default
139+
}
140+
}
141+
}
142+
```

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,28 @@ module.exports = (api, options) => {
315315
[`${outputDir}/background.js`],
316316
{
317317
cwd: api.resolve('.'),
318-
stdio: 'inherit',
319318
env: {
320319
...process.env,
321320
// Disable electron security warnings
322321
ELECTRON_DISABLE_SECURITY_WARNINGS: true
323322
}
324323
}
325324
)
325+
326+
if (pluginOptions.removeElectronJunk === false) {
327+
// Pipe output to console
328+
child.stdout.pipe(process.stdout)
329+
child.stderr.pipe(process.stderr)
330+
} else {
331+
// Remove junk terminal output (#60)
332+
child.stdout
333+
.pipe(require('./lib/removeJunk.js')())
334+
.pipe(process.stdout)
335+
child.stderr
336+
.pipe(require('./lib/removeJunk.js')())
337+
.pipe(process.stderr)
338+
}
339+
326340
child.on('exit', () => {
327341
// Exit when electron is closed
328342
process.exit(0)

lib/removeJunk.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const split = require('split2')
2+
const filter = require('through2-filter')
3+
const map = require('through2-map')
4+
const pumpify = require('pumpify')
5+
6+
process.env.FORCE_COLOR = '1'
7+
8+
module.exports = () => {
9+
const filterStream = filter({ wantStrings: true }, chunk => {
10+
// Example: 2018-08-10 22:48:42.866 Electron[90311:4883863] *** WARNING: Textured window <AtomNSWindow: 0x7fb75f68a770>
11+
if (
12+
/\d+-\d+-\d+ \d+:\d+:\d+\.\d+ Electron(?: Helper)?\[\d+:\d+] /.test(chunk)
13+
) {
14+
return false
15+
}
16+
17+
// Example: [90789:0810/225804.894349:ERROR:CONSOLE(105)] "Uncaught (in promise) Error: Could not instantiate: ProductRegistryImpl.Registry", source: chrome-devtools://devtools/bundled/inspector.js (105)
18+
if (/\[\d+:\d+\/|\d+\.\d+:ERROR:CONSOLE\(\d+\)\]/.test(chunk)) {
19+
return false
20+
}
21+
22+
return true
23+
})
24+
25+
return pumpify(split(), filterStream, map(chunk => `${chunk}\n`))
26+
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
"fs-extra": "^7.0.0",
3434
"lodash.merge": "^4.6.1",
3535
"portfinder": "^1.0.16",
36+
"pumpify": "^1.5.1",
3637
"spectron": "^3.8.0",
38+
"split2": "^3.0.0",
39+
"through2-filter": "^3.0.0",
40+
"through2-map": "^3.0.0",
3741
"uglifyjs-webpack-plugin": "^1.3.0",
3842
"unzip-crx": "^0.2.0",
3943
"webpack": "^4.16.5",

yarn.lock

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9366,7 +9366,7 @@ pump@^3.0.0:
93669366
end-of-stream "^1.1.0"
93679367
once "^1.3.1"
93689368

9369-
pumpify@^1.3.3:
9369+
pumpify@^1.3.3, pumpify@^1.5.1:
93709370
version "1.5.1"
93719371
resolved "https://registry.yarnpkg.com/pumpify/-/pumpify-1.5.1.tgz#36513be246ab27570b1a374a5ce278bfd74370ce"
93729372
dependencies:
@@ -9583,6 +9583,14 @@ [email protected], readable-stream@~1.1.9:
95839583
isarray "0.0.1"
95849584
string_decoder "~0.10.x"
95859585

9586+
readable-stream@^3.0.0:
9587+
version "3.0.2"
9588+
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.0.2.tgz#ca0f4f8e426bcabd772df73096b8d3f6722b31ff"
9589+
dependencies:
9590+
inherits "^2.0.3"
9591+
string_decoder "^1.1.1"
9592+
util-deprecate "^1.0.1"
9593+
95869594
readable-stream@~2.0.6:
95879595
version "2.0.6"
95889596
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
@@ -10456,6 +10464,12 @@ split-string@^3.0.1, split-string@^3.0.2:
1045610464
dependencies:
1045710465
extend-shallow "^3.0.0"
1045810466

10467+
split2@^3.0.0:
10468+
version "3.0.0"
10469+
resolved "https://registry.yarnpkg.com/split2/-/split2-3.0.0.tgz#55057cd560687a7ef6464471597404577ff1735d"
10470+
dependencies:
10471+
readable-stream "^3.0.0"
10472+
1045910473
1046010474
version "0.3.3"
1046110475
resolved "https://registry.yarnpkg.com/split/-/split-0.3.3.tgz#cd0eea5e63a211dfff7eb0f091c4133e2d0dd28f"
@@ -10642,7 +10656,7 @@ string_decoder@^0.10.25, string_decoder@~0.10.x:
1064210656
version "0.10.31"
1064310657
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"
1064410658

10645-
string_decoder@^1.0.0, string_decoder@~1.1.1:
10659+
string_decoder@^1.0.0, string_decoder@^1.1.1, string_decoder@~1.1.1:
1064610660
version "1.1.1"
1064710661
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8"
1064810662
dependencies:
@@ -10938,7 +10952,21 @@ [email protected]:
1093810952
version "0.0.2"
1093910953
resolved "https://registry.yarnpkg.com/throttleit/-/throttleit-0.0.2.tgz#cfedf88e60c00dd9697b61fdd2a8343a9b680eaf"
1094010954

10941-
through2@^2.0.0:
10955+
through2-filter@^3.0.0:
10956+
version "3.0.0"
10957+
resolved "https://registry.yarnpkg.com/through2-filter/-/through2-filter-3.0.0.tgz#700e786df2367c2c88cd8aa5be4cf9c1e7831254"
10958+
dependencies:
10959+
through2 "~2.0.0"
10960+
xtend "~4.0.0"
10961+
10962+
through2-map@^3.0.0:
10963+
version "3.0.0"
10964+
resolved "https://registry.yarnpkg.com/through2-map/-/through2-map-3.0.0.tgz#a6c3026ce63b4898a997d540506b66ffd970f271"
10965+
dependencies:
10966+
through2 "~2.0.0"
10967+
xtend "^4.0.0"
10968+
10969+
through2@^2.0.0, through2@~2.0.0:
1094210970
version "2.0.3"
1094310971
resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.3.tgz#0004569b37c7c74ba39c43f3ced78d1ad94140be"
1094410972
dependencies:
@@ -11431,7 +11459,7 @@ utf8-byte-length@^1.0.1:
1143111459
version "1.0.4"
1143211460
resolved "https://registry.yarnpkg.com/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz#f45f150c4c66eee968186505ab93fcbb8ad6bf61"
1143311461

11434-
util-deprecate@~1.0.1:
11462+
util-deprecate@^1.0.1, util-deprecate@~1.0.1:
1143511463
version "1.0.2"
1143611464
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
1143711465

@@ -12271,7 +12299,7 @@ [email protected]:
1227112299
version "4.0.0"
1227212300
resolved "https://registry.yarnpkg.com/xregexp/-/xregexp-4.0.0.tgz#e698189de49dd2a18cc5687b05e17c8e43943020"
1227312301

12274-
xtend@^4.0.0, xtend@~4.0.1:
12302+
xtend@^4.0.0, xtend@~4.0.0, xtend@~4.0.1:
1227512303
version "4.0.1"
1227612304
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"
1227712305

0 commit comments

Comments
 (0)