Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expand scenarios covered by default arguments for environments #20

Merged
merged 10 commits into from
Dec 28, 2021
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
'use strict';

function defaultArgv() {
// When evaluating script arg, user CLI args follow executable
const execArgv = process.execArgv;
if (execArgv.includes('-e') || execArgv.includes('--eval') ||
execArgv.includes('-p') || execArgv.includes('--print')) {
return process.argv.slice(1);
}

// In bundled Electron app, user CLI args follow executable:
// 1) process.versions.electron is either set by electron, or undefined
// see https://github.com/electron/electron/blob/master/docs/api/process.md#processversionselectron-readonly
// 2) process.defaultApp is either set by electron in an electron
// unbundled app, or undefined
// see https://github.com/electron/electron/blob/master/docs/api/process.md#processversionselectron-readonly
if (process.versions && process.versions.electron && !process.defaultApp) {
shadowspawn marked this conversation as resolved.
Show resolved Hide resolved
return process.argv.slice(1);
}

// Normally first two arguments are executable and script,
// then CLI arguments
return process.argv.slice(2);
}

const parseArgs = (
argv = process.argv.slice(require.main ? 2 : 1),
argv = defaultArgv(),
options = {}
) => {
if (typeof options !== 'object' || options === null) {
Expand Down