Skip to content

Commit

Permalink
feat: add getStat method.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 25, 2022
1 parent 1d04d65 commit 82f60a1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export interface IFileDirStat extends Partial<fs.Stats> {
declare type Callback = (filepath: string, stat: IFileDirStat) => void;
export default function recursiveReaddirFiles(rootPath: string, options?: RecursiveReaddirFilesOptions, callback?: Callback): Promise<IFileDirStat[]>;
export { recursiveReaddirFiles };
export declare const getStat: (filepath: string) => Promise<IFileDirStat>;
/**
* Get ext
* @param {String} filePath `/a/b.jpg` => `jpg`
Expand Down
9 changes: 8 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="jest" />

import path from 'path';
import recursiveReaddirFiles from './';
import recursiveReaddirFiles, { getStat } from './';

it('ignored test case', async () => {
const files = await recursiveReaddirFiles(process.cwd(), {
Expand Down Expand Up @@ -144,3 +144,10 @@ it('filter options test case', async () => {
expect(files.length).toBe(1);
expect(files[0].name).toEqual('README.md');
});

it('getStat test case', async () => {
const stat = await getStat(path.resolve(process.cwd(), 'package.json'));
expect(stat.ext).toEqual('json');
expect(stat.name).toEqual('package.json');
expect(stat.path.endsWith('package.json')).toBeTruthy();
});
25 changes: 13 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,20 @@ async function getFiles(
});
if (callback) {
fileDir.map(async (item: IFileDirStat) => {
const stat = (await fs.promises.stat(item.path)) as IFileDirStat;
stat.ext = '';
const stat = await getStat(item.path);
if (stat.isDirectory()) {
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);
});
} else {
await Promise.all(
fileDir.map(async (item: IFileDirStat) => {
const stat = (await fs.promises.stat(item.path)) as IFileDirStat;
// item.size = stat.size;
stat.ext = '';
const stat = await getStat(item.path);
if (stat.isDirectory()) {
const arr = await getFiles(item.path, options, []);
files = files.concat(arr);
} else if (stat.isFile()) {
stat.ext = getExt(item.path);
stat.name = item.name;
stat.path = item.path;
files.push(stat);
}
}),
Expand All @@ -112,6 +102,17 @@ async function getFiles(
});
}

export const getStat = async (filepath: string): Promise<IFileDirStat> => {
const stat = (await fs.promises.stat(filepath)) as IFileDirStat;
stat.ext = '';
if (stat.isFile()) {
stat.ext = getExt(filepath);
stat.name = path.basename(filepath);
stat.path = path.resolve(filepath);
}
return stat;
};

/**
* Get ext
* @param {String} filePath `/a/b.jpg` => `jpg`
Expand Down

0 comments on commit 82f60a1

Please sign in to comment.