Skip to content

Commit

Permalink
feat: add typings to extend nodes on file or dir
Browse files Browse the repository at this point in the history
Signed-off-by: euberdeveloper <euberdeveloper@gmail.com>
  • Loading branch information
euberdeveloper committed Dec 7, 2023
1 parent ae0fe35 commit 3e4418f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions source/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export enum Type {
/**
* Callback used by [[scan]] when a file or dir is encountered
*/
export type Callback = (dirTree: Dree, stat: Stats) => void;
export type Callback<Node extends Dree = Dree> = (dirTree: Node, stat: Stats) => void;
/**
* Callback used by [[scanAsync]] when a file or dir is encountered
*/
export type CallbackAsync = (dirTree: Dree, stat: Stats) => void | Promise<void>;
export type CallbackAsync<Node extends Dree = Dree> = (dirTree: Node, stat: Stats) => void | Promise<void>;


/**
Expand Down Expand Up @@ -390,7 +390,7 @@ function sortDreeNodes(dreeNodes: Dree[], sortOption: boolean | SortMethodPredef
}
}

function _scan(root: string, path: string, depth: number, options: ScanOptions, onFile?: Callback, onDir?: Callback): Dree | null {
function _scan<Node extends Dree = Dree>(root: string, path: string, depth: number, options: ScanOptions, onFile?: Callback<Node>, onDir?: Callback<Node>): Node | null {
if (options.depth !== undefined && depth > options.depth) {
return null;
}
Expand Down Expand Up @@ -448,21 +448,21 @@ function _scan(root: string, path: string, depth: number, options: ScanOptions,
hash.update(name);
}

const dirTree: Dree = {
const dirTree = {
name: name,
path: options.normalize ? path.replace(/\\/g, '/') : path,
relativePath: options.normalize ? relativePath.replace(/\\/g, '/') : relativePath,
type: type,
isSymbolicLink: symbolicLink,
stat: options.followLinks ? stat : lstat
};
} as Node;
if (!options.stat) {
delete dirTree.stat;
}

switch (type) {
case Type.DIRECTORY:
const children: Dree[] = [];
const children: Node[] = [];
let files: string[];
if (options.followLinks || !symbolicLink) {
try {
Expand All @@ -482,7 +482,7 @@ function _scan(root: string, path: string, depth: number, options: ScanOptions,
dirTree.isEmpty = !files.length
}
files.forEach(file => {
const child: Dree | null = _scan(root, resolve(path, file), depth + 1, options, onFile, onDir);
const child: Node | null = _scan<Node>(root, resolve(path, file), depth + 1, options, onFile, onDir);
if (child !== null) {
children.push(child);
}
Expand Down Expand Up @@ -569,7 +569,7 @@ function _scan(root: string, path: string, depth: number, options: ScanOptions,
return dirTree;
}

async function _scanAsync(root: string, path: string, depth: number, options: ScanOptions, onFile?: CallbackAsync, onDir?: CallbackAsync): Promise<Dree | null> {
async function _scanAsync<Node extends Dree = Dree>(root: string, path: string, depth: number, options: ScanOptions, onFile?: CallbackAsync<Node>, onDir?: CallbackAsync<Node>): Promise<Node | null> {

if (options.depth !== undefined && depth > options.depth) {
return null;
Expand Down Expand Up @@ -628,14 +628,14 @@ async function _scanAsync(root: string, path: string, depth: number, options: Sc
hash.update(name);
}

const dirTree: Dree = {
const dirTree = {
name: name,
path: options.normalize ? path.replace(/\\/g, '/') : path,
relativePath: options.normalize ? relativePath.replace(/\\/g, '/') : relativePath,
type: type,
isSymbolicLink: symbolicLink,
stat: options.followLinks ? stat : lstat
};
} as Node;
if (!options.stat) {
delete dirTree.stat;
}
Expand All @@ -662,7 +662,7 @@ async function _scanAsync(root: string, path: string, depth: number, options: Sc
dirTree.isEmpty = !files.length
}
children = await Promise.all(files.map(async file => {
const child: Dree | null = await _scanAsync(root, resolve(path, file), depth + 1, options, onFile, onDir);
const child: Node | null = await _scanAsync<Node>(root, resolve(path, file), depth + 1, options, onFile, onDir);
return child;
}));
children = children.filter(ch => ch !== null);
Expand Down Expand Up @@ -971,10 +971,10 @@ async function _parseTreeAsync(children: Dree[], prefix: string, options: ParseO
* @param {function} onDir A function called when a dir is added - has the tree object and its stat as parameters
* @return {object} The directory tree as a Dree object
*/
export function scan(path: string, options?: ScanOptions, onFile?: Callback, onDir?: Callback): Dree {
export function scan<Node extends Dree = Dree>(path: string, options?: ScanOptions, onFile?: Callback<Node>, onDir?: Callback<Node>): Node {
const root = resolve(path);
const opt = mergeScanOptions(options);
const result = _scan(root, root, 0, opt, onFile, onDir) as Dree;
const result = _scan<Node>(root, root, 0, opt, onFile, onDir);
if (result) {
result.sizeInBytes = opt.sizeInBytes ? result.sizeInBytes : undefined;
}
Expand All @@ -989,10 +989,10 @@ export function scan(path: string, options?: ScanOptions, onFile?: Callback, onD
* @param {function} onDir A function called when a dir is added - has the tree object and its stat as parameters
* @return {Promise<object>} A promise to the directory tree as a Dree object
*/
export async function scanAsync(path: string, options?: ScanOptions, onFile?: CallbackAsync, onDir?: CallbackAsync): Promise<Dree> {
export async function scanAsync<Node extends Dree = Dree>(path: string, options?: ScanOptions, onFile?: CallbackAsync<Node>, onDir?: CallbackAsync<Node>): Promise<Node> {
const root = resolve(path);
const opt = mergeScanOptions(options);
const result = await _scanAsync(root, root, 0, opt, onFile, onDir) as Dree;
const result = await _scanAsync<Node>(root, root, 0, opt, onFile, onDir);
if (result) {
result.sizeInBytes = opt.sizeInBytes ? result.sizeInBytes : undefined;
}
Expand Down

0 comments on commit 3e4418f

Please sign in to comment.