Skip to content

Commit

Permalink
refactor: Modify callback handle.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 19, 2022
1 parent f5e9904 commit 515d1d1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export interface IFileDirStat extends Partial<fs.Stats> {
*/
ext?: string;
}
declare type Callback = (filepath: string, stat: IFileDirStat, childs: IFileDirStat[]) => void;
declare type Callback = (filepath: string, stat: IFileDirStat) => void;
export default function recursiveReaddirFiles(rootPath: string, options?: RecursiveReaddirFilesOptions, callback?: Callback): Promise<IFileDirStat[]>;
export { recursiveReaddirFiles };
/**
Expand Down
7 changes: 2 additions & 5 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,10 @@ it('Recursive Readdir Files, callback', async () => {
exclude: /(\.json)$/,
include: /(package\.json)$/,
},
(file, stat, childs) => {
(file, stat) => {
expect(typeof file === 'string').toBeTruthy();
expect(typeof stat.isFile === 'function').toBeTruthy();
expect(typeof stat.isDirectory === 'function').toBeTruthy();
expect(Array.isArray(childs)).toBeTruthy();
if (stat.isDirectory() && file.endsWith('src')) {
expect(childs.length).toEqual(2);
}
},
);
expect(files.length).toBe(0);
Expand Down
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface IFileDirStat extends Partial<fs.Stats> {
ext?: string;
}

type Callback = (filepath: string, stat: IFileDirStat, childs: IFileDirStat[]) => void;
type Callback = (filepath: string, stat: IFileDirStat) => void;

export default function recursiveReaddirFiles(
rootPath: string,
Expand Down Expand Up @@ -77,16 +77,14 @@ async function getFiles(
fileDir.map(async (item: IFileDirStat) => {
const stat = (await fs.promises.stat(item.path)) as IFileDirStat;
stat.ext = '';
let childs: IFileDirStat[] = [];
if (stat.isDirectory()) {
const arr = await getFiles(item.path, options, []);
childs = childs.concat(arr);
getFiles(item.path, options, [], callback);
} else if (stat.isFile()) {
stat.ext = getExt(item.path);
stat.name = item.name;
stat.path = item.path;
}
callback(item.path, stat, childs);
callback(item.path, stat);
});
} else {
await Promise.all(
Expand Down

0 comments on commit 515d1d1

Please sign in to comment.