Skip to content

Commit

Permalink
feat: add storeDir config (#31)
Browse files Browse the repository at this point in the history
* feat: add storeDir config

* fix: storeDir spec for windows
  • Loading branch information
BatuhanW authored Dec 29, 2023
1 parent 96179ad commit 70a37a3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 57 deletions.
110 changes: 61 additions & 49 deletions spec/get-store-path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,72 +33,84 @@ describe('get-store-path', () => {
});
});

describe('platforms', () => {
describeIfUnix('unix', () => {
describe('when CONFIG_DIR is present', () => {
beforeEach(() => {
process.env.CONFIG_DIR = '/home/config_dir';
});
describe('storeDir', () => {
describeIfUnix('when provided', () => {
it('should respect storeDir', () => {
const path = getStorePath('pop', 'dog', '/my/store/dir');

afterEach(() => {
delete process.env.CONFIG_DIR;
});
expect(path).toEqual('/my/store/dir/pop.dog');
});
});

it('should respect CONFIG_DIR', () => {
const path = getStorePath('pop');
describe('when absent', () => {
describe('platforms', () => {
describeIfUnix('unix', () => {
describe('when CONFIG_DIR is present', () => {
beforeEach(() => {
process.env.CONFIG_DIR = '/home/config_dir';
});

expect(path).toEqual('/home/config_dir/pop.haf');
});
});
afterEach(() => {
delete process.env.CONFIG_DIR;
});

describe('when XDG_CONFIG_HOME is present', () => {
beforeEach(() => {
process.env.XDG_CONFIG_HOME = '/home/xdg_config_home';
});
it('should respect CONFIG_DIR', () => {
const path = getStorePath('pop');

afterEach(() => {
delete process.env.XDG_CONFIG_HOME;
});
expect(path).toEqual('/home/config_dir/pop.haf');
});
});

it('should respect XDG_CONFIG_HOME', () => {
const path = getStorePath('pop');
describe('when XDG_CONFIG_HOME is present', () => {
beforeEach(() => {
process.env.XDG_CONFIG_HOME = '/home/xdg_config_home';
});

expect(path).toEqual('/home/xdg_config_home/pop.haf');
});
});
afterEach(() => {
delete process.env.XDG_CONFIG_HOME;
});

describe('when fallback', () => {
const spy = jest.spyOn(os, 'homedir');
it('should respect XDG_CONFIG_HOME', () => {
const path = getStorePath('pop');

beforeEach(() => {
spy.mockReturnValue('/Users/pop');
});
expect(path).toEqual('/home/xdg_config_home/pop.haf');
});
});

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

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

it('should put under ~/.config', () => {
const path = getStorePath('pop');
afterEach(() => {
spy.mockRestore();
});

expect(path).toEqual('/Users/pop/.config/pop.haf');
it('should put under ~/.config', () => {
const path = getStorePath('pop');

expect(path).toEqual('/Users/pop/.config/pop.haf');
});
});
});
});
});

describeIfWindows('when windows 😞', () => {
beforeEach(() => {
process.env.LOCALAPPDATA = 'C:\\Users\\Pop\\ApplicationData';
});
describeIfWindows('when windows 😞', () => {
beforeEach(() => {
process.env.LOCALAPPDATA = 'C:\\Users\\Pop\\ApplicationData';
});

afterEach(() => {
delete process.env.LOCALAPPDATA;
});
afterEach(() => {
delete process.env.LOCALAPPDATA;
});

it('should deal with WINDOWS', () => {
const path = getStorePath('pop');
it('should deal with WINDOWS', () => {
const path = getStorePath('pop');

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

export const getStorePath = (name: string, extension = 'haf'): string => {
const fileName = `${name}.${extension}`;
const getDefaultDir = () =>
process.env['CONFIG_DIR'] ||
process.env['XDG_CONFIG_HOME'] ||
(os.platform() === 'win32' && process.env['LOCALAPPDATA']) ||
path.join(os.homedir(), '.config');

const storeDir =
process.env['CONFIG_DIR'] ||
process.env['XDG_CONFIG_HOME'] ||
(os.platform() === 'win32' && process.env['LOCALAPPDATA']) ||
path.join(os.homedir(), '.config');
export const getStorePath = (
name: string,
extension = 'haf',
storeDir = getDefaultDir(),
): string => {
const fileName = `${name}.${extension}`;

return path.join(storeDir, fileName);
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ interface HafConfig<Schema> {
name: string;
extension?: string;
defaultSchema?: Partial<Schema>;
storeDir?: string;
}

class Haf<Schema, FlattenedSchema = FlattenedWithDotNotation<Schema>> {
private storePath: string;
private defaultSchema: Partial<Schema>;

constructor(config: HafConfig<Schema>) {
this.storePath = getStorePath(config.name, config.extension);
this.storePath = getStorePath(config.name, config.extension, config.storeDir);
this.defaultSchema = config.defaultSchema ?? {};

this.initializeStore();
Expand Down

0 comments on commit 70a37a3

Please sign in to comment.