Skip to content

Commit 70a37a3

Browse files
authored
feat: add storeDir config (#31)
* feat: add storeDir config * fix: storeDir spec for windows
1 parent 96179ad commit 70a37a3

File tree

3 files changed

+74
-57
lines changed

3 files changed

+74
-57
lines changed

spec/get-store-path.spec.ts

+61-49
Original file line numberDiff line numberDiff line change
@@ -33,72 +33,84 @@ describe('get-store-path', () => {
3333
});
3434
});
3535

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

43-
afterEach(() => {
44-
delete process.env.CONFIG_DIR;
45-
});
41+
expect(path).toEqual('/my/store/dir/pop.dog');
42+
});
43+
});
4644

47-
it('should respect CONFIG_DIR', () => {
48-
const path = getStorePath('pop');
45+
describe('when absent', () => {
46+
describe('platforms', () => {
47+
describeIfUnix('unix', () => {
48+
describe('when CONFIG_DIR is present', () => {
49+
beforeEach(() => {
50+
process.env.CONFIG_DIR = '/home/config_dir';
51+
});
4952

50-
expect(path).toEqual('/home/config_dir/pop.haf');
51-
});
52-
});
53+
afterEach(() => {
54+
delete process.env.CONFIG_DIR;
55+
});
5356

54-
describe('when XDG_CONFIG_HOME is present', () => {
55-
beforeEach(() => {
56-
process.env.XDG_CONFIG_HOME = '/home/xdg_config_home';
57-
});
57+
it('should respect CONFIG_DIR', () => {
58+
const path = getStorePath('pop');
5859

59-
afterEach(() => {
60-
delete process.env.XDG_CONFIG_HOME;
61-
});
60+
expect(path).toEqual('/home/config_dir/pop.haf');
61+
});
62+
});
6263

63-
it('should respect XDG_CONFIG_HOME', () => {
64-
const path = getStorePath('pop');
64+
describe('when XDG_CONFIG_HOME is present', () => {
65+
beforeEach(() => {
66+
process.env.XDG_CONFIG_HOME = '/home/xdg_config_home';
67+
});
6568

66-
expect(path).toEqual('/home/xdg_config_home/pop.haf');
67-
});
68-
});
69+
afterEach(() => {
70+
delete process.env.XDG_CONFIG_HOME;
71+
});
6972

70-
describe('when fallback', () => {
71-
const spy = jest.spyOn(os, 'homedir');
73+
it('should respect XDG_CONFIG_HOME', () => {
74+
const path = getStorePath('pop');
7275

73-
beforeEach(() => {
74-
spy.mockReturnValue('/Users/pop');
75-
});
76+
expect(path).toEqual('/home/xdg_config_home/pop.haf');
77+
});
78+
});
7679

77-
afterEach(() => {
78-
spy.mockRestore();
79-
});
80+
describe('when fallback', () => {
81+
const spy = jest.spyOn(os, 'homedir');
82+
83+
beforeEach(() => {
84+
spy.mockReturnValue('/Users/pop');
85+
});
8086

81-
it('should put under ~/.config', () => {
82-
const path = getStorePath('pop');
87+
afterEach(() => {
88+
spy.mockRestore();
89+
});
8390

84-
expect(path).toEqual('/Users/pop/.config/pop.haf');
91+
it('should put under ~/.config', () => {
92+
const path = getStorePath('pop');
93+
94+
expect(path).toEqual('/Users/pop/.config/pop.haf');
95+
});
96+
});
8597
});
86-
});
87-
});
8898

89-
describeIfWindows('when windows 😞', () => {
90-
beforeEach(() => {
91-
process.env.LOCALAPPDATA = 'C:\\Users\\Pop\\ApplicationData';
92-
});
99+
describeIfWindows('when windows 😞', () => {
100+
beforeEach(() => {
101+
process.env.LOCALAPPDATA = 'C:\\Users\\Pop\\ApplicationData';
102+
});
93103

94-
afterEach(() => {
95-
delete process.env.LOCALAPPDATA;
96-
});
104+
afterEach(() => {
105+
delete process.env.LOCALAPPDATA;
106+
});
97107

98-
it('should deal with WINDOWS', () => {
99-
const path = getStorePath('pop');
108+
it('should deal with WINDOWS', () => {
109+
const path = getStorePath('pop');
100110

101-
expect(path).toEqual('C:\\Users\\Pop\\ApplicationData\\pop.haf');
111+
expect(path).toEqual('C:\\Users\\Pop\\ApplicationData\\pop.haf');
112+
});
113+
});
102114
});
103115
});
104116
});

src/get-store-path.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import path from 'path';
22
import os from 'os';
33

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

7-
const storeDir =
8-
process.env['CONFIG_DIR'] ||
9-
process.env['XDG_CONFIG_HOME'] ||
10-
(os.platform() === 'win32' && process.env['LOCALAPPDATA']) ||
11-
path.join(os.homedir(), '.config');
10+
export const getStorePath = (
11+
name: string,
12+
extension = 'haf',
13+
storeDir = getDefaultDir(),
14+
): string => {
15+
const fileName = `${name}.${extension}`;
1216

1317
return path.join(storeDir, fileName);
1418
};

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ interface HafConfig<Schema> {
77
name: string;
88
extension?: string;
99
defaultSchema?: Partial<Schema>;
10+
storeDir?: string;
1011
}
1112

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

1617
constructor(config: HafConfig<Schema>) {
17-
this.storePath = getStorePath(config.name, config.extension);
18+
this.storePath = getStorePath(config.name, config.extension, config.storeDir);
1819
this.defaultSchema = config.defaultSchema ?? {};
1920

2021
this.initializeStore();

0 commit comments

Comments
 (0)