From e2f39580dfe2c9c961f9d93511ef9b851623edf6 Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford <7019101+cwillisf@users.noreply.github.com> Date: Thu, 3 Sep 2020 17:01:22 -0700 Subject: [PATCH] make command line args available to render process --- src/main/argv.js | 23 +++++++++++++++++++++++ src/main/index.js | 3 +++ src/renderer/app.jsx | 10 ++++++++++ 3 files changed, 36 insertions(+) create mode 100644 src/main/argv.js diff --git a/src/main/argv.js b/src/main/argv.js new file mode 100644 index 0000000..25be323 --- /dev/null +++ b/src/main/argv.js @@ -0,0 +1,23 @@ +import minimist from 'minimist'; + +// inspired by yargs' process-argv +export const isElectronApp = () => !!process.versions.electron; +export const isElectronBundledApp = () => isElectronApp() && !process.defaultApp; + +export const parseAndTrimArgs = argv => { + // bundled Electron app: ignore 1 from "my-app arg1 arg2" + // unbundled Electron app: ignore 2 from "electron main/index.js arg1 arg2" + // node.js app: ignore 2 from "node src/index.js arg1 arg2" + const ignoreCount = isElectronBundledApp() ? 1 : 2; + + const parsed = minimist(argv); + + // ignore arguments AFTER parsing to handle cases like "electron --inspect=42 my.js arg1 arg2" + parsed._ = parsed._.slice(ignoreCount); + + return parsed; +}; + +const argv = parseAndTrimArgs(process.argv); + +export default argv; diff --git a/src/main/index.js b/src/main/index.js index 229d4a4..c806962 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -3,6 +3,7 @@ import fs from 'fs-extra'; import path from 'path'; import {URL} from 'url'; +import argv from './argv'; import {getFilterForExtension} from './FileFilters'; import telemetry from './ScratchDesktopTelemetry'; import MacOSMenu from './MacOSMenu'; @@ -373,3 +374,5 @@ app.on('ready', () => { ipcMain.on('open-about-window', () => { _windows.about.show(); }); + +ipcMain.handle('get-argv', () => argv); diff --git a/src/renderer/app.jsx b/src/renderer/app.jsx index 49ff284..e3c1169 100644 --- a/src/renderer/app.jsx +++ b/src/renderer/app.jsx @@ -6,6 +6,7 @@ import {compose} from 'redux'; import GUI, {AppStateHOC} from 'scratch-gui'; import ElectronStorageHelper from '../common/ElectronStorageHelper'; +import log from '../common/log'; import styles from './app.css'; @@ -99,4 +100,13 @@ const WrappedGui = compose( AppStateHOC )(GUI); +ipcRenderer.invoke('get-argv').then( + argv => { + log.log(`argv._ = ${argv._}`); + }, + err => { + log.warn('Failed to retrieve argv', err); + } +); + ReactDOM.render(, appTarget);