From 96179ad3b33ead57efba9b4039665b36d19dbc2a Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Sat, 30 Dec 2023 00:43:06 +0300 Subject: [PATCH] feat: add .haf extension by default (#30) --- spec/get-store-path.spec.ts | 16 ++++++++-------- src/get-store-path.ts | 10 ++++------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/spec/get-store-path.spec.ts b/spec/get-store-path.spec.ts index 464b3cd..534a7ad 100644 --- a/spec/get-store-path.spec.ts +++ b/spec/get-store-path.spec.ts @@ -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); }); @@ -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'); }); }); @@ -63,7 +63,7 @@ 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'); }); }); @@ -71,7 +71,7 @@ describe('get-config-path', () => { const spy = jest.spyOn(os, 'homedir'); beforeEach(() => { - spy.mockReturnValue('/Users/anda'); + spy.mockReturnValue('/Users/pop'); }); afterEach(() => { @@ -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'); }); }); }); @@ -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'); }); }); }); diff --git a/src/get-store-path.ts b/src/get-store-path.ts index fbea162..de877b4 100644 --- a/src/get-store-path.ts +++ b/src/get-store-path.ts @@ -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); };