-
-
Save NullFragment/ccb1cfb5fda14d904f82e2f0ce09136d to your computer and use it in GitHub Desktop.
Rewriting file scans in typescript.
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
import chalk from 'chalk' | |
import {promises as fs} from 'fs' | |
import dir from "node-dir"; | |
const config = { | |
extensions: [ | |
'svelte', | |
'md', | |
'js', | |
'ts'], | |
rename: ['404'], | |
catch: [ | |
'ENOENT', | |
'EEXIST'] | |
} | |
class Logger { | |
static log( | |
message: string, | |
destination: string, | |
logColor: string | |
) { | |
console.log(chalk.dim(new Date().toLocaleTimeString() + ' ') | |
+ chalk.magentaBright.bold('[urara] ') | |
+ chalk[logColor](message + ' ') | |
+ chalk.dim(destination ?? '')) | |
} | |
static error(err: Error) { | |
console.log(chalk.dim(new Date().toLocaleTimeString() | |
+ ' ') | |
+ chalk.redBright.bold('[urara] ') | |
+ chalk.red('error ') | |
+ chalk.dim(err.message)) | |
if(!config.catch.includes(err.name)) { | |
throw err | |
} | |
} | |
} | |
class FileManager { | |
static scanWithNodeDir(sourceDir: string) { | |
const structure = dir.files( | |
'urara', | |
'all', | |
null, | |
{sync: true}) | |
const structureWithDepth = new Map<string, Map<number, Array<string>>>() | |
structureWithDepth.set( | |
'dirs', | |
new Map<number, Array<string>>()) | |
structureWithDepth.set( | |
'files', | |
new Map<number, Array<string>>()) | |
this.addDepthContext( | |
structureWithDepth, | |
structure.dirs, | |
'dirs') | |
this.addDepthContext( | |
structureWithDepth, | |
structure.files, | |
'files') | |
return structureWithDepth | |
} | |
static async scanDirWithDepth( | |
sourceDir: string, | |
depth = 0, | |
currentTree: Map<string, Map<number, Array<string>>> | |
): Promise<Map<string, Map<number, Array<string>>>> { | |
Logger.log( | |
"Currently mapping: " + sourceDir, | |
null, | |
'magenta') | |
if(!currentTree) { | |
currentTree = new Map<string, Map<number, Array<string>>>(); | |
currentTree.set( | |
'dirs', | |
new Map<number, Array<string>>()) | |
currentTree.set( | |
'files', | |
new Map<number, Array<string>>()) | |
} | |
await fs.readdir( | |
sourceDir, | |
{withFileTypes: true}) | |
.then(async files => await Promise.all(files.map(async file => { | |
const entryName = `${sourceDir}/${file.name}` | |
const key = file.isDirectory() | |
? 'dirs' | |
: 'files' | |
const vals = currentTree.get(key) | |
if(vals.has(depth)) { | |
vals.get(depth) | |
.push(entryName) | |
} else { | |
vals.set( | |
depth, | |
[entryName]) | |
} | |
if(file.isDirectory()) { | |
await this.scanDirWithDepth( | |
entryName, | |
depth + 1, | |
currentTree) | |
.then(nextTree => currentTree = new Map([ | |
...Array.from(currentTree.entries()), | |
...Array.from(nextTree.entries())])) | |
} | |
}))) | |
return currentTree; | |
} | |
private static addDepthContext( | |
dirStructure: Map<string, Map<number, Array<string>>>, | |
listToAdd: Array<string>, | |
key: string | |
) { | |
listToAdd.forEach(item => { | |
const depth: number = item | |
.split("\\").length - 2 | |
if(dirStructure.get(key) | |
.has(depth)) { | |
dirStructure.get(key) | |
.get(depth) | |
.push(item) | |
} else { | |
dirStructure.get(key) | |
.set( | |
depth, | |
[item]) | |
} | |
}) | |
} | |
} | |
switch(process.argv[2]) { | |
case 'scan': | |
await FileManager.scanDirWithDepth( | |
"urara", | |
0, | |
null) | |
.then(dirStructure => { | |
let files = 0 | |
let dirs = 0 | |
const blah = dirStructure.get('dirs') | |
const blah2 = dirStructure.get('files') | |
blah.forEach(array => dirs += array.length) | |
blah2.forEach(array => files += array.length) | |
console.log(dirs) | |
console.log(files) | |
}) | |
console.log(FileManager.scanWithNodeDir('urara')) | |
} |
Author
NullFragment
commented
May 17, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment