Skip to content

Commit

Permalink
refactor(last-usage): rename some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zaldih committed Nov 4, 2022
1 parent 5cfba79 commit ace3e01
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,9 @@ export class Controller {
return;
}
const parentFolder = path.join(nodeFolder.path, '../');
const result = await this.fileService.getProjectLastUsage(parentFolder);
const result = await this.fileService.getRecentModificationInDir(
parentFolder,
);
nodeFolder.modificationTime = result;
}),
tap(() => this.finishFolderStats()),
Expand Down
7 changes: 3 additions & 4 deletions src/interfaces/file-service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ export interface IFileService {
getFileContent(path: string): string;
isSafeToDelete(path: string, targetFolder: string): boolean;
isDangerous(path: string): boolean;
getFileList(path: string): any;
getProjectLastUsage(path: string): Promise<number>;
getFileList(dirname: string): Promise<IFileList[]>;
getRecentModificationInDir(path: string): Promise<number>;
getFileStatsInDir(dirname: string): Promise<IFileStat[]>;
}

export interface IFileList {
export interface IFileStat {
path: string;
modificationTime: number;
}
17 changes: 9 additions & 8 deletions src/services/files.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
IFileList,
IFileStat,
IFileService,
IListDirParams,
} from '../interfaces/index.js';
Expand Down Expand Up @@ -49,16 +49,16 @@ export abstract class FileService implements IFileService {
return hiddenFilePattern.test(path);
}

async getProjectLastUsage(path: string): Promise<number> {
const files = await this.getFileList(path);
async getRecentModificationInDir(path: string): Promise<number> {
const files = await this.getFileStatsInDir(path);
const sorted = files.sort(
(a, b) => b.modificationTime - a.modificationTime,
);
return sorted[0]?.modificationTime || null;
}

async getFileList(dirname: string): Promise<IFileList[]> {
let files: IFileList[] = [];
async getFileStatsInDir(dirname: string): Promise<IFileStat[]> {
let files: IFileStat[] = [];
const items = await readdir(dirname, { withFileTypes: true });

for (const item of items) {
Expand All @@ -67,12 +67,13 @@ export abstract class FileService implements IFileService {
if (item.name === 'node_modules') continue;
files = [
...files,
...(await this.getFileList(`${dirname}/${item.name}`)),
...(await this.getFileStatsInDir(`${dirname}/${item.name}`)),
];
} else {
const path = `${dirname}/${item.name}`;
const lastModify = (await stat(path)).mtime;
files.push({ path, modificationTime: lastModify.getTime() / 1000 });
const fileStat = await stat(path);

files.push({ path, modificationTime: fileStat.mtimeMs / 1000 });
}
} catch (error) {}
}
Expand Down

0 comments on commit ace3e01

Please sign in to comment.