add tests and append tag
This commit is contained in:
parent
3dc64b4b47
commit
e7f4ded272
@ -103,6 +103,15 @@ export default class finder {
|
|||||||
return listOfTags
|
return listOfTags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static removeTagInProperties(properties, tagName) {
|
||||||
|
|
||||||
|
let foundTagNameIndex = properties.tags.indexOf(tagName)
|
||||||
|
if (foundTagNameIndex) {
|
||||||
|
delete properties.tags[foundTagNameIndex]
|
||||||
|
}
|
||||||
|
return properties
|
||||||
|
}
|
||||||
|
|
||||||
static cleanSpaces (inputString) {
|
static cleanSpaces (inputString) {
|
||||||
return inputString.trim().replace(/ *g/, ' ')
|
return inputString.trim().replace(/ *g/, ' ')
|
||||||
}
|
}
|
||||||
|
300
index.mjs
300
index.mjs
@ -8,221 +8,217 @@
|
|||||||
--------------------- */
|
--------------------- */
|
||||||
import fs from 'node-fs'
|
import fs from 'node-fs'
|
||||||
import minimist from 'minimist'
|
import minimist from 'minimist'
|
||||||
import log from 'loglevel';
|
import log from 'loglevel'
|
||||||
import path from "node:path";
|
import path from 'node:path'
|
||||||
/** ---------------------
|
/** ---------------------
|
||||||
custom utilities and configuration
|
custom utilities and configuration
|
||||||
--------------------- */
|
--------------------- */
|
||||||
import rangement_instance from './configs.mjs'
|
import rangement_instance from './configs.mjs'
|
||||||
import {
|
import {
|
||||||
TestFindFormattedDate,
|
TestFindFormattedDate,
|
||||||
TestScreenShotIsFoundAndRenamed,
|
TestScreenShotIsFoundAndRenamed,
|
||||||
TestTagsAreDetectedInFileName
|
TestTagsAreDetectedInFileName
|
||||||
} from './testFunctions.mjs'
|
} from './testFunctions.mjs'
|
||||||
import finder from './finder.mjs'
|
import finder from './finder.mjs'
|
||||||
|
|
||||||
import exiftool from "node-exiftool";
|
|
||||||
|
|
||||||
let mini_arguments
|
let mini_arguments
|
||||||
|
|
||||||
log.setLevel(rangement_instance.log_level)
|
log.setLevel(rangement_instance.log_level)
|
||||||
log.info(' ')
|
log.info(' ')
|
||||||
|
|
||||||
function parseArguments() {
|
function parseArguments () {
|
||||||
mini_arguments = minimist(process.argv.slice(2))
|
mini_arguments = minimist(process.argv.slice(2))
|
||||||
log.debug('arguments', mini_arguments)
|
log.debug('arguments', mini_arguments)
|
||||||
}
|
}
|
||||||
|
|
||||||
function addOriginalFileNameIfMissing(originalFileName, fileMixedNewName) {
|
function addOriginalFileNameIfMissing (originalFileName, fileMixedNewName) {
|
||||||
|
|
||||||
// const ep = new exiftool.ExiftoolProcess()
|
// const ep = new exiftool.ExiftoolProcess()
|
||||||
//
|
//
|
||||||
// ep
|
// ep
|
||||||
// .open(fileMixedNewName)
|
// .open(fileMixedNewName)
|
||||||
// .then(() => ep.writeMetadata(fileMixedNewName, {
|
// .then(() => ep.writeMetadata(fileMixedNewName, {
|
||||||
// 'OriginalFileName+': originalFileName,
|
// 'OriginalFileName+': originalFileName,
|
||||||
// }))
|
// }))
|
||||||
// .then(console.log, console.error)
|
// .then(console.log, console.error)
|
||||||
// .then(() => ep.close())
|
// .then(() => ep.close())
|
||||||
// .catch(console.error)
|
// .catch(console.error)
|
||||||
}
|
}
|
||||||
|
|
||||||
function renameFile(originalFileName, fileMixedNewName) {
|
function renameFile (originalFileName, fileMixedNewName) {
|
||||||
fs.rename(originalFileName, fileMixedNewName, function (err) {
|
fs.rename(originalFileName, fileMixedNewName, function (err) {
|
||||||
log.info('name changed', fileMixedNewName)
|
log.info('name changed', fileMixedNewName)
|
||||||
if (err) {
|
if (err) {
|
||||||
log.info('rename ERROR: ' + err)
|
log.info('rename ERROR: ' + err)
|
||||||
} else {
|
} else {
|
||||||
// otherRenames.push(originalFileName)
|
// otherRenames.push(originalFileName)
|
||||||
otherRenames.push(fileMixedNewName)
|
otherRenames.push(fileMixedNewName)
|
||||||
|
|
||||||
addOriginalFileNameIfMissing(originalFileName, fileMixedNewName)
|
addOriginalFileNameIfMissing(originalFileName, fileMixedNewName)
|
||||||
// rangement_instance.statistics['filesModified']++
|
// rangement_instance.statistics['filesModified']++
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
|
||||||
function makeFileNameFromProperties(fileProperties) {
|
|
||||||
|
|
||||||
let tagPlace = ''
|
|
||||||
log.info('fileProperties.tags',fileProperties.tags)
|
|
||||||
if (fileProperties.tags.length && rangement_instance.keepTags) {
|
|
||||||
tagPlace = ' ' + rangement_instance.tagSectionSeparator + ' '+ fileProperties.tags.join(rangement_instance.tagSeparator)
|
|
||||||
}
|
|
||||||
console.log('fileProperties.dateStampExif', fileProperties.dateStampExif)
|
|
||||||
let newName = ''
|
|
||||||
+ fileProperties.dateStampExif
|
|
||||||
+ ' '
|
|
||||||
+ (rangement_instance.keepFreeText? fileProperties.freeText : '')
|
|
||||||
+ tagPlace
|
|
||||||
+ fileProperties.extension;
|
|
||||||
|
|
||||||
if(rangement_instance.replaceUnderscoreWithSpaces){
|
|
||||||
newName = newName.replace('_', ' ')
|
|
||||||
}
|
|
||||||
|
|
||||||
newName = newName.replace(' ', '')
|
|
||||||
|
|
||||||
return newName
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let otherRenames = [];
|
export function makeFileNameFromProperties (fileProperties) {
|
||||||
|
|
||||||
function shouldWeChangeName(structureForFile) {
|
let tagPlace = ''
|
||||||
log.info(' ______ shouldWeChangeName ', structureForFile.fileNameOriginal)
|
log.info('fileProperties.tags', fileProperties.tags)
|
||||||
let newName = makeFileNameFromProperties(structureForFile)
|
if (fileProperties.tags.length && rangement_instance.keepTags) {
|
||||||
log.debug('newName', newName)
|
tagPlace = ' ' + rangement_instance.tagSectionSeparator + ' ' + fileProperties.tags.join(rangement_instance.tagSeparator)
|
||||||
if (structureForFile.fileNameOriginal !== newName && !otherRenames.includes(newName)) {
|
}
|
||||||
|
console.log('fileProperties.dateStampExif', fileProperties.dateStampExif)
|
||||||
|
let newName = ''
|
||||||
|
+ fileProperties.dateStampExif
|
||||||
|
+ ' '
|
||||||
|
+ (rangement_instance.keepFreeText ? fileProperties.freeText : '')
|
||||||
|
+ tagPlace
|
||||||
|
+ fileProperties.extension
|
||||||
|
|
||||||
log.info('\n ancien nom :', structureForFile.fileNameOriginal)
|
if (rangement_instance.replaceUnderscoreWithSpaces) {
|
||||||
|
newName = newName.replace('_', ' ')
|
||||||
|
}
|
||||||
|
|
||||||
log.info(' nouveau nom:', newName)
|
newName = finder.cleanSpaces(newName)
|
||||||
if (!mini_arguments['dry-run']) {
|
|
||||||
renameFile(structureForFile.fullPath, structureForFile.folderPath + newName)
|
return newName
|
||||||
} else {
|
|
||||||
log.info('no renaming for real, this is a dry run')
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.info(' rien à changer')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let otherRenames = []
|
||||||
|
|
||||||
|
function shouldWeChangeName (structureForFile) {
|
||||||
|
log.info(' ______ shouldWeChangeName ', structureForFile.fileNameOriginal)
|
||||||
|
let newName = makeFileNameFromProperties(structureForFile)
|
||||||
|
log.debug('newName', newName)
|
||||||
|
if (structureForFile.fileNameOriginal !== newName && !otherRenames.includes(newName)) {
|
||||||
|
|
||||||
|
log.info('\n ancien nom :', structureForFile.fileNameOriginal)
|
||||||
|
|
||||||
|
log.info(' nouveau nom:', newName)
|
||||||
|
if (!mini_arguments['dry-run']) {
|
||||||
|
renameFile(structureForFile.fullPath, structureForFile.folderPath + newName)
|
||||||
|
} else {
|
||||||
|
log.info('no renaming for real, this is a dry run')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.info(' rien à changer')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* guess file name on one file which is not a directory
|
* guess file name on one file which is not a directory
|
||||||
* @param fullPath
|
* @param fullPath
|
||||||
*/
|
*/
|
||||||
function guessFileNameOnOnefile(fullPath) {
|
function guessFileNameOnOnefile (fullPath) {
|
||||||
|
|
||||||
log.info('go guess file name on file: ', fullPath )
|
log.info('go guess file name on file: ', fullPath)
|
||||||
fs.stat(fullPath, (err, stats) => {
|
fs.stat(fullPath, (err, stats) => {
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
log.error('échec fichier', err)
|
log.error('échec fichier', err)
|
||||||
log.error('ce fichier n existe pas: ', fullPath)
|
log.error('ce fichier n existe pas: ', fullPath)
|
||||||
return;
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
let structureForFile = finder.destructurateFileName(fullPath)
|
let structureForFile = finder.destructurateFileName(fullPath)
|
||||||
|
|
||||||
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
|
// examiner les infos exif de chaque fichier pour proposer un nouveau nom
|
||||||
if (!structureForFile.dateStampInFileNameOriginal) {
|
if (!structureForFile.dateStampInFileNameOriginal) {
|
||||||
log.info(' le nom de fichier "'+structureForFile.freeText +'" ne contient pas de date formatée au début')
|
log.info(' le nom de fichier "' + structureForFile.freeText + '" ne contient pas de date formatée au début')
|
||||||
|
|
||||||
finder.findExifCreationDate(structureForFile.fullPath)
|
finder.findExifCreationDate(structureForFile.fullPath)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
log.info(' ... chercher la date de création : "'+structureForFile.freeText +'"')
|
log.info(' ... chercher la date de création : "' + structureForFile.freeText + '"')
|
||||||
let foundDate = finder.findEarliestDateInExifData(data)
|
let foundDate = finder.findEarliestDateInExifData(data)
|
||||||
|
|
||||||
log.info(' =>>>>>>> foundDate : ', foundDate)
|
log.info(' =>>>>>>> foundDate : ', foundDate)
|
||||||
if (foundDate) {
|
if (foundDate) {
|
||||||
structureForFile.dateStampExif = foundDate
|
structureForFile.dateStampExif = foundDate
|
||||||
} else {
|
} else {
|
||||||
log.info('pas de date trouvée dans le nom')
|
log.info('pas de date trouvée dans le nom')
|
||||||
}
|
}
|
||||||
shouldWeChangeName(structureForFile)
|
shouldWeChangeName(structureForFile)
|
||||||
}
|
}
|
||||||
,
|
,
|
||||||
(error) => {
|
(error) => {
|
||||||
log.warn('/////////// Error in reading exif of file: ' + error.message)
|
log.warn('/////////// Error in reading exif of file: ' + error.message)
|
||||||
return ''
|
return ''
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let expandedFileList = []
|
let expandedFileList = []
|
||||||
let cwd = path.dirname(process.cwd()) + '/' + path.basename(process.cwd());
|
let cwd = path.dirname(process.cwd()) + '/' + path.basename(process.cwd())
|
||||||
console.log('cwd', cwd)
|
console.log('cwd', cwd)
|
||||||
|
|
||||||
function guessFileNameOnAllFilesFromArguments() {
|
function guessFileNameOnAllFilesFromArguments () {
|
||||||
|
|
||||||
|
// parcourir les fichiers
|
||||||
|
log.debug('liste des fichiers', mini_arguments._)
|
||||||
|
let fileList = mini_arguments._
|
||||||
|
|
||||||
// parcourir les fichiers
|
// test file exists
|
||||||
log.debug('liste des fichiers', mini_arguments._)
|
fileList.forEach(fullPath => {
|
||||||
let fileList = mini_arguments._
|
// parcourir les dossiers
|
||||||
|
isFolderOrFile(`${fullPath}`)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
log.info('expanded file list :', expandedFileList)
|
||||||
// test file exists
|
expandedFileList.forEach(filePath => guessFileNameOnOnefile(filePath))
|
||||||
fileList.forEach(fullPath => {
|
|
||||||
// parcourir les dossiers
|
|
||||||
isFolderOrFile(`${fullPath}`)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
log.info('expanded file list :', expandedFileList)
|
|
||||||
expandedFileList.forEach(filePath => guessFileNameOnOnefile(filePath))
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function readSubdirectories(baseDir) {
|
function readSubdirectories (baseDir) {
|
||||||
const newGlob = baseDir;
|
const newGlob = baseDir
|
||||||
fs.readdir(baseDir, (err, files) => {
|
fs.readdir(baseDir, (err, files) => {
|
||||||
if (err) throw err;
|
if (err) throw err
|
||||||
|
|
||||||
console.log('readSubdirectories - files', files)
|
console.log('readSubdirectories - files', files)
|
||||||
files.forEach((subDirOrFile) => {
|
files.forEach((subDirOrFile) => {
|
||||||
const newFullPath = cwd +'/'+ subDirOrFile;
|
const newFullPath = cwd + '/' + subDirOrFile
|
||||||
|
|
||||||
if (fs.existsSync(newFullPath)) {
|
if (fs.existsSync(newFullPath)) {
|
||||||
const s = fs.statSync(newFullPath);
|
const s = fs.statSync(newFullPath)
|
||||||
|
|
||||||
if (s.isFile()) {
|
if (s.isFile()) {
|
||||||
expandedFileList.push(cwd+'/'+subDirOrFile)
|
expandedFileList.push(cwd + '/' + subDirOrFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function isFolderOrFile(fileName) {
|
function isFolderOrFile (fileName) {
|
||||||
// const stat = fs.statSync(cwd + '/' + fileName);
|
// const stat = fs.statSync(cwd + '/' + fileName);
|
||||||
const stat = fs.statSync( fileName);
|
const stat = fs.statSync(fileName)
|
||||||
|
|
||||||
if (stat.isDirectory()) {
|
if (stat.isDirectory()) {
|
||||||
let fileList = readSubdirectories(fileName);
|
let fileList = readSubdirectories(fileName)
|
||||||
console.log('fileList in directory ',fileName, '\n', fileList)
|
console.log('fileList in directory ', fileName, '\n', fileList)
|
||||||
if (fileList) {
|
if (fileList) {
|
||||||
expandedFileList.push(...fileList)
|
expandedFileList.push(...fileList)
|
||||||
}
|
}
|
||||||
}
|
} else if (stat.isFile()) {
|
||||||
else if (stat.isFile()) {
|
expandedFileList.push(cwd + '/' + fileName)
|
||||||
expandedFileList.push(cwd + '/' + fileName)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parseArguments()
|
parseArguments()
|
||||||
|
|
||||||
guessFileNameOnAllFilesFromArguments()
|
guessFileNameOnAllFilesFromArguments()
|
||||||
|
|
||||||
|
|
||||||
// run tests
|
// run tests
|
||||||
if (rangement_instance.enableTestsLocally) {
|
if (rangement_instance.enableTestsLocally) {
|
||||||
|
|
||||||
TestTagsAreDetectedInFileName()
|
TestTagsAreDetectedInFileName()
|
||||||
TestFindFormattedDate()
|
TestFindFormattedDate()
|
||||||
TestScreenShotIsFoundAndRenamed()
|
TestScreenShotIsFoundAndRenamed()
|
||||||
}
|
}
|
||||||
if (rangement_instance.reportStatistics || mini_arguments.stats) {
|
if (rangement_instance.reportStatistics || mini_arguments.stats) {
|
||||||
finder.reportStatistics()
|
finder.reportStatistics()
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import finder from '../finder.mjs'
|
import finder from '../finder.mjs'
|
||||||
|
import { makeFileNameFromProperties } from '../index'
|
||||||
|
import { expect } from '@jest/globals'
|
||||||
|
|
||||||
describe('detection in file name', () => {
|
describe('detection in file name', () => {
|
||||||
|
|
||||||
@ -52,21 +54,69 @@ describe('modification in file name', () => {
|
|||||||
)
|
)
|
||||||
.toBe('mon texte ajouté à la fin du texte libre')
|
.toBe('mon texte ajouté à la fin du texte libre')
|
||||||
})
|
})
|
||||||
xtest('should replace text in file name', () => {
|
test('should replace text in file name', () => {
|
||||||
|
let searchString = 'vacances', replaceString = 'machin'
|
||||||
|
let replacedString = finder.searchAndReplaceInFileName(searchString, replaceString, fileNameOriginal)
|
||||||
|
expect(
|
||||||
|
replacedString
|
||||||
|
)
|
||||||
|
.toBe('mon texte -- famille machin plage.jpg')
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
xdescribe('modifications with tags', () => {
|
describe('modifications with tags', () => {
|
||||||
|
|
||||||
xtest('should add tag in file name', () => {
|
let fileNameOriginal = 'mon nom sans aucun tag.jpg'
|
||||||
|
let properties = finder.destructurateFileName(fileNameOriginal)
|
||||||
|
|
||||||
|
test('should have no tag in file properties', () => {
|
||||||
|
expect(
|
||||||
|
properties.tags
|
||||||
|
)
|
||||||
|
.toStrictEqual([])
|
||||||
|
|
||||||
})
|
})
|
||||||
xtest('should remove tag in file name', () => {
|
test('should list existing tags in file name', () => {
|
||||||
|
let fileNameOriginal = 'mon nom -- carte bidule.jpg'
|
||||||
|
let otherProperties = finder.destructurateFileName(fileNameOriginal)
|
||||||
|
|
||||||
|
expect(
|
||||||
|
otherProperties.tags
|
||||||
|
)
|
||||||
|
.toStrictEqual(['carte','bidule'])
|
||||||
|
|
||||||
})
|
})
|
||||||
xtest('should remove and add tags in file name', () => {
|
test('should add tag in file name', () => {
|
||||||
|
|
||||||
|
let newTag = 'illustration'
|
||||||
|
let newName = finder.addTagInFileName(newTag, fileNameOriginal)
|
||||||
|
|
||||||
|
expect(newName)
|
||||||
|
.toBe('mon nom sans aucun tag -- illustration.jpg')
|
||||||
|
|
||||||
|
let otherProperties = finder.destructurateFileName(newName)
|
||||||
|
expect(
|
||||||
|
otherProperties.tags
|
||||||
|
)
|
||||||
|
.toStrictEqual(['illustration'])
|
||||||
|
|
||||||
|
expect(
|
||||||
|
makeFileNameFromProperties(otherProperties)
|
||||||
|
)
|
||||||
|
.toBe('mon nom sans aucun tag -- illustration.jpg')
|
||||||
|
})
|
||||||
|
test('should remove tag in file name', () => {
|
||||||
|
|
||||||
|
let fileNameOriginal = 'mon nom avec un tag -- voyages.jpg'
|
||||||
|
// let properties = finder.destructurateFileName(fileNameOriginal)
|
||||||
|
// expect(properties.tags).toStrictEqual(['voyages'])
|
||||||
|
|
||||||
|
let newProperties = finder.removeTagInProperties(properties, 'voyages')
|
||||||
|
expect(newProperties.tags).toStrictEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
xtest('should remove with minus prefix and add tags in file name at the same time', () => {
|
||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
Loading…
Reference in New Issue
Block a user