Skip to content

Commit

Permalink
feat(core): FilenameOption -> GetConfigOptions and `DeleteStoreOp…
Browse files Browse the repository at this point in the history
…tions`

**BREAKING CHANGE**: `FilenameOption` is removed in favor of `GetConfigOptions` and `DeleteStoreOptions`.
  • Loading branch information
jjangga0214 committed Aug 21, 2022
1 parent 23d7ed1 commit b026892
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/core-0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@haetae/core': patch
---

**BREAKING CHANGE**: `FilenameOption` is removed in favor of `GetConfigOptions` and `DeleteStoreOptions`.
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
- .editorconfig
- commitlint.config.js
- license
- docs/urls.json # By `stefanzweifel/git-auto-commit-action`, the file would be pushed alone.
- docs/urls.json
pull_request:
paths-ignore:
- '.devbots/**'
Expand All @@ -20,7 +20,7 @@ on:
- .editorconfig
- commitlint.config.js
- license
- docs/urls.json # By `stefanzweifel/git-auto-commit-action`, the file would be pushed alone.
- docs/urls.json

# for `concurrency`, release should be an independent workflow. (e.g. from test, lint, etc)
concurrency: ${{ github.workflow }}-${{ github.ref }}
Expand Down Expand Up @@ -96,4 +96,4 @@ jobs:
with:
commit_message: 'docs: add url'
env:
HUSKY: 0 # For stefanzweifel/git-auto-commit-action to commit, let's ignore husky
HUSKY: 0 # To commit, let's ignore husky
33 changes: 22 additions & 11 deletions docs/pages/apis/haetae-core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,13 @@ The return value is a normalized result of user-given config.
`configure(obj){:ts}` is eqaul to `configure(configure(obj)){:ts}`.
</Callout>

### `FilenameOption`
### `GetConfigOptions`

An argument interface of function [`getConfig`](#getconfig) and [`deleteStore`](#deletestore).

```ts
interface FilenameOption {
filename?: PromiseOr<string> // It should be an absolute path
interface GetConfigOptions {
filename?: string
}
```

Expand All @@ -409,7 +409,7 @@ A function to get config object by config file path.

<TokenLinkCode tokens={['GetConfigOptions', 'HaetaeConfig']}>
```ts
<D = unknown, E = unknown>(options?: FilenameOption) => Promise<HaetaeConfig<D, E>>
<D = unknown, E = unknown>(options?: GetConfigOptions) => Promise<HaetaeConfig<D, E>>
```
</TokenLinkCode>

Expand All @@ -433,10 +433,10 @@ Initializes an empty store. It just returns an object. It does not save it as a

An argument interface of function [`getStore`](#getstore).

<TokenLinkCode tokens={['FilenameOption', 'HaetaeStore']}>
<TokenLinkCode tokens={['HaetaeStore']}>
```ts
interface GetStoreOptions<D = unknown, E = unknown>
extends FilenameOption {
interface GetStoreOptions<D = unknown, E = unknown> {
filename?: string
fallback?: ({
filename,
error,
Expand Down Expand Up @@ -678,9 +678,10 @@ A function to add a new record under the given command to store.

An argument interface of function [`saveStore`](#savestore).

<TokenLinkCode tokens={['FilenameOption', 'HaetaeStore']}>
<TokenLinkCode tokens={['HaetaeStore']}>
```ts
interface SaveStoreOptions extends FilenameOption {
interface SaveStoreOptions {
filename?: string
store?: PromiseOr<HaetaeStore>
}
```
Expand All @@ -705,6 +706,16 @@ File IO and Memoization cache clear is done sequentially in synchronous manner,
- `filename?{:ts}` : A file path to write store on. <small>(default: [`getStoreFilename(){:ts}`](#getstorefilename))</small>
- `store?{:ts}` : A store object to save. <small>(default: [`addRecord(){:ts}`](#addrecord))</small>

### `DeleteStoreOptions`

An argument interface of function [`deleteStore`](#deleteStore).

```ts
interface DeleteStoreOptions {
filename?: string
}
```

### `deleteStore`

A function to delete the entire store by removing the store file.<br/>
Expand All @@ -713,9 +724,9 @@ File IO and Memoization cache clear is done sequentially in synchronous manner,

**Type**

<TokenLinkCode tokens={['FilenameOption']}>
<TokenLinkCode tokens={['DeleteStoreOptions']}>
```ts
(options?: FilenameOption) => Promise<void>
(options?: DeleteStoreOptions) => Promise<void>
```
</TokenLinkCode>

Expand Down
38 changes: 12 additions & 26 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,14 @@ export function configure<D = unknown, E = unknown>({
}
}

export interface FilenameOption {
filename?: PromiseOr<string> // It should be an absolute path
export interface GetConfigOptions {
filename?: string
}

/**
* The function will fill in default values if not already configured..
* @memoized
*/
export const getConfig = memoizee(
async <D = unknown, E = unknown>({
filename = getConfigFilename(),
}: FilenameOption = {}): Promise<HaetaeConfig<D, E>> => {
}: GetConfigOptions = {}): Promise<HaetaeConfig<D, E>> => {
// eslint-disable-next-line @typescript-eslint/naming-convention
let _filename = await filename
_filename = upath.normalize(_filename)
Expand Down Expand Up @@ -269,8 +265,8 @@ export function initNewStore<D = unknown, E = unknown>(): HaetaeStore<D, E> {
return { version: pkg.version.value, commands: {} }
}

export interface GetStoreOptions<D = unknown, E = unknown>
extends FilenameOption {
export interface GetStoreOptions<D = unknown, E = unknown> {
filename?: string
// When there's no store file yet.
fallback?: ({
filename,
Expand All @@ -281,11 +277,6 @@ export interface GetStoreOptions<D = unknown, E = unknown>
}) => PromiseOr<HaetaeStore<D, E>> | never
}

/**
* @throw if the file does not exist
* `config.storeFile` should be absolute path
* @memoized
*/
export const getStore = memoizee(
async <D = unknown, E = unknown>({
filename = getStoreFilename(),
Expand Down Expand Up @@ -325,9 +316,6 @@ export interface CommandFromConfig<D = unknown, E = unknown> {
config?: PromiseOr<HaetaeConfig<D, E>>
}

/**
* @memoized
*/
export const invokeEnv = memoizee(
async <E = unknown>({
command = getCurrentCommand(),
Expand Down Expand Up @@ -361,9 +349,6 @@ export interface GetRecordOptions<D = unknown, E = unknown>
env?: PromiseOr<E | null>
}

/**
* @returns true if those envs are identical, false otherwise.
*/
export function compareEnvs(one: unknown, theOther: unknown): boolean {
let sOne
let sTheOther
Expand Down Expand Up @@ -420,10 +405,6 @@ export interface AddRecordOptions<D = unknown, E = unknown> {
record?: PromiseOr<HaetaeRecord<D, E>>
}

/**
* This creates a new store from previous store
* The term map is coined from map function
*/
export async function addRecord<D = unknown, E = unknown>({
config = getConfig(),
command = getCurrentCommand(),
Expand Down Expand Up @@ -461,7 +442,8 @@ export async function addRecord<D = unknown, E = unknown>({
})
}

export interface SaveStoreOptions extends FilenameOption {
export interface SaveStoreOptions {
filename?: string
store?: PromiseOr<HaetaeStore>
}

Expand All @@ -479,9 +461,13 @@ export async function saveStore({
getStore.clear() // memoization cache clear
}

export interface DeleteStoreOptions {
filename?: string
}

export async function deleteStore({
filename = getStoreFilename(),
}: FilenameOption = {}): Promise<void> {
}: DeleteStoreOptions = {}): Promise<void> {
fs.unlinkSync(await filename)
getStore.clear() // memoization cache clear
}

0 comments on commit b026892

Please sign in to comment.