From ace3e01dc8a13f58c56d9a0e122b5367ed2904cc Mon Sep 17 00:00:00 2001 From: zaldih Date: Fri, 4 Nov 2022 17:41:53 +0100 Subject: [PATCH] refactor(last-usage): rename some methods --- src/controller.ts | 4 +++- src/interfaces/file-service.interface.ts | 7 +++---- src/services/files.service.ts | 17 +++++++++-------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/controller.ts b/src/controller.ts index 591d2d15..1f68741a 100644 --- a/src/controller.ts +++ b/src/controller.ts @@ -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()), diff --git a/src/interfaces/file-service.interface.ts b/src/interfaces/file-service.interface.ts index d9d5fdc8..9a97f978 100644 --- a/src/interfaces/file-service.interface.ts +++ b/src/interfaces/file-service.interface.ts @@ -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; - getFileList(dirname: string): Promise; + getRecentModificationInDir(path: string): Promise; + getFileStatsInDir(dirname: string): Promise; } -export interface IFileList { +export interface IFileStat { path: string; modificationTime: number; } diff --git a/src/services/files.service.ts b/src/services/files.service.ts index 5c2997a6..27cd14e9 100644 --- a/src/services/files.service.ts +++ b/src/services/files.service.ts @@ -1,5 +1,5 @@ import { - IFileList, + IFileStat, IFileService, IListDirParams, } from '../interfaces/index.js'; @@ -49,16 +49,16 @@ export abstract class FileService implements IFileService { return hiddenFilePattern.test(path); } - async getProjectLastUsage(path: string): Promise { - const files = await this.getFileList(path); + async getRecentModificationInDir(path: string): Promise { + 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 { - let files: IFileList[] = []; + async getFileStatsInDir(dirname: string): Promise { + let files: IFileStat[] = []; const items = await readdir(dirname, { withFileTypes: true }); for (const item of items) { @@ -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) {} }