add recursive function to get subfolders

Co-authored-by: Avior <github@avior.me>
This commit is contained in:
2023-02-03 13:38:01 +01:00
parent 99c872fe97
commit 9e1c476fb3
44 changed files with 64 additions and 33 deletions

View File

@ -3,13 +3,38 @@
import Jimp from 'jimp'
import fs from 'fs/promises'
import crypto from 'crypto'
import path from 'path'
;(async (folder: string) => {
/**
*
*
* @param path the folder to search
*
* @return the list of files included in this folder/subfolders
*/
async function filesList(Path: string): Promise<Array<string>> {
const files: Array<string> = []
const subFolders = await fs.readdir(Path)
for await (const subFolder of subFolders)
{
const stats = await fs.stat(Path + '/' + subFolder)
if (stats.isDirectory()) {
files.push(...await filesList(Path + '/' + subFolder))
} else {
files.push(Path + '/' + subFolder)
}
const files = await fs.readdir(folder)
}
return files
}
(async (dirPath: string) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const files = await filesList(dirPath)
console.log(files)
// Filtre cool
const imageFiles = files.filter((file) => !file.startsWith('.'))
const imageFiles = (await files).filter((file) => file.endsWith('.jfif'))
const data = (await fs.readFile('./output.json')).toString('utf8')
let json: Record<string, string> = {}
@ -28,8 +53,7 @@ import crypto from 'crypto'
const hash = crypto.createHash('sha256')
hash.setEncoding('hex')
// the text that you want to hash
const readI = await fs.readFile(`${folder}/${file}`)
const readI = await fs.readFile(`${file}`)
hash.write(readI)
hash.end()
const sha256sum = hash.read()
@ -51,9 +75,13 @@ import crypto from 'crypto'
// read image
image.sepia()
// change name
const newFileName = '.' + file
const bFile = path.basename(file)
const pName = path.dirname(file)
console.log(bFile)
const newFileName = pName + '/.' + bFile
console.log('newfilename:' + newFileName)
// save image with new file name
await image.writeAsync(`${folder}/${newFileName}`)
await image.writeAsync(`${newFileName}`)
console.log('Images have been inverted and rename ' + '.' + file)
}