-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure dir, file and add a list class (#5)
* implement a list class to handle list returns and add new functions * write, fix and extend test and fix code * update package-lock.json
- Loading branch information
1 parent
ed39004
commit 7dbed5d
Showing
14 changed files
with
778 additions
and
295 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,67 @@ | ||
import * as fs from 'node:fs/promises'; | ||
import type { Dirent } from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import { File } from './file.js'; | ||
import { List } from './list.js'; | ||
|
||
|
||
type PickMatching<T, V> = | ||
{ [K in keyof T as T[K] extends V ? K : never]: T[K] } | ||
type DirentMethodsName = keyof PickMatching<Dirent, () => unknown>; | ||
type ReturnTypeTuble<T extends Array<DirentMethodsName>> = { | ||
[K in keyof T]: ReturnType<Dirent[T[K]]> | ||
} | ||
|
||
export class Directory { | ||
|
||
constructor( | ||
protected path: string | ||
protected _path: string | ||
) { | ||
fs.access(path); | ||
fs.access(_path); | ||
} | ||
|
||
async list(subDir?: string): Promise<string[]> | ||
async list<T extends DirentMethodsName[], U = [string, ...ReturnTypeTuble<T>][]>(subDir?: string, ...params: T): Promise<U> | ||
async list<T extends DirentMethodsName[], U = [string, ...ReturnTypeTuble<T>][]>(subDir?: string, ...params: T): Promise<U | string[]> { | ||
if(params.length === 0) { | ||
const dirs = await fs.readdir(path.join(this.path, subDir ?? '')); | ||
return dirs; | ||
} | ||
get path() { | ||
return this._path; | ||
} | ||
|
||
subdir(subDir: string) { | ||
return new Directory(path.join(this.path, subDir)); | ||
} | ||
|
||
async list(): Promise<List> { | ||
|
||
const dirs = await fs.readdir(path.join(this.path, subDir ?? ''), {withFileTypes: true}); | ||
const paths = await fs.readdir(this.path, {withFileTypes: true}); | ||
|
||
return dirs.map((dir: Dirent) => { | ||
const p = params.map((param) => { | ||
return dir[param](); | ||
}); | ||
return [dir.name, ...p]; | ||
}) as U; | ||
|
||
return new List(this, paths); | ||
} | ||
|
||
relativePath(dir: Directory): string { | ||
return path.relative(this.path, dir.path); | ||
} | ||
|
||
|
||
async listFiles(subDir?: string, recursive: boolean = false): Promise<string[]> { | ||
const pathContent = await this.list(subDir, 'isDirectory'); | ||
file(name: string) { | ||
return new File(path.join(this.path, name)); | ||
} | ||
|
||
|
||
return await pathContent.reduce<Promise<string[]>>( | ||
async (acc, [name, isDirecotry]) => { | ||
const data = await acc; | ||
if (recursive && isDirecotry) { | ||
const p = path.join(subDir ?? '', name); | ||
const list = await this.listFiles(p, recursive); | ||
const subDirFiles = list.map((subName) => path.join(p, subName)); | ||
return data.concat(subDirFiles); | ||
} else if (!isDirecotry) { | ||
data.push(name); | ||
return data; | ||
} | ||
protected async filesRecursion(list: List): Promise<List> { | ||
|
||
return data; | ||
}, Promise.resolve(Array<string>()) | ||
); | ||
const dirList = list.filterByType('isDirectory'); | ||
const fileList = list.filterByType('isFile'); | ||
|
||
for(const el of dirList) { | ||
if(el instanceof Directory) { | ||
const subList = await el.list(); | ||
fileList.add(await this.filesRecursion(subList)); | ||
} | ||
} | ||
|
||
return fileList; | ||
} | ||
|
||
async files(recursive: boolean = false) { | ||
const paths = await this.listFiles('', recursive); | ||
return paths.map((file) => new File(path.join(this.path, file))); | ||
async files(recursive: boolean = false): Promise<List> { | ||
const list = await this.list(); | ||
|
||
if(recursive) { | ||
return this.filesRecursion(list); | ||
} else { | ||
const fileList = list.filterByType('isFile'); | ||
return fileList; | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.