Created
July 1, 2021 12:21
-
-
Save RoySegall/ed06e866644cfb391bd4596965ec224d to your computer and use it in GitHub Desktop.
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
const handleFiles = async (sourceDirectory, destination, results) => { | |
let index = 1; | |
const files = fs.readdirSync(sourceDirectory); | |
return Promise.all(files.map(async (filename) => { | |
return new Promise(async (resolve) => { | |
console.log(colors.yellow(`Process the file ${filename}`)); | |
const destinationPath = path.join(destination, `${filename.split('.')[0]}.json`); | |
if (fs.existsSync(destinationPath)) { | |
results[filename] = "Skipped"; | |
console.log(colors.blue(`${filename} (${index++} / ${files.length}) skipped`)); | |
} else { | |
try { | |
const processResults = await handleFile(type, path.join(sourceDirectory, filename)); | |
fs.writeFileSync(destinationPath, JSON.stringify(processResults)); | |
console.log(colors.green(`${filename} (${index++} / ${files.length}) processed`)); | |
results[filename] = "Passed"; | |
} catch (e) { | |
console.log(colors.red(`${filename} (${index++} / ${files.length}) error`)); | |
console.error(filename, e); | |
results[filename] = `Failed: ${e}` | |
} | |
} | |
resolve(); | |
}); | |
})); | |
}; |
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
const handleFiles = async (sourceDirectory, destination, results) => { | |
const filesChunks = 20; | |
if (!existsSync(destination)) { | |
mkdirSync(destination); | |
console.log(colors.magenta(`Destination folder ${destination} created`)) | |
} | |
// Get all the files we need to process. | |
let filesFromSourceDirectory = await readdirSync(sourceDirectory); | |
// Get all the files we processed. | |
const filesFromDestination = await readdirSync(destination); | |
// Clean the processed files the un-processed files and log it and Split the unprocessed files to chunks of 20. | |
// todo: Filter is wrong, there are duplicate files! | |
let filesToProcess = chunk(filesFromSourceDirectory.filter((file: string) => { | |
const filename = basename(file); | |
return !filesFromDestination.includes(`${parse(filename).name}.json`) || file === '.DS_Store'; | |
}), filesChunks); | |
if (filesFromDestination.length > 0) { | |
console.log(`${filesFromDestination.length} files were processed been processed: ${filesFromDestination.map(file => `${file}\n`)}`.yellow) | |
} else { | |
console.log('Starting to process all the files'.blue) | |
} | |
console.log(`Start to process ${filesToProcess.length} chunks of files.`.yellow.bgBlue); | |
for (let files of filesToProcess) { | |
for (let filename of files) { | |
console.log(filename, freemem() / 1000); | |
try { | |
const processResults = await handleFile(type, join(sourceDirectory, filename)); | |
writeFileSync(join(destination, `${basename(filename)}.json`), JSON.stringify(processResults)); | |
results[filename] = "Passed"; | |
} catch (e) { | |
console.error(filename, e); | |
results[filename] = `Failed: ${e}` | |
} | |
global.gc(); | |
} | |
console.log('-------'); | |
} | |
console.log(results); | |
return Promise.resolve(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment