From 53062809650c2b8da064b4c2c8ce4dceed67e819 Mon Sep 17 00:00:00 2001 From: garzj Date: Sat, 9 Mar 2024 14:40:10 +0100 Subject: [PATCH] Add verbose logging flag --- src/commands/common.ts | 6 ++++++ src/commands/migrate-flat.ts | 12 +++++++++++- src/commands/migrate-full.ts | 4 ++++ src/dir/migrate-full.ts | 1 + src/dir/migration-args.ts | 2 ++ src/dir/restructure-and-process.ts | 15 ++++++++++++--- 6 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/commands/common.ts b/src/commands/common.ts index dbfa343..3fd39b8 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -25,3 +25,9 @@ export const skipCorrectionsArg = flag({ long: 'skip-corrections', description: 'Skips renaming wrong extensions identified by ExifTool.', }); + +export const verboseArg = flag({ + short: 'v', + long: 'verbose', + description: 'Enables verbose logging.', +}); diff --git a/src/commands/migrate-flat.ts b/src/commands/migrate-flat.ts index 8433088..d12858e 100644 --- a/src/commands/migrate-flat.ts +++ b/src/commands/migrate-flat.ts @@ -5,6 +5,7 @@ import { errorDirArg, forceArg, timeoutArg, + verboseArg, } from './common'; import { migrateDirFlatGen } from '../dir/migrate-flat'; import { ExifTool } from 'exiftool-vendored'; @@ -28,8 +29,16 @@ export const migrateFlat = command({ force: forceArg, timeout: timeoutArg, skipCorrections: skipCorrectionsArg, + verbose: verboseArg, }, - handler: async ({ inputDir, outputDir, errorDir, force, timeout }) => { + handler: async ({ + inputDir, + outputDir, + errorDir, + force, + timeout, + verbose, + }) => { const errs: string[] = []; const checkErrs = () => { if (errs.length !== 0) { @@ -71,6 +80,7 @@ export const migrateFlat = command({ errorDir, log: console.log, warnLog: console.error, + verboseLog: verbose ? console.log : undefined, exiftool: new ExifTool({ taskTimeoutMillis: timeout }), endExifTool: true, }); diff --git a/src/commands/migrate-full.ts b/src/commands/migrate-full.ts index 99de168..05f2294 100644 --- a/src/commands/migrate-full.ts +++ b/src/commands/migrate-full.ts @@ -5,6 +5,7 @@ import { errorDirArg, forceArg, timeoutArg, + verboseArg, } from './common'; import { migrateDirFullGen } from '..'; import { ExifTool } from 'exiftool-vendored'; @@ -29,6 +30,7 @@ export const migrateFull = command({ force: forceArg, timeout: timeoutArg, skipCorrections: skipCorrectionsArg, + verbose: verboseArg, }, handler: async ({ inputDir, @@ -37,6 +39,7 @@ export const migrateFull = command({ force, timeout, skipCorrections, + verbose, }) => { const errs: string[] = []; const checkErrs = () => { @@ -71,6 +74,7 @@ export const migrateFull = command({ outputDir, log: console.log, warnLog: console.error, + verboseLog: verbose ? console.log : undefined, exiftool: new ExifTool({ taskTimeoutMillis: timeout }), endExifTool: true, skipCorrections, diff --git a/src/dir/migrate-full.ts b/src/dir/migrate-full.ts index 3d8f00e..4faffed 100644 --- a/src/dir/migrate-full.ts +++ b/src/dir/migrate-full.ts @@ -36,6 +36,7 @@ export async function* migrateDirFullGen( yield new NoPhotosDirError(migCtx.inputDir); return; } + migCtx.verboseLog(`Found google photos directory: ${googlePhotosDir}`); yield* restructureAndProcess(googlePhotosDir, migCtx); diff --git a/src/dir/migration-args.ts b/src/dir/migration-args.ts index a78ba69..a812c72 100644 --- a/src/dir/migration-args.ts +++ b/src/dir/migration-args.ts @@ -6,6 +6,7 @@ export type MigrationArgs = { errorDir: string; log?: (msg: string) => void; warnLog?: (msg: string) => void; + verboseLog?: (msg: string) => void; exiftool?: ExifTool; endExifTool?: boolean; skipCorrections?: boolean; @@ -22,6 +23,7 @@ export async function migrationArgsDefaults( endExifTool: args.endExifTool ?? !args.exiftool, log: args.log ?? (() => {}), warnLog: args.warnLog ?? (() => {}), + verboseLog: args.verboseLog ?? (() => {}), skipCorrections: args.skipCorrections ?? false, }; } diff --git a/src/dir/restructure-and-process.ts b/src/dir/restructure-and-process.ts index 8ff1827..ba8faaa 100644 --- a/src/dir/restructure-and-process.ts +++ b/src/dir/restructure-and-process.ts @@ -3,6 +3,7 @@ import { mkdir, readdir } from 'fs/promises'; import { migrateDirFlatGen } from './migrate-flat'; import { FullMigrationContext } from './migrate-full'; import { untitledDirs } from '../config/langs'; +import { Dirent } from 'fs'; async function* _restructureAndProcess( folders: string[], @@ -47,9 +48,15 @@ export async function* restructureAndProcess( // $rootdir/AlbumsProcessed/My Album 2/* // $rootdir/PhotosProcessed/* - const allDirs = (await readdir(sourceDir, { withFileTypes: true })).filter( - (f) => f.isDirectory() - ); + const verboseLogFiles = (label: string, files: (Dirent | string)[]) => + migCtx.verboseLog( + `${label}: ${files.map((f) => (typeof f === 'string' ? basename(f) : f.name)).join(', ')}` + ); + + const dirents = await readdir(sourceDir, { withFileTypes: true }); + verboseLogFiles('All entries', dirents); + const allDirs = dirents.filter((f) => f.isDirectory()); + verboseLogFiles('Only dirs', allDirs); // move the "Photos from $YEAR" directories to Photos/ migCtx.log('Processing photos...'); @@ -58,6 +65,7 @@ export async function* restructureAndProcess( .filter((f) => f.name === 'Photos' || f.name.startsWith('Photos from ')) .map((f) => join(f.path, f.name)) ); + verboseLogFiles('Photos from year dirs', [...photosFromDirs]); yield* _restructureAndProcess([...photosFromDirs], false, migCtx); // move everythingg else to Albums/, so we end up with two top level folders @@ -65,5 +73,6 @@ export async function* restructureAndProcess( const albumDirs = allDirs .filter((f) => !photosFromDirs.has(join(f.path, f.name))) .map((f) => join(f.path, f.name)); + verboseLogFiles('Album list', albumDirs); yield* _restructureAndProcess(albumDirs, true, migCtx); }