From 675666552bd2cfd1f473bda7dbd5726d6c6eaede Mon Sep 17 00:00:00 2001 From: Ryuu Mitsuki Date: Sun, 21 Apr 2024 17:20:10 +0700 Subject: [PATCH 1/2] refactor: Reorganize and improve types declaration 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. --- src/index.ts | 9 ++++++++ src/lsfnd.ts | 15 +++++--------- tsconfig.production.json | 3 ++- types/index.d.ts | 45 +++++++++++++++++++++++++++------------- 4 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/index.ts b/src/index.ts index 28b617f..06775ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,3 +10,12 @@ export * from './lsTypes'; export * from './lsfnd'; +export type { + LsTypes, + LsTypesInterface, + LsTypesKeys, + LsTypesValues, + LsOptions, + LsEntries, + LsResult +} from '../types'; diff --git a/src/lsfnd.ts b/src/lsfnd.ts index 5dd7755..0efcd8c 100644 --- a/src/lsfnd.ts +++ b/src/lsfnd.ts @@ -19,8 +19,7 @@ import type { LsEntries, LsResult, LsOptions, - LsTypesKeys, - LsTypesValues + LsTypes } from '../types'; type Unpack = A extends Array<(infer U)> ? U : A; @@ -82,7 +81,7 @@ function fileUrlToPath(url: URL | string): string { * @internal */ function checkType( - type: lsTypes | LsTypesKeys | LsTypesValues | N, + type: LsTypes | N, validTypes: Array<(string | number | N)> ): void { function joinAll(arr: (typeof validTypes), delim: string): string { @@ -186,11 +185,7 @@ function checkType( export async function ls( dirpath: string | URL, options?: LsOptions | RegExp | undefined, - type?: - | lsTypes - | LsTypesKeys - | LsTypesValues - | undefined + type?: LsTypes | undefined ): Promise { let absdirpath: string; let match: string | RegExp, @@ -355,7 +350,7 @@ export async function ls( */ export async function lsFiles( dirpath: string | URL, - options?: LsOptions | RegExp + options?: LsOptions | RegExp | undefined ): Promise { return ls(dirpath, options, lsTypes.LS_F); } @@ -426,7 +421,7 @@ export async function lsFiles( */ export async function lsDirs( dirpath: string | URL, - options?: LsOptions | RegExp + options?: LsOptions | RegExp | undefined ): Promise { return ls(dirpath, options, lsTypes.LS_D); } diff --git a/tsconfig.production.json b/tsconfig.production.json index 5f13bbe..1f6b47a 100644 --- a/tsconfig.production.json +++ b/tsconfig.production.json @@ -3,6 +3,7 @@ "compilerOptions": { "strict": true, "alwaysStrict": true, - "removeComments": true + "removeComments": true, + "declaration": true } } diff --git a/types/index.d.ts b/types/index.d.ts index e3cd666..18d7b98 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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) @@ -21,6 +20,7 @@ * @since 0.1.0 */ export declare type LsEntries = Array; + /** * 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, @@ -30,22 +30,23 @@ export declare type LsEntries = Array; 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) @@ -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`) @@ -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, /** @@ -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 +/** {@inheritDoc !lsfnd~lsFiles} */ export declare function lsFiles( dirpath: string | URL, options?: LsOptions | RegExp | undefined ): Promise +/** {@inheritDoc !lsfnd~lsDirs} */ export declare function lsDirs( dirpath: string | URL, options?: LsOptions | RegExp | undefined From 4d48e06fa75ebd6c600f40f7db6a0d0296a5b8a1 Mon Sep 17 00:00:00 2001 From: Ryuu Mitsuki Date: Sun, 21 Apr 2024 17:44:00 +0700 Subject: [PATCH 2/2] refactor(build): Include `lsTypes` module to minify list Added the `lsTypes` module to be minified after get transpiled from TypeScript. Other changes is unrelated changes. --- build.prop.js | 11 ++++++----- scripts/build.ts | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/build.prop.js b/build.prop.js index 229f797..0cdb1d1 100644 --- a/build.prop.js +++ b/build.prop.js @@ -1,13 +1,14 @@ -const { resolve } = require('node:path'); +const { join } = require('node:path'); /** @type {import('./types/build.prop').BuildPropConfig} */ module.exports = { minify: { files: [ 'dist/index.js', - 'dist/lsfnd.js' + 'dist/lsfnd.js', + 'dist/lsTypes.js' ] }, - rootDir: resolve(__dirname), - outDir: resolve('dist'), - tsconfig: 'tsconfig.production.json', + rootDir: __dirname, + outDir: join(__dirname, 'dist'), + tsconfig: 'tsconfig.production.json' }; \ No newline at end of file diff --git a/scripts/build.ts b/scripts/build.ts index b50097c..ba641f6 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -17,8 +17,7 @@ * npx ts-node scripts/build.ts [-ow|--overwrite] [-m|--minify] * ``` * - * Copyright (c) 2024 Ryuu Mitsuki. - * Licensed under the MIT license. + * Copyright (c) 2024 Ryuu Mitsuki. All rights reserved. * * @module scripts/build * @author Ryuu Mitsuki (https://github.com/mitsuki31) @@ -62,6 +61,7 @@ const legalComments: string = ` * Copyright (c) ${new Date().getFullYear()} Ryuu Mitsuki. All rights reserved. * @author Ryuu Mitsuki (https://github.com/mitsuki31) * @license ${pkg.license} + * @see [Homepage](${pkg.homepage}) * @see [Source code](${pkg.repository.url.replace(/^git\+/, '')}) */ `.trimStart();