Skip to content

Commit

Permalink
feat: add xdg support
Browse files Browse the repository at this point in the history
If ~/.local/share/polar exists, use it to store application data instead
of ~/.polar, abiding to the XDG specification.

The xdg directory is not  created  automatically  (the  user  must  have
created it manually beforehand) and if it does not exist, the  data  dir
falls back to ~/.polar.
  • Loading branch information
uwla authored and jamaljsr committed Jan 9, 2024
1 parent 03735cb commit d8b4565
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 2 additions & 3 deletions e2e/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { remove } from 'fs-extra';
import { join } from 'path';
import { homedir } from 'os';
import { ClientFunction } from 'testcafe';
import { networksPath } from '../../src/utils/config';

export const pageUrl = '../build/index.html';

Expand All @@ -14,5 +13,5 @@ export const assertNoConsoleErrors = async (t: TestController) => {
};

export const cleanup = async () => {
await remove(join(homedir(), '.polar', 'networks'));
await remove(networksPath);
};
6 changes: 5 additions & 1 deletion src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import log from 'electron-log';
import { join } from 'path';
import http from 'http';
import https from 'https';
import { existsSync } from 'fs';

/**
* setup logging to store log files in ~/.polar/logs/ dir
*/
export const initLogger = () => {
log.transports.file.resolvePath = (variables: log.PathVariables) => {
const ap = app || remote.app;
return join(ap.getPath('home'), '.polar', 'logs', variables.fileName as string);
const home = ap.getPath('home');
const xdgPath = join(home, '.local', 'share', 'polar');
const dataPath = existsSync(xdgPath) ? xdgPath : join(home, '.polar');
return join(dataPath, 'logs', variables.fileName as string);
};
};

Expand Down
12 changes: 10 additions & 2 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import { join } from 'path';
import { NodeImplementation } from 'shared/types';
import { Network } from 'types';
import { dockerConfigs } from './constants';
import { existsSync } from 'fs';

/**
* XDG-compliant path where application data is stored
*/
export const xdgDataPath = join(remote.app.getPath('home'), '.local', 'share', 'polar');

/**
* root path where application data is stored
*/
export const dataPath = join(remote.app.getPath('home'), '.polar');
export const dataPath = existsSync(xdgDataPath)
? xdgDataPath
: join(remote.app.getPath('home'), '.polar');

/**
* legacy path where application data was stored in v0.1.0
Expand All @@ -20,7 +28,7 @@ export const legacyDataPath = join(remote.app.getPath('userData'), 'data');
export const networksPath = join(dataPath, 'networks');

/**
* returns a path to store dtat for an individual node
* returns a path to store data for an individual node
*/
export const nodePath = (
network: Network,
Expand Down

0 comments on commit d8b4565

Please sign in to comment.