Skip to content

Commit

Permalink
feat: dx improvements, refactors (#29)
Browse files Browse the repository at this point in the history
* feat: add dev command

* feat: add test:watch command

* refactor: rename upsertSchema to initializeStore

* refactor: rename configPath references to storePath

* refactor: make defaultSchema private class property

* refactor: get rid of defaultOptions

* refactor: rename Options interface to HafConfig
  • Loading branch information
BatuhanW committed Dec 29, 2023
1 parent 0cbc604 commit fe8f535
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"dist/**/*"
],
"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",
Expand Down
14 changes: 7 additions & 7 deletions spec/get-config-path.spec.ts → spec/get-store-path.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');
});
Expand All @@ -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');
});
Expand All @@ -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');
});
Expand All @@ -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');
});
Expand Down
7 changes: 4 additions & 3 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/get-config-path.ts → src/get-store-path.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}
Expand Down
38 changes: 16 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
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<Schema> {
interface HafConfig<Schema> {
name: string;
extension?: string;
defaultSchema?: Partial<Schema>;
}

class Haf<Schema, FlattenedSchema = FlattenedWithDotNotation<Schema>> {
readonly #options: Readonly<Options<Schema>>;
private configPath: string;
private storePath: string;
private defaultSchema: Partial<Schema>;

readonly #defaultOptions: Readonly<Options<Schema>> = {
name: 'haf',
extension: '',
defaultSchema: {},
};
constructor(config: HafConfig<Schema>) {
this.storePath = getStorePath(config.name, config.extension);
this.defaultSchema = config.defaultSchema ?? {};

constructor(options: Options<Schema>) {
this.#options = Object.assign(this.#defaultOptions, options);
this.configPath = getConfigPath(this.#options.name, this.#options.extension);

this.upsertSchema();
this.initializeStore();
}

private upsertSchema() {
if (pathExistsSync(this.configPath)) return;
private initializeStore() {
if (pathExistsSync(this.storePath)) return;

writeJsonSync(this.configPath, this.#options.defaultSchema);
writeJsonSync(this.storePath, this.defaultSchema);
}

get store(): Schema | Partial<Schema> {
return readJsonSync(this.configPath);
return readJsonSync(this.storePath);
}

set store(schema: Schema | Partial<Schema>) {
writeJsonSync(this.configPath, schema);
writeJsonSync(this.storePath, schema);
}

get<Path extends StringKeysOf<FlattenedSchema>>(path: Path): FlattenedSchema[Path] {
Expand Down Expand Up @@ -65,18 +59,18 @@ class Haf<Schema, FlattenedSchema = FlattenedWithDotNotation<Schema>> {

reset(path?: StringKeysOf<FlattenedSchema>): 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);
}

nuke(): void {
removeSync(this.configPath);
removeSync(this.storePath);
}

private _get<Path extends StringKeysOf<FlattenedSchema>, Returns = FlattenedSchema[Path]>(
Expand Down

0 comments on commit fe8f535

Please sign in to comment.