scripts/rangement/index.mjs

135 lines
4.0 KiB
JavaScript
Raw Normal View History

2023-06-29 18:58:08 +02:00
/**---------------------
* @name tykayn Rangement
* @description Rangement sorts and rename files depending on their exif data
* @contact contact@cipherbliss.com
--------------------- */
/** ---------------------
libs
--------------------- */
2023-06-27 15:40:50 +02:00
import fs from 'node-fs'
2023-06-29 18:58:08 +02:00
import minimist from 'minimist'
/** ---------------------
custom utilities and configuration
--------------------- */
import { enableTestsLocally, reportStatistics,tagSectionSeparator, tagSeparator } from './configs.mjs'
2023-06-29 18:58:08 +02:00
import {
TestFindFormattedDate,
TestScreenShotIsFoundAndRenamed,
TestTagsAreDetectedInFileName
} from './testFunctions.mjs'
import finder from './finder.mjs'
let mini_arguments
2023-06-29 18:58:08 +02:00
console.log(' ')
2023-06-29 18:59:12 +02:00
2023-06-29 18:58:08 +02:00
function parseArguments () {
mini_arguments = minimist(process.argv.slice(2))
2023-06-29 19:33:31 +02:00
console.log('arguments', mini_arguments)
2023-06-29 18:58:08 +02:00
}
parseArguments()
2023-06-27 15:40:50 +02:00
2023-06-29 18:58:08 +02:00
function renameFile (originalFileName, fileMixedNewName) {
fs.rename(originalFileName, fileMixedNewName, function (err) {
if (err) console.log('rename ERROR: ' + err)
})
2023-06-27 23:03:35 +02:00
}
2023-06-29 18:58:08 +02:00
function appendFileName (fileProperties, newText) {
fileProperties.freeText = finder.cleanSpaces(fileProperties.freeText + ' ' + newText)
return fileProperties
2023-06-27 23:18:00 +02:00
}
2023-06-29 17:09:18 +02:00
2023-06-29 18:58:08 +02:00
function prependFileName (fileProperties, newText) {
fileProperties.freeText = finder.cleanSpaces(newText + ' ' + fileProperties.freeText)
return fileProperties
2023-06-27 23:03:35 +02:00
}
2023-06-29 17:09:18 +02:00
function makeFileNameFromProperties(fileProperties) {
let tagPlace = ''
if (fileProperties.tags.length) {
tagPlace = ' ' + tagSectionSeparator + ' '
}
// return finder.cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension).replace(+' ' + tagSectionSeparator + ' ' + '.', '.')
return ''+fileProperties.dateStampExif + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension
}
function shouldWeChangeName (structureForFile) {
console.log(' ______ allez hop fini la recherche on fait un nouveau nom')
console.log('structureForFile', structureForFile)
let newName = makeFileNameFromProperties(structureForFile)
if (structureForFile.fileNameOriginal !== newName) {
console.log('\n ancien nom :', structureForFile.fileNameOriginal)
// console.log(' nouveau nom:', foundDate +structureForFile.freeText + structureForFile.tags.join(tagSeparator) + structureForFile.extension )
console.log(' nouveau nom:', newName)
} else {
console.log(' rien à changer')
}
}
async function guessFileNameOnAllFilesFromArguments () {
2023-06-29 19:33:31 +02:00
// parcourir les dossiers
// parcourir les fichiers
console.log('liste des fichiers', mini_arguments._)
let fileList = mini_arguments._
fileList.forEach(fullPath => {
let structureForFile = finder.destructurateFileName(fullPath)
2023-06-29 19:33:31 +02:00
2023-06-29 20:09:33 +02:00
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
if (!structureForFile.dateStampInFileNameOriginal) {
console.log(' le nom de fichier ne contient pas de date formatée au début')
finder.findExifCreationDate(structureForFile.fullPath)
.then(data => {
console.log(' ... chercher la date de création')
let foundDate = finder.findEarliestDateInExifData(data)
console.log(' =>>>>>>> foundDate : ', foundDate)
if (foundDate) {
// finder.findEarliestDateInExifData(fullPath).then(response => {
// console.log(' ... trouvée')
// if (response) {
structureForFile.dateStampExif = foundDate
shouldWeChangeName(structureForFile)
// }
// })
} else {
console.log('pas de date trouvée dans le nom')
}
}
,
(error) => {
console.log('/////////// Error in reading exif of file: ' + error.message)
return ''
})
2023-06-29 19:33:31 +02:00
}
}
2023-06-29 20:09:33 +02:00
)
2023-06-29 19:33:31 +02:00
}
guessFileNameOnAllFilesFromArguments()
2023-06-27 23:18:00 +02:00
// run tests
2023-06-29 18:13:46 +02:00
if (enableTestsLocally) {
2023-06-27 23:18:00 +02:00
2023-06-29 18:58:08 +02:00
TestTagsAreDetectedInFileName()
TestFindFormattedDate()
TestScreenShotIsFoundAndRenamed()
2023-06-27 23:18:00 +02:00
}
if (reportStatistics || mini_arguments.stats) {
2023-06-29 18:58:08 +02:00
finder.reportStatistics()
2023-06-29 19:33:31 +02:00
}