Skip to content

Commit

Permalink
feat: add finder
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamensuli committed Dec 20, 2023
1 parent 62658f3 commit bb9e7af
Show file tree
Hide file tree
Showing 22 changed files with 980 additions and 22 deletions.
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"types": "src/types/index.d.ts",
"type": "module",
"private": false,
"engines": {
"node": ">=16"
},
"bin": {
"nodefony": "bin/nodefony"
},
Expand All @@ -32,9 +35,11 @@
"eslint-test:fix": "npm run eslint-test -- --fix"
},
"dependencies": {
"@types/mime-types": "2.1.4",
"cli-color": "2.0.3",
"lodash": "4.17.21",
"mime": "4.0.1",
"mime-types": "2.1.35",
"shortid": "2.2.16"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Event extends EventEmitter {
}
}

settingsToListen (localSettings: EventDefaultInterface, context: ContextType ) {
settingsToListen (localSettings: EventDefaultInterface, context?: ContextType ) {
for (const i in localSettings) {
const res = regListenOn.exec(i);
if (!res) {
Expand Down
41 changes: 21 additions & 20 deletions src/FileClass.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import mime from "mime";
import mime from 'mime-types';
import crypto from "node:crypto";
import path from "node:path"
import fs from 'node:fs'
import {extend} from './Tools'

interface FileInterface {
interface FileClassInterface {
path: fs.PathOrFileDescriptor
name: string
ext: string
Expand All @@ -14,20 +14,20 @@ interface FileInterface {
dirName: string
parse: path.ParsedPath
encoding?: string
mimeType? : string | null
extention? : string | null
mimeType? : string | false
extention? : string | false
}


const checkPath = function (myPath: string): string{
const checkPath = function (myPath: string | fs.PathOrFileDescriptor): string {
if (!myPath) {
throw new Error(`Bad path`) ;
}
const abs = path.isAbsolute(myPath);
const abs = path.isAbsolute(<string>myPath);
if (abs) {
return myPath;
return <string>myPath;
}
return path.resolve(process.cwd(), myPath);
return path.resolve(process.cwd(), <string>myPath);
};

const regHidden: RegExp = /^\./;
Expand Down Expand Up @@ -56,14 +56,14 @@ class FileClass {
public name : string
public shortName : string
public ext: string
public mimeType : string | null = null
public mimeType : string | false = false
public encoding : string = "UTF-8"
public extention : string | null = null
public extention : string | false = false
public dirName : string
public match : RegExpExecArray | null = null


constructor (Path: string) {
constructor (Path: string | fs.PathOrFileDescriptor) {
if (Path) {
Path = checkPath(Path);
this.stats = fs.lstatSync(Path);
Expand All @@ -88,14 +88,14 @@ class FileClass {
} else {
throw new Error(`error fileClass Path : ${Path}`);
}
}
}

toString () {
return JSON.stringify(this.toJson(), null, "\n");
}

toJson () : FileInterface {
const obj : FileInterface= {
toJson () : FileClassInterface {
const obj : FileClassInterface= {
path: this.path,
name: this.name,
ext: this.ext,
Expand Down Expand Up @@ -149,15 +149,15 @@ class FileClass {
.digest("hex");
}

getMimeType (name: string) : string | null{
return mime.getType(name || this.name);
getMimeType (name: string) : string | false{
return mime.lookup(name || this.name);
}

getExtension (mimeType: string | null) : string | null{
getExtension (mimeType: string | false) : string | false{
if( mimeType){
return mime.getExtension(mimeType);
return mime.extension(mimeType);
}
return mime.getExtension(<string>this.mimeType);
return mime.extension(<string>this.mimeType);
}

getRealpath (Path: string, options: fs.EncodingOption= {}) {
Expand Down Expand Up @@ -266,4 +266,5 @@ class FileClass {
}
}

export default FileClass;
export default FileClass
export {FileClassInterface}
45 changes: 45 additions & 0 deletions src/finder/File.ts
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;
128 changes: 128 additions & 0 deletions src/finder/FileResult.ts
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
Loading

0 comments on commit bb9e7af

Please sign in to comment.