The swiss army knife of lossless video/audio editing
Resolve CLI file args against the second instance's cwd
Claudeae957f1b7d66
diff --git a/src/main/index.ts b/src/main/index.tsindex 34e43f09..5d32f59c 100644--- a/src/main/index.ts+++ b/src/main/index.ts@@ -25,7 +25,7 @@ import mitt from 'mitt';import logger from './logger.js';import menu from './menu.js';import * as configStore from './configStore.js';-import { isLinux, isWindows, isMac, platform, arch, pathExists } from './util.js';+import { isLinux, isWindows, isMac, platform, arch, pathExists, resolveCliFileArgs } from './util.js';import { appName } from './common.js';import attachContextMenu from './contextMenu.js';import HttpServer from './httpServer.js';@@ -308,7 +308,7 @@ async function init() {// However when users start your app in command line, the system's single instance mechanism will be bypassed, and you have to use this method to ensure single instance.// This can be tested with one terminal: npx electron .// and another terminal: npx electron . path/to/file.mp4- app.on('second-instance', (_event, _commandLine, _workingDirectory, additionalData) => {+ app.on('second-instance', (_event, _commandLine, workingDirectory, additionalData) => {// Someone tried to run a second instance, we should focus our window.if (mainWindow) {if (mainWindow.isMinimized()) mainWindow.restore();@@ -322,7 +322,7 @@ async function init() {logger.info('second-instance', argv2);if (argv2['keyboardAction']) sendApiAction(argv2['keyboardAction'], argv2._.map((arg) => JSON.parse(String(arg))));- else if (argv2._ && argv2._.length > 0) openFilesEventually(argv2._.map(String));+ else if (argv2._ && argv2._.length > 0) openFilesEventually(resolveCliFileArgs(argv2._.map(String), workingDirectory));});// Quit when all windows are closed.diff --git a/src/main/util.test.ts b/src/main/util.test.tsnew file mode 100644index 00000000..5c724e59--- /dev/null+++ b/src/main/util.test.ts@@ -0,0 +1,19 @@+// eslint-disable-next-line import/no-extraneous-dependencies+import { describe, expect, test } from 'vitest';+import { join, resolve } from 'node:path';++import { resolveCliFileArgs } from './util';++// build platform-native absolute paths so the test also passes on Windows+const cwd = resolve('/home/user/videos');++describe('resolveCliFileArgs', () => {+ test('resolves relative file args against the given cwd (#1571)', () => {+ expect(resolveCliFileArgs(['video.mp4', 'clips/a.mkv'], cwd)).toEqual([join(cwd, 'video.mp4'), join(cwd, 'clips', 'a.mkv')]);+ });++ test('leaves absolute file args unchanged', () => {+ const absolute = resolve('/media/in/a.mp4');+ expect(resolveCliFileArgs([absolute], cwd)).toEqual([absolute]);+ });+});diff --git a/src/main/util.ts b/src/main/util.tsindex c85814bb..5a01857c 100644--- a/src/main/util.ts+++ b/src/main/util.ts@@ -1,5 +1,6 @@import { access, constants } from 'node:fs/promises';import os from 'node:os';+import { resolve } from 'node:path';export const platform = os.platform();export const arch = os.arch();@@ -16,3 +17,7 @@ export async function pathExists(path: string) {return false;}}++// File args forwarded from a second instance are relative to *its* cwd, not ours+// https://github.com/mifi/lossless-cut/issues/1571+export const resolveCliFileArgs = (args: string[], workingDirectory: string) => args.map((arg) => resolve(workingDirectory, arg));