Skip to content

Commit

Permalink
Add verbose logging flag
Browse files Browse the repository at this point in the history
  • Loading branch information
garzj committed Mar 10, 2024
1 parent 3e661d4 commit 5306280
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/commands/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
});
12 changes: 11 additions & 1 deletion src/commands/migrate-flat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
errorDirArg,
forceArg,
timeoutArg,
verboseArg,
} from './common';
import { migrateDirFlatGen } from '../dir/migrate-flat';
import { ExifTool } from 'exiftool-vendored';
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
});
Expand Down
4 changes: 4 additions & 0 deletions src/commands/migrate-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
errorDirArg,
forceArg,
timeoutArg,
verboseArg,
} from './common';
import { migrateDirFullGen } from '..';
import { ExifTool } from 'exiftool-vendored';
Expand All @@ -29,6 +30,7 @@ export const migrateFull = command({
force: forceArg,
timeout: timeoutArg,
skipCorrections: skipCorrectionsArg,
verbose: verboseArg,
},
handler: async ({
inputDir,
Expand All @@ -37,6 +39,7 @@ export const migrateFull = command({
force,
timeout,
skipCorrections,
verbose,
}) => {
const errs: string[] = [];
const checkErrs = () => {
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/dir/migrate-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/dir/migration-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
};
}
15 changes: 12 additions & 3 deletions src/dir/restructure-and-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down Expand Up @@ -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...');
Expand All @@ -58,12 +65,14 @@ 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
migCtx.log('Processing albums...');
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);
}

0 comments on commit 5306280

Please sign in to comment.