Skip to content

Commit

Permalink
implement custom config dir arg
Browse files Browse the repository at this point in the history
closes #1956
  • Loading branch information
mifi committed May 14, 2024
1 parent 0417932 commit fedd826
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ LosslessCut --settings-json '{captureFormat:"jpeg", "keyframeCut":true}'
- `--disable-networking` Turn off all network requests.
- `--http-api` Start the [HTTP server with an API](./api.md) to control LosslessCut, optionally specifying a port (default `8080`).
- `--keyboard-action` Run a keyboard action (see below.)
- `--custom-config-dir` Path to a directory where the `config.json` file will be stored and loaded from.

## Controlling a running instance (experimental)

Expand Down
11 changes: 6 additions & 5 deletions src/main/configStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,18 @@ const defaults: Config = {
invertTimelineScroll: undefined,
};

// look for a config.json file next to the executable
// For portable app: https://github.com/mifi/lossless-cut/issues/645
async function getCustomStoragePath() {
async function lookForCustomStoragePath() {
try {
if (!isWindows || process.windowsStore) return undefined;

// https://github.com/mifi/lossless-cut/issues/645#issuecomment-1001363314
// https://stackoverflow.com/questions/46307797/how-to-get-the-original-path-of-a-portable-electron-app
// https://github.com/electron-userland/electron-builder/blob/master/docs/configuration/nsis.md
if (!isWindows || process.windowsStore) return undefined;
const customStorageDir = process.env['PORTABLE_EXECUTABLE_DIR'] || dirname(app.getPath('exe'));
const customConfigPath = join(customStorageDir, 'config.json');
if (await pathExists(customConfigPath)) return customStorageDir;

return undefined;
} catch (err) {
logger.error('Failed to get custom storage path', err);
Expand Down Expand Up @@ -196,8 +197,8 @@ async function tryCreateStore({ customStoragePath }: { customStoragePath: string
throw new Error('Timed out while creating config store');
}

export async function init() {
const customStoragePath = await getCustomStoragePath();
export async function init({ customConfigDir }: { customConfigDir: string | undefined }) {
const customStoragePath = customConfigDir ?? await lookForCustomStoragePath();
if (customStoragePath) logger.info('customStoragePath', customStoragePath);

await tryCreateStore({ customStoragePath });
Expand Down
7 changes: 5 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ function parseCliArgs(rawArgv = process.argv) {
// dev: First 2 args are electron and the index.js
const argsWithoutAppName = rawArgv.length > ignoreFirstArgs ? rawArgv.slice(ignoreFirstArgs) : [];

return yargsParser(argsWithoutAppName, { boolean: ['allow-multiple-instances', 'disable-networking'], string: ['settings-json'] });
return yargsParser(argsWithoutAppName, {
boolean: ['allow-multiple-instances', 'disable-networking'],
string: ['settings-json', 'custom-config-dir'],
});
}

const argv = parseCliArgs();
Expand Down Expand Up @@ -330,7 +333,7 @@ const readyPromise = app.whenReady();
(async () => {
try {
logger.info('Initializing config store');
await configStore.init();
await configStore.init({ customConfigDir: argv['customConfigDir'] });

const allowMultipleInstances = configStore.get('allowMultipleInstances');

Expand Down

0 comments on commit fedd826

Please sign in to comment.