scripts/rangement/index.mjs

100 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**---------------------
* @name tykayn Rangement
* @description Rangement sorts and rename files depending on their exif data
* @contact contact@cipherbliss.com
--------------------- */
/** ---------------------
libs
--------------------- */
import fs from 'node-fs'
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;
console.log(' ')
function parseArguments () {
mini_arguments = minimist(process.argv.slice(2))
console.log('arguments', mini_arguments)
}
parseArguments()
function renameFile (originalFileName, fileMixedNewName) {
fs.rename(originalFileName, fileMixedNewName, function (err) {
if (err) console.log('rename ERROR: ' + err)
})
}
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 + ' '+'.' ,'.')
}
function appendFileName (fileProperties, newText) {
fileProperties.freeText = finder.cleanSpaces(fileProperties.freeText + ' ' + newText)
return fileProperties
}
function prependFileName (fileProperties, newText) {
fileProperties.freeText = finder.cleanSpaces(newText + ' ' + fileProperties.freeText)
return fileProperties
}
function guessFileNameOnAllFilesFromArguments(){
// parcourir les dossiers
// parcourir les fichiers
console.log('liste des fichiers', mini_arguments._)
let fileList = mini_arguments._;
fileList.forEach(fileName => {
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)
if(foundDate){
structureForFile.dateStamp = (finder.findExifCreationDate(fileName) + '')
}
}
let newname = makeFileNameFromProperties(structureForFile)
if(fileName !== newname ){
console.log(' nouveau nom:',newname )
}else{
console.log(' rien à changer' )
}
})
}
guessFileNameOnAllFilesFromArguments()
// run tests
if (enableTestsLocally) {
TestTagsAreDetectedInFileName()
TestFindFormattedDate()
TestScreenShotIsFoundAndRenamed()
}
if (reportStatistics || mini_arguments.stats) {
finder.reportStatistics()
}