Skip to content

Commit 82f60a1

Browse files
committed
feat: add getStat method.
1 parent 1d04d65 commit 82f60a1

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export interface IFileDirStat extends Partial<fs.Stats> {
150150
declare type Callback = (filepath: string, stat: IFileDirStat) => void;
151151
export default function recursiveReaddirFiles(rootPath: string, options?: RecursiveReaddirFilesOptions, callback?: Callback): Promise<IFileDirStat[]>;
152152
export { recursiveReaddirFiles };
153+
export declare const getStat: (filepath: string) => Promise<IFileDirStat>;
153154
/**
154155
* Get ext
155156
* @param {String} filePath `/a/b.jpg` => `jpg`

src/index.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference types="jest" />
22

33
import path from 'path';
4-
import recursiveReaddirFiles from './';
4+
import recursiveReaddirFiles, { getStat } from './';
55

66
it('ignored test case', async () => {
77
const files = await recursiveReaddirFiles(process.cwd(), {
@@ -144,3 +144,10 @@ it('filter options test case', async () => {
144144
expect(files.length).toBe(1);
145145
expect(files[0].name).toEqual('README.md');
146146
});
147+
148+
it('getStat test case', async () => {
149+
const stat = await getStat(path.resolve(process.cwd(), 'package.json'));
150+
expect(stat.ext).toEqual('json');
151+
expect(stat.name).toEqual('package.json');
152+
expect(stat.path.endsWith('package.json')).toBeTruthy();
153+
});

src/index.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,20 @@ async function getFiles(
7575
});
7676
if (callback) {
7777
fileDir.map(async (item: IFileDirStat) => {
78-
const stat = (await fs.promises.stat(item.path)) as IFileDirStat;
79-
stat.ext = '';
78+
const stat = await getStat(item.path);
8079
if (stat.isDirectory()) {
8180
getFiles(item.path, options, [], callback);
82-
} else if (stat.isFile()) {
83-
stat.ext = getExt(item.path);
84-
stat.name = item.name;
85-
stat.path = item.path;
8681
}
8782
callback(item.path, stat);
8883
});
8984
} else {
9085
await Promise.all(
9186
fileDir.map(async (item: IFileDirStat) => {
92-
const stat = (await fs.promises.stat(item.path)) as IFileDirStat;
93-
// item.size = stat.size;
94-
stat.ext = '';
87+
const stat = await getStat(item.path);
9588
if (stat.isDirectory()) {
9689
const arr = await getFiles(item.path, options, []);
9790
files = files.concat(arr);
9891
} else if (stat.isFile()) {
99-
stat.ext = getExt(item.path);
100-
stat.name = item.name;
101-
stat.path = item.path;
10292
files.push(stat);
10393
}
10494
}),
@@ -112,6 +102,17 @@ async function getFiles(
112102
});
113103
}
114104

105+
export const getStat = async (filepath: string): Promise<IFileDirStat> => {
106+
const stat = (await fs.promises.stat(filepath)) as IFileDirStat;
107+
stat.ext = '';
108+
if (stat.isFile()) {
109+
stat.ext = getExt(filepath);
110+
stat.name = path.basename(filepath);
111+
stat.path = path.resolve(filepath);
112+
}
113+
return stat;
114+
};
115+
115116
/**
116117
* Get ext
117118
* @param {String} filePath `/a/b.jpg` => `jpg`

0 commit comments

Comments
 (0)