PLWL/src/argv.js

57 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict'
// Shared command-line parsing for the launcher process itself. Both boot
// (process.argv) and the single-instance handoff (second-instance argv) use
// it, so a shortcut, a "Open with PLWL" file-open and Chromium's own injected
// flags are handled the same way everywhere.
//
// Supported forms:
// --launch <id> | --launch=<id> auto-start that library app
// --open <path> | --open=<path> open an .exe with the wine picker
// a bare existing-file path same as --open (plumbing for Exec %f)
function firstNonFlagAfter(args, i) {
// Chromium injects its own flags into the second instance's argv and they
// can land BETWEEN the option and its value (e.g. `--launch
// --allow-file-access-from-files 1790414564627`), so skip anything that
// looks like a flag and take the first non-flag token.
for (let j = i + 1; j < args.length; j++) {
if (!args[j].startsWith('--')) return args[j]
}
return null
}
function findValue(args, names) {
for (const name of names) {
const i = args.indexOf(name)
if (i !== -1) return firstNonFlagAfter(args, i)
const eq = args.find(a => a.startsWith(name + '='))
if (eq) return eq.slice(name.length + 1)
}
return null
}
// PLWL's own options must not swallow Chromium's flags (--flag=... style),
// so anything after a flag that itself carries a value is skipped here by
// looking only at the KNOWN option names above.
function parseArgv(argv) {
const args = Array.isArray(argv) ? argv : process.argv
const launchId = findValue(args, ['--launch'])
let openPath = findValue(args, ['--open'])
if (!openPath && !launchId) {
// "Open with" plumbing (Exec %f): the file path arrives as the only
// non-flag argument - never pick a bare token when a real flag carries
// the intent, and skip this process's own binary (electron dev mode
// passes it as the first argument).
const bare = args.find(
a =>
!a.startsWith('-') &&
/\.(exe|msi|bat|cmd|lnk)$/i.test(a) &&
!/(electron|olcxja\.plwl)(\.exe)?$/i.test(a)
)
if (bare) openPath = bare
}
return { launchId, openPath }
}
module.exports = { parseArgv, findValue }