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);