From 802a7014306818d61282956f7feaecfccfab73dc Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Sat, 19 Feb 2022 17:23:58 +0800 Subject: [PATCH] allow overriding any stored setting from the command line #371 --- README.md | 13 +++++++++++++ package.json | 3 ++- public/electron.js | 47 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d734d556..5029b27c 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,19 @@ If you have an issue you can check the developer tools for any errors or clues. 1234,,Last segment ``` +## Command line interface (CLI) + +### Open one or more files: +```bash +LosslessCut file1.mp4 file2.mkv +``` + +### Override settings (experimental) +See [available settings](https://github.com/mifi/lossless-cut/blob/master/public/configStore.js). Note that this is subject to change in newer versions. ⚠️ If you specify incorrect values it could corrupt your configuration file. You may use JSON or JSON5: +```bash +LosslessCut --settings-json '{captureFormat:"jpeg", "keyframeCut":true}' +``` + ## Developing See the [developer notes](developer-notes.md). diff --git a/package.json b/package.json index 18b72f19..e8854328 100644 --- a/package.json +++ b/package.json @@ -115,7 +115,8 @@ "semver": "^7.1.3", "string-to-stream": "^1.1.1", "strtok3": "^6.0.0", - "trash": "^7.1.1" + "trash": "^7.1.1", + "yargs-parser": "^21.0.0" }, "eslintConfig": { "extends": "react-app" diff --git a/public/electron.js b/public/electron.js index 6b39c30e..23668984 100644 --- a/public/electron.js +++ b/public/electron.js @@ -3,6 +3,9 @@ const isDev = require('electron-is-dev'); const unhandled = require('electron-unhandled'); const i18n = require('i18next'); const debounce = require('lodash/debounce'); +const yargsParser = require('yargs-parser'); +const JSON5 = require('json5'); + const menu = require('./menu'); const configStore = require('./configStore'); @@ -23,6 +26,10 @@ unhandled({ app.name = 'LosslessCut'; +let filesToOpen; +let openFileInitial; +let fileOpened = false; + // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow; @@ -116,6 +123,20 @@ function updateMenu() { menu(app, mainWindow, newVersion); } + +// https://github.com/electron/electron/issues/3657 +// https://github.com/mifi/lossless-cut/issues/357 +// https://github.com/mifi/lossless-cut/issues/639 +// https://github.com/mifi/lossless-cut/issues/591 +function parseCliArgs() { + const ignoreFirstArgs = isDev ? 2 : 1; + // production: First arg is the LosslessCut executable + // dev: First 2 args are electron and the electron.js + const argsWithoutAppName = process.argv.length > ignoreFirstArgs ? process.argv.slice(ignoreFirstArgs) : []; + + return yargsParser(argsWithoutAppName); +} + // This method will be called when Electron has finished // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. @@ -132,6 +153,18 @@ app.on('ready', async () => { await configStore.init(); + const argv = parseCliArgs(); + console.log('CLI arguments', argv); + filesToOpen = argv._; + const { settingsJson } = argv; + + if (settingsJson != null) { + console.log('initializing settings', settingsJson); + Object.entries(JSON5.parse(settingsJson)).forEach(([key, value]) => { + configStore.set(key, value); + }); + } + if (isDev) { const { default: installExtension, REACT_DEVELOPER_TOOLS } = require('electron-devtools-installer'); // eslint-disable-line global-require,import/no-extraneous-dependencies @@ -163,9 +196,6 @@ app.on('activate', () => { } }); -let openFileInitial; -let fileOpened = false; - function openFilesOnce(files) { if (fileOpened) return; fileOpened = true; @@ -174,17 +204,6 @@ function openFilesOnce(files) { electron.ipcMain.on('renderer-ready', () => { rendererReady = true; - const ignoreFirstArgs = isDev ? 2 : 1; - // production: First arg is the LosslessCut executable - // dev: First 2 args are electron and the electron.js - - // https://github.com/electron/electron/issues/3657 - // https://github.com/mifi/lossless-cut/issues/357 - // https://github.com/mifi/lossless-cut/issues/639 - // https://github.com/mifi/lossless-cut/issues/591 - const filesToOpen = process.argv.length > ignoreFirstArgs - ? process.argv.slice(ignoreFirstArgs).filter((arg) => arg && !arg.startsWith('-')) - : []; if (filesToOpen.length > 0) openFilesOnce(filesToOpen); else if (openFileInitial) openFilesOnce([openFileInitial]);