-
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.
- Loading branch information
1 parent
62658f3
commit bb9e7af
Showing
22 changed files
with
980 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import FileClass, {FileClassInterface} from '../FileClass' | ||
import FileResult from './FileResult' | ||
import fs from 'node:fs' | ||
|
||
|
||
interface FileInterface extends FileClassInterface { | ||
children?: string; | ||
} | ||
|
||
class File extends FileClass { | ||
|
||
public parent : File | null = null | ||
public children : FileResult = new FileResult() | ||
|
||
constructor (path: string | fs.PathOrFileDescriptor, parent :File | null = null) { | ||
super(path); | ||
this.parent = parent; | ||
} | ||
|
||
get length () : number{ | ||
return this.children.length; | ||
} | ||
|
||
toJson () : FileInterface{ | ||
const obj : FileInterface= { | ||
path: this.path, | ||
name: this.name, | ||
ext: this.ext, | ||
shortName: this.shortName, | ||
type: this.type, | ||
stats: this.stats, | ||
dirName: this.dirName, | ||
parse: this.parse | ||
}; | ||
if (this.type === "File") { | ||
obj.encoding = this.encoding; | ||
obj.mimeType = this.mimeType; | ||
obj.extention = this.extention; | ||
} | ||
obj.children = this.children.toJson(); | ||
return obj; | ||
} | ||
} | ||
|
||
export default File; |
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import Result from './Result' | ||
import File from './File' | ||
|
||
|
||
class FileResult extends Result { | ||
|
||
constructor (res?: File[] | undefined) { | ||
super(res); | ||
Array.prototype.find | ||
} | ||
|
||
toString () : string { | ||
let txt = ""; | ||
for (let index = 0; index < this.length; index++) { | ||
const info = this[index]; | ||
txt += `${info.name}\n`; | ||
} | ||
return txt; | ||
} | ||
|
||
toJson (json :any[]= []) : string{ | ||
for (let index = 0; index < this.length; index++) { | ||
const info :File = this[index]; | ||
switch (info.type) { | ||
case "File": | ||
json.push(info.toJson()); | ||
break; | ||
case "symbolicLink": | ||
case "Directory":{ | ||
const dir = info.toJson(); | ||
if (info.children) { | ||
dir.children = info.children.toJson(); | ||
} | ||
json.push(dir); | ||
break; | ||
} | ||
} | ||
} | ||
return JSON.stringify(json); | ||
} | ||
|
||
uniq () { | ||
return this; | ||
} | ||
|
||
find <S>(predicate: (value: any, index: number, obj: any[]) => value is S, result : FileResult = new FileResult()) : FileResult { | ||
for (let index = 0; index < this.length; index++) { | ||
const info: File = this[index]; | ||
const unknownType : unknown= predicate | ||
const match = info.matchName(<string>unknownType); | ||
if (match) { | ||
result.push(info); | ||
} | ||
info.children.find(predicate, result); | ||
} | ||
return result.uniq(); | ||
} | ||
|
||
getDirectories (result :FileResult = new FileResult()) :FileResult { | ||
for (let index = 0; index < this.length; index++) { | ||
const info: File = this[index]; | ||
switch (info.type) { | ||
case "Directory": | ||
result.push(info); | ||
info.children.getDirectories(result); | ||
break; | ||
case "symbolicLink": | ||
info.children.getDirectories(result); | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
getFiles (result :FileResult = new FileResult()) : FileResult{ | ||
for (let index = 0; index < this.length; index++) { | ||
const info :File= this[index]; | ||
switch (info.type) { | ||
case "File": | ||
result.push(info); | ||
break; | ||
case "symbolicLink": | ||
case "Directory": | ||
info.children.getFiles(result); | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
sortByName (result : FileResult = new FileResult()) : FileResult{ | ||
const res = this.sort((a, b) => { | ||
if (a.name.toString() > b.name.toString()) { | ||
return 1; | ||
} | ||
if (a.name.toString() < b.name.toString()) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
if (res) { | ||
const unknownResult: unknown = result.concat(res); | ||
return <FileResult>unknownResult; | ||
} | ||
return this; | ||
} | ||
|
||
sortByType (result = new FileResult()) : FileResult{ | ||
const res = this.sort((a, b) => { | ||
if (a.type.toString() > b.type.toString()) { | ||
return 1; | ||
} | ||
if (a.type.toString() < b.type.toString()) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
if (res) { | ||
const unknownResult: unknown = result.concat(res); | ||
return <FileResult>unknownResult; | ||
} | ||
return this; | ||
} | ||
} | ||
|
||
export default FileResult |
Oops, something went wrong.