scripts/rangement/index.mjs

108 lines
2.9 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'
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 makeFileNameFromProperties (fileProperties) {
2023-06-29 19:33:31 +02:00
let tagPlace = ''
if (fileProperties.tags.length) {
2023-06-29 19:33:31 +02:00
tagPlace = ' ' + tagSectionSeparator + ' '
}
return finder.cleanSpaces(fileProperties.dateStamp + ' ' + fileProperties.freeText + tagPlace + fileProperties.tags.join(tagSeparator) + fileProperties.extension).replace(+' ' + tagSectionSeparator + ' ' + '.', '.')
2023-06-27 23:03:35 +02:00
}
2023-06-27 16:06:22 +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
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._
2023-06-29 19:33:31 +02:00
fileList.forEach(fileName => {
2023-06-29 20:09:33 +02:00
let structureForFile = finder.destructurateFileName(fileName)
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
if (!structureForFile.dateStamp) {
let foundDate = finder.findExifCreationDate(fileName).then((data) => {
console.log(' =>>>>>>> foundDate : ', foundDate)
if (foundDate) {
structureForFile.dateStamp = finder.findExifCreationDate(fileName)
} else {
console.log(' pas de date trouvée dans le nom')
}
let newname = makeFileNameFromProperties(structureForFile)
if (fileName !== newname) {
console.log(' nouveau nom:', newname)
} else {
console.log(' rien à changer')
}
}, (err) => {
console.error(err)
})
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
}