From 2af3093a700a24984f4c52b656ba28bf7088d421 Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Fri, 29 Dec 2023 22:30:50 +0300 Subject: [PATCH 1/7] feat: add dev command --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 1b68124..76e4126 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dist/**/*" ], "scripts": { + "dev": "npm run build -- --watch", "test": "jest --runInBand", "test:coverage": "jest --runInBand --coverage", "build": "rimraf dist && tsc --build tsconfig.build.json", From 65471acb571604f2c15bd1efcb6b3c992290d7ec Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Fri, 29 Dec 2023 22:32:09 +0300 Subject: [PATCH 2/7] feat: add test:watch command --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 76e4126..831a266 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "scripts": { "dev": "npm run build -- --watch", "test": "jest --runInBand", + "test:watch": "npm run test -- --watch", "test:coverage": "jest --runInBand --coverage", "build": "rimraf dist && tsc --build tsconfig.build.json", "lint": "eslint . --ext .ts", From b421925d892b783abd939d318d0ab3b7e066d11b Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Fri, 29 Dec 2023 22:42:21 +0300 Subject: [PATCH 3/7] refactor: rename upsertSchema to initializeStore --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8b58736..2cd4ca3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,10 +23,10 @@ class Haf> { this.#options = Object.assign(this.#defaultOptions, options); this.configPath = getConfigPath(this.#options.name, this.#options.extension); - this.upsertSchema(); + this.initializeStore(); } - private upsertSchema() { + private initializeStore() { if (pathExistsSync(this.configPath)) return; writeJsonSync(this.configPath, this.#options.defaultSchema); From 79a6b2e0763d62871e9f69ef77740052333cb273 Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Fri, 29 Dec 2023 22:46:58 +0300 Subject: [PATCH 4/7] refactor: rename configPath references to storePath --- ...onfig-path.spec.ts => get-store-path.spec.ts} | 14 +++++++------- src/{get-config-path.ts => get-store-path.ts} | 2 +- src/index.ts | 16 ++++++++-------- 3 files changed, 16 insertions(+), 16 deletions(-) rename spec/{get-config-path.spec.ts => get-store-path.spec.ts} (87%) rename src/{get-config-path.ts => get-store-path.ts} (81%) diff --git a/spec/get-config-path.spec.ts b/spec/get-store-path.spec.ts similarity index 87% rename from spec/get-config-path.spec.ts rename to spec/get-store-path.spec.ts index a604417..464b3cd 100644 --- a/spec/get-config-path.spec.ts +++ b/spec/get-store-path.spec.ts @@ -1,6 +1,6 @@ import os from 'os'; -import { getConfigPath } from '../src/get-config-path'; +import { getStorePath } from '../src/get-store-path'; const isWindows = os.platform() === 'win32'; @@ -14,7 +14,7 @@ describe('get-config-path', () => { describe('extension', () => { describe('when absent', () => { it('should skip extension', () => { - const path = getConfigPath('pop'); + const path = getStorePath('pop'); const index = path.split(splitKey).lastIndexOf('pop'); @@ -24,7 +24,7 @@ describe('get-config-path', () => { describe('when provided', () => { it('should append extension', () => { - const path = getConfigPath('pop', 'dog'); + const path = getStorePath('pop', 'dog'); const index = path.split(splitKey).lastIndexOf('pop.dog'); @@ -45,7 +45,7 @@ describe('get-config-path', () => { }); it('should respect CONFIG_DIR', () => { - const path = getConfigPath('pop'); + const path = getStorePath('pop'); expect(path).toEqual('/home/config_dir/pop'); }); @@ -61,7 +61,7 @@ describe('get-config-path', () => { }); it('should respect XDG_CONFIG_HOME', () => { - const path = getConfigPath('pop'); + const path = getStorePath('pop'); expect(path).toEqual('/home/xdg_config_home/pop'); }); @@ -79,7 +79,7 @@ describe('get-config-path', () => { }); it('should put under ~/.config', () => { - const path = getConfigPath('pop'); + const path = getStorePath('pop'); expect(path).toEqual('/Users/anda/.config/pop'); }); @@ -96,7 +96,7 @@ describe('get-config-path', () => { }); it('should deal with WINDOWS', () => { - const path = getConfigPath('pop'); + const path = getStorePath('pop'); expect(path).toEqual('C:\\Users\\Pop\\ApplicationData\\pop'); }); diff --git a/src/get-config-path.ts b/src/get-store-path.ts similarity index 81% rename from src/get-config-path.ts rename to src/get-store-path.ts index a7d26a3..fbea162 100644 --- a/src/get-config-path.ts +++ b/src/get-store-path.ts @@ -1,7 +1,7 @@ import path from 'path'; import os from 'os'; -export const getConfigPath = (name: string, extension?: string): string => { +export const getStorePath = (name: string, extension?: string): string => { if (extension) { name += `.${extension}`; } diff --git a/src/index.ts b/src/index.ts index 2cd4ca3..4692271 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { pathExistsSync, readJsonSync, writeJsonSync, removeSync } from 'fs-extra'; -import { getConfigPath } from './get-config-path'; +import { getStorePath } from './get-store-path'; import type { FlattenedWithDotNotation, OptionalKeysOf, StringKeysOf, ArrayKeysOf } from './types'; interface Options { @@ -11,7 +11,7 @@ interface Options { class Haf> { readonly #options: Readonly>; - private configPath: string; + private storePath: string; readonly #defaultOptions: Readonly> = { name: 'haf', @@ -21,23 +21,23 @@ class Haf> { constructor(options: Options) { this.#options = Object.assign(this.#defaultOptions, options); - this.configPath = getConfigPath(this.#options.name, this.#options.extension); + this.storePath = getStorePath(this.#options.name, this.#options.extension); this.initializeStore(); } private initializeStore() { - if (pathExistsSync(this.configPath)) return; + if (pathExistsSync(this.storePath)) return; - writeJsonSync(this.configPath, this.#options.defaultSchema); + writeJsonSync(this.storePath, this.#options.defaultSchema); } get store(): Schema | Partial { - return readJsonSync(this.configPath); + return readJsonSync(this.storePath); } set store(schema: Schema | Partial) { - writeJsonSync(this.configPath, schema); + writeJsonSync(this.storePath, schema); } get>(path: Path): FlattenedSchema[Path] { @@ -76,7 +76,7 @@ class Haf> { } nuke(): void { - removeSync(this.configPath); + removeSync(this.storePath); } private _get, Returns = FlattenedSchema[Path]>( From 31260ac7a56c76373a89c608e8fab3d88db9a2f8 Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Fri, 29 Dec 2023 23:03:29 +0300 Subject: [PATCH 5/7] refactor: make defaultSchema private class property --- spec/index.spec.ts | 7 ++++--- src/index.ts | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/spec/index.spec.ts b/spec/index.spec.ts index aeea182..c9c8aac 100644 --- a/spec/index.spec.ts +++ b/spec/index.spec.ts @@ -366,12 +366,13 @@ describe('Haf', () => { }); describe('when path provided', () => { - it('resets given path to its default value', () => { - haf.set('name', 'Pop2'); + it('resets **only** given path to its default value', () => { + haf.set('name', 'Popita'); + haf.set('age', 3); haf.reset('name'); - expect(haf.get('name')).toEqual('Pop'); + expect(haf.store).toEqual({ ...defaultSchema, age: 3 }); }); }); }); diff --git a/src/index.ts b/src/index.ts index 4692271..9977c2f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,16 +12,17 @@ interface Options { class Haf> { readonly #options: Readonly>; private storePath: string; + private defaultSchema: Partial; readonly #defaultOptions: Readonly> = { name: 'haf', extension: '', - defaultSchema: {}, }; constructor(options: Options) { this.#options = Object.assign(this.#defaultOptions, options); this.storePath = getStorePath(this.#options.name, this.#options.extension); + this.defaultSchema = options.defaultSchema ?? {}; this.initializeStore(); } @@ -29,7 +30,7 @@ class Haf> { private initializeStore() { if (pathExistsSync(this.storePath)) return; - writeJsonSync(this.storePath, this.#options.defaultSchema); + writeJsonSync(this.storePath, this.defaultSchema); } get store(): Schema | Partial { @@ -65,12 +66,12 @@ class Haf> { reset(path?: StringKeysOf): void { if (typeof path === 'undefined') { - this.store = this.#defaultOptions.defaultSchema || {}; + this.store = this.defaultSchema; return; } - const defaultValue = this._get(path, this.#defaultOptions.defaultSchema); + const defaultValue = this._get(path, this.defaultSchema); this._set(path, defaultValue); } From 88630e43744d5ac4f7cac5683165d08d09a34b9b Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Fri, 29 Dec 2023 23:11:55 +0300 Subject: [PATCH 6/7] refactor: get rid of defaultOptions --- src/index.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9977c2f..4e18989 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,18 +10,11 @@ interface Options { } class Haf> { - readonly #options: Readonly>; private storePath: string; private defaultSchema: Partial; - readonly #defaultOptions: Readonly> = { - name: 'haf', - extension: '', - }; - constructor(options: Options) { - this.#options = Object.assign(this.#defaultOptions, options); - this.storePath = getStorePath(this.#options.name, this.#options.extension); + this.storePath = getStorePath(options.name, options.extension); this.defaultSchema = options.defaultSchema ?? {}; this.initializeStore(); From 327597b22e052e9b1bb2d08587a8a3432a4d1d05 Mon Sep 17 00:00:00 2001 From: Batuhan Wilhelm Date: Fri, 29 Dec 2023 23:21:05 +0300 Subject: [PATCH 7/7] refactor: rename Options interface to HafConfig --- src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4e18989..6874215 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import { pathExistsSync, readJsonSync, writeJsonSync, removeSync } from 'fs-extr import { getStorePath } from './get-store-path'; import type { FlattenedWithDotNotation, OptionalKeysOf, StringKeysOf, ArrayKeysOf } from './types'; -interface Options { +interface HafConfig { name: string; extension?: string; defaultSchema?: Partial; @@ -13,9 +13,9 @@ class Haf> { private storePath: string; private defaultSchema: Partial; - constructor(options: Options) { - this.storePath = getStorePath(options.name, options.extension); - this.defaultSchema = options.defaultSchema ?? {}; + constructor(config: HafConfig) { + this.storePath = getStorePath(config.name, config.extension); + this.defaultSchema = config.defaultSchema ?? {}; this.initializeStore(); }