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

refactor(types): Enhance typings structure and module compatibility #5

Merged
merged 2 commits into from
Apr 21, 2024
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
11 changes: 6 additions & 5 deletions build.prop.js
Original file line number Diff line number Diff line change
@@ -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'
};
4 changes: 2 additions & 2 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
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
Loading