Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dx improvements, refactors #29

Merged
merged 7 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading