Skip to content

Commit

Permalink
fix: default node path should be using path.join for cross platform…
Browse files Browse the repository at this point in the history
… path construction
  • Loading branch information
CMCDragonkai committed Aug 11, 2022
1 parent 7b1edbd commit 27df70f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ function getDefaultNodePath(): string | undefined {
const homeDir = os.homedir();
const dataDir = process.env.XDG_DATA_HOME;
if (dataDir != null) {
p = `${dataDir}/${prefix}`;
p = path.join(dataDir, prefix);
} else {
p = `${homeDir}/.local/share/${prefix}`;
p = path.join(homeDir, '.local', 'share', prefix);
}
} else if (platform === 'darwin') {
const homeDir = os.homedir();
p = `${homeDir}/Library/Application Support/${prefix}`;
p = path.join(homeDir, 'Library', 'Application Support', prefix);
} else if (platform === 'win32') {
const homeDir = os.homedir();
const appDataDir = process.env.LOCALAPPDATA;
if (appDataDir != null) {
p = `${appDataDir}/${prefix}`;
p = path.join(appDataDir, prefix);
} else {
p = `${homeDir}/AppData/Local/${prefix}`;
p = path.join(homeDir, 'AppData', 'Local', prefix);
}
} else {
return;
Expand Down
22 changes: 19 additions & 3 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import os from 'os';
import path from 'path';
import process from 'process';
import * as utils from '@/utils';

describe('utils', () => {
test('getting default node path', () => {
const homeDir = os.homedir();
const prefix = 'polykey';
const p = utils.getDefaultNodePath();
expect(p).toBeDefined();
if (process.platform === 'linux') {
expect(p).toBe(`${homeDir}/.local/share/polykey`);
const dataDir = process.env.XDG_DATA_HOME;
if (dataDir != null) {
expect(p).toBe(path.join(dataDir, prefix));
} else {
expect(p).toBe(path.join(homeDir, '.local', 'share', prefix));
}
} else if (process.platform === 'darwin') {
expect(p).toBe(`${homeDir}/Library/Application Support/polykey`);
expect(p).toBe(
path.join(homeDir, 'Library', 'Application Support', 'polykey'),
);
} else if (process.platform === 'win32') {
expect(p).toBe(`${homeDir}/AppData/Local/polykey`);
const appDataDir = process.env.LOCALAPPDATA;
if (appDataDir != null) {
expect(p).toBe(path.join(appDataDir, prefix));
} else {
expect(p).toBe(path.join(homeDir, 'AppData', 'Local', prefix));
}
}
});
});

0 comments on commit 27df70f

Please sign in to comment.