Skip to content

Commit

Permalink
refactor: Reorganize and improve types declaration
Browse files Browse the repository at this point in the history
This change reorganizes and refactors the types declaration within the `types` module for better maintainability and future development. It includes restructuring the type definitions for specific API parameters.

Additionally, the `types` module is now re-exported in the main package entry (`index.ts`), and the `declaration` option is enabled in the 'tsconfig.production.json' file.

These changes enable the LSFND package to be correctly imported and used in TypeScript projects with the `module` set to `node16`, `nodenext`, `es5`, `esnext`, and others, in addition to `commonjs`, which was supported in previous release.
  • Loading branch information
mitsuki31 committed Apr 21, 2024
1 parent 8c64dc7 commit 6756665
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@

export * from './lsTypes';
export * from './lsfnd';
export type {
LsTypes,
LsTypesInterface,
LsTypesKeys,
LsTypesValues,
LsOptions,
LsEntries,
LsResult
} from '../types';
15 changes: 5 additions & 10 deletions src/lsfnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import type {
LsEntries,
LsResult,
LsOptions,
LsTypesKeys,
LsTypesValues
LsTypes
} from '../types';

type Unpack<A> = A extends Array<(infer U)> ? U : A;
Expand Down Expand Up @@ -82,7 +81,7 @@ function fileUrlToPath(url: URL | string): string {
* @internal
*/
function checkType<N extends null | undefined>(
type: lsTypes | LsTypesKeys | LsTypesValues | N,
type: LsTypes | N,
validTypes: Array<(string | number | N)>
): void {
function joinAll(arr: (typeof validTypes), delim: string): string {
Expand Down Expand Up @@ -186,11 +185,7 @@ function checkType<N extends null | undefined>(
export async function ls(
dirpath: string | URL,
options?: LsOptions | RegExp | undefined,
type?:
| lsTypes
| LsTypesKeys
| LsTypesValues
| undefined
type?: LsTypes | undefined
): Promise<LsResult> {
let absdirpath: string;
let match: string | RegExp,
Expand Down Expand Up @@ -355,7 +350,7 @@ export async function ls(
*/
export async function lsFiles(
dirpath: string | URL,
options?: LsOptions | RegExp
options?: LsOptions | RegExp | undefined
): Promise<LsResult> {
return ls(dirpath, options, lsTypes.LS_F);
}
Expand Down Expand Up @@ -426,7 +421,7 @@ export async function lsFiles(
*/
export async function lsDirs(
dirpath: string | URL,
options?: LsOptions | RegExp
options?: LsOptions | RegExp | undefined
): Promise<LsResult> {
return ls(dirpath, options, lsTypes.LS_D);
}
3 changes: 2 additions & 1 deletion tsconfig.production.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"strict": true,
"alwaysStrict": true,
"removeComments": true
"removeComments": true,
"declaration": true
}
}
45 changes: 31 additions & 14 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* Project: lsfnd (https://github.com/mitsuki31/lsfnd.git)
* Definitions by: Ryuu Mitsuki (https://github.com/mitsuki31)
*
* Copyright (c) 2024 Ryuu Mitsuki.
* Licensed under the MIT license.
* Copyright (c) 2024 Ryuu Mitsuki. All rights reserved.
*
* @module types
* @author Ryuu Mitsuki (https://github.com/mitsuki31)
Expand All @@ -21,6 +20,7 @@
* @since 0.1.0
*/
export declare type LsEntries = Array<string>;

/**
* This type alias represents the possible return values of the `ls*` functions.
* It can be either a {@link LsEntries} array containing the list of file paths,
Expand All @@ -30,22 +30,23 @@ export declare type LsEntries = Array<string>;
export declare type LsResult = LsEntries | null;

/**
* An enum representing different file types.
* @since 0.1.0
* @see {@link lsfnd!~lsTypes lsfnd.lsTypes}
* @see {@link LsTypes}
* A combination union types containing all possible values used to specify the
* returned results on {@link !lsfnd~ls ls} function.
* @since 1.0.0
*/
export declare const lsTypes: LsTypes;
export declare type LsTypes = LsTypesKeys | LsTypesValues;

/**
* Type representing all possible keys of the {@link lsTypes} enum.
* @since 0.1.0
* @see {@link LsTypes}
* @see {@link LsTypesInterface}
*/
export declare type LsTypesKeys = keyof LsTypes;
export declare type LsTypesKeys = keyof LsTypesInterface;

/**
* Type representing all possible values of the {@link lsTypes} enum.
* @since 0.1.0
* @see {@link LsTypes}
* @see {@link LsTypesInterface}
*/
export declare type LsTypesValues =
| 0b00 // 0 (interpreted the same as LS_A | 1)
Expand All @@ -60,7 +61,7 @@ export declare type LsTypesValues =
* @interface
* @since 0.1.0
*/
export declare interface LsTypes {
export declare interface LsTypesInterface {
/**
* Represents an option to include all file types.
* @defaultValue `0b01 << 0b00` (`0b01` | `0o01` | `0x01` | `1`)
Expand All @@ -87,12 +88,12 @@ export declare interface LsTypes {
export declare interface LsOptions {
/**
* Specifies the character encoding to be used when reading a directory.
* @defaultValue Defaults to `'utf8'` if not provided.
* @defaultValue `'utf8'`
*/
encoding?: BufferEncoding | undefined,
/**
* A boolean flag indicating whether to include subdirectories in the listing.
* @defaultValue Defaults to `false` if not provided.
* @defaultValue `false`
*/
recursive?: boolean | undefined,
/**
Expand All @@ -112,17 +113,33 @@ export declare interface LsOptions {

// ====== APIs ===== //

/**
* {@inheritDoc !lsTypes~lsTypes}
*
* @see For more details, refer to {@link !lsTypes~lsTypes lsTypes} enum documentation.
*/
export declare const lsTypes: Record<
LsTypesKeys,
LsTypesValues
> & Record<
LsTypesValues,
LsTypesKeys
>;

/** {@inheritDoc !lsfnd~ls} */
export declare function ls(
dirpath: string | URL,
options?: LsOptions | RegExp | undefined,
type?: LsTypes | LsTypesKeys | LsTypesValues | undefined
type?: LsTypes | undefined
): Promise<LsResult>

/** {@inheritDoc !lsfnd~lsFiles} */
export declare function lsFiles(
dirpath: string | URL,
options?: LsOptions | RegExp | undefined
): Promise<LsResult>

/** {@inheritDoc !lsfnd~lsDirs} */
export declare function lsDirs(
dirpath: string | URL,
options?: LsOptions | RegExp | undefined
Expand Down

0 comments on commit 6756665

Please sign in to comment.