Skip to content

Commit

Permalink
feat: add .haf extension by default (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
BatuhanW committed Dec 29, 2023
1 parent fe8f535 commit 96179ad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
16 changes: 8 additions & 8 deletions spec/get-store-path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ const describeIfUnix = isWindows ? describe.skip : describe;

const splitKey = isWindows ? '\\' : '/';

describe('get-config-path', () => {
describe('get-store-path', () => {
describe('extension', () => {
describe('when absent', () => {
it('should skip extension', () => {
it('should add .haf extension', () => {
const path = getStorePath('pop');

const index = path.split(splitKey).lastIndexOf('pop');
const index = path.split(splitKey).lastIndexOf('pop.haf');

expect(index).toBeGreaterThanOrEqual(0);
});
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('get-config-path', () => {
it('should respect CONFIG_DIR', () => {
const path = getStorePath('pop');

expect(path).toEqual('/home/config_dir/pop');
expect(path).toEqual('/home/config_dir/pop.haf');
});
});

Expand All @@ -63,15 +63,15 @@ describe('get-config-path', () => {
it('should respect XDG_CONFIG_HOME', () => {
const path = getStorePath('pop');

expect(path).toEqual('/home/xdg_config_home/pop');
expect(path).toEqual('/home/xdg_config_home/pop.haf');
});
});

describe('when fallback', () => {
const spy = jest.spyOn(os, 'homedir');

beforeEach(() => {
spy.mockReturnValue('/Users/anda');
spy.mockReturnValue('/Users/pop');
});

afterEach(() => {
Expand All @@ -81,7 +81,7 @@ describe('get-config-path', () => {
it('should put under ~/.config', () => {
const path = getStorePath('pop');

expect(path).toEqual('/Users/anda/.config/pop');
expect(path).toEqual('/Users/pop/.config/pop.haf');
});
});
});
Expand All @@ -98,7 +98,7 @@ describe('get-config-path', () => {
it('should deal with WINDOWS', () => {
const path = getStorePath('pop');

expect(path).toEqual('C:\\Users\\Pop\\ApplicationData\\pop');
expect(path).toEqual('C:\\Users\\Pop\\ApplicationData\\pop.haf');
});
});
});
Expand Down
10 changes: 4 additions & 6 deletions src/get-store-path.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import path from 'path';
import os from 'os';

export const getStorePath = (name: string, extension?: string): string => {
if (extension) {
name += `.${extension}`;
}
export const getStorePath = (name: string, extension = 'haf'): string => {
const fileName = `${name}.${extension}`;

const confDir =
const storeDir =
process.env['CONFIG_DIR'] ||
process.env['XDG_CONFIG_HOME'] ||
(os.platform() === 'win32' && process.env['LOCALAPPDATA']) ||
path.join(os.homedir(), '.config');

return path.join(confDir, name);
return path.join(storeDir, fileName);
};

0 comments on commit 96179ad

Please sign in to comment.