make command line args available to render process

This commit is contained in:
Christopher Willis-Ford 2020-09-03 17:01:22 -07:00
parent 25243b0542
commit e2f39580df
3 changed files with 36 additions and 0 deletions

23
src/main/argv.js Normal file
View file

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

View file

@ -3,6 +3,7 @@ import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import {URL} from 'url'; import {URL} from 'url';
import argv from './argv';
import {getFilterForExtension} from './FileFilters'; import {getFilterForExtension} from './FileFilters';
import telemetry from './ScratchDesktopTelemetry'; import telemetry from './ScratchDesktopTelemetry';
import MacOSMenu from './MacOSMenu'; import MacOSMenu from './MacOSMenu';
@ -373,3 +374,5 @@ app.on('ready', () => {
ipcMain.on('open-about-window', () => { ipcMain.on('open-about-window', () => {
_windows.about.show(); _windows.about.show();
}); });
ipcMain.handle('get-argv', () => argv);

View file

@ -6,6 +6,7 @@ import {compose} from 'redux';
import GUI, {AppStateHOC} from 'scratch-gui'; import GUI, {AppStateHOC} from 'scratch-gui';
import ElectronStorageHelper from '../common/ElectronStorageHelper'; import ElectronStorageHelper from '../common/ElectronStorageHelper';
import log from '../common/log';
import styles from './app.css'; import styles from './app.css';
@ -99,4 +100,13 @@ const WrappedGui = compose(
AppStateHOC AppStateHOC
)(GUI); )(GUI);
ipcRenderer.invoke('get-argv').then(
argv => {
log.log(`argv._ = ${argv._}`);
},
err => {
log.warn('Failed to retrieve argv', err);
}
);
ReactDOM.render(<WrappedGui />, appTarget); ReactDOM.render(<WrappedGui />, appTarget);