From c197059c80291e4a9bfcbc8ea91429729b15ad3a Mon Sep 17 00:00:00 2001 From: electrovir Date: Sat, 3 Jul 2021 07:26:18 -0600 Subject: [PATCH] add custom track processing input --- package-lock.json | 4 +-- package.json | 2 +- src/api/api-types.ts | 7 +++-- src/migration/make-new-library.ts | 6 ++-- src/migration/migrate-library.ts | 6 ++++ src/tests/migrate-library.test.ts | 46 +++++++++++++++++++++++++++++-- 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 530aa5d..d2c427e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { "name": "itunes-library-migration-assistant", - "version": "0.2.1", + "version": "0.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "version": "0.2.1", + "version": "0.3.0", "license": "MIT", "dependencies": { "fast-deep-equal": "^3.1.3", diff --git a/package.json b/package.json index 7998aae..cea79d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "itunes-library-migration-assistant", - "version": "0.2.1", + "version": "0.3.0", "description": "iTunes Library Migration Assistant", "keywords": [ "itunes", diff --git a/src/api/api-types.ts b/src/api/api-types.ts index fb00f01..8bb28fc 100644 --- a/src/api/api-types.ts +++ b/src/api/api-types.ts @@ -1,3 +1,5 @@ +import {ParsedTrack} from '../migration/reading/parsed-types'; + export type ReplacePath = { old: string; new: string; @@ -16,13 +18,13 @@ export enum MigrationOutput { PlistString = 'plist-string', } -export type RunTimeOptions = { +export interface RunTimeOptions extends Record { validationEnabled: boolean; loggingEnabled: boolean; checkReplacementPaths: boolean; checkFiles: boolean; removeRatingComputed: boolean; -}; +} export const defaultOptions: RunTimeOptions = { validationEnabled: true, @@ -37,4 +39,5 @@ export type MigrationApiInput[]>; outputType?: OutputType; options?: Readonly>; + extraTrackProcessing?: (updatedTrack: Readonly) => Readonly; }; diff --git a/src/migration/make-new-library.ts b/src/migration/make-new-library.ts index 4c4b5e8..f9c718c 100644 --- a/src/migration/make-new-library.ts +++ b/src/migration/make-new-library.ts @@ -1,5 +1,5 @@ import {existsSync} from 'fs'; -import {InputPath, ReplacePath} from '../api/api-types'; +import {InputPath, MigrationApiInput, ReplacePath} from '../api/api-types'; import {decodeLocation, encodeLocation} from '../augments/string'; import {RequiredBy} from '../augments/type'; import {LibraryMigrationError} from '../errors/library-migration-error'; @@ -13,6 +13,7 @@ export function makeNewLibrary({ loggingEnabled = true, checkFiles = false, removeRatingComputed = false, + extraTrackProcessing = (track) => track, }: Readonly<{ oldLibrary: Readonly; replacePaths: Readonly[]>; @@ -20,6 +21,7 @@ export function makeNewLibrary({ loggingEnabled?: boolean; checkFiles?: boolean; removeRatingComputed?: boolean; + extraTrackProcessing?: MigrationApiInput['extraTrackProcessing']; }>): Readonly { const unreplacedPaths = new Set(); const replacePathUsage = replacePaths.map(() => 0); @@ -117,7 +119,7 @@ export function makeNewLibrary({ } if (!markedForDeletion) { - newTracks[trackKey] = rawNewTrack; + newTracks[trackKey] = extraTrackProcessing(rawNewTrack); } return newTracks; diff --git a/src/migration/migrate-library.ts b/src/migration/migrate-library.ts index 66a690b..ecdf18d 100644 --- a/src/migration/migrate-library.ts +++ b/src/migration/migrate-library.ts @@ -9,30 +9,35 @@ export function migrateLibrary({ replacePaths, outputType, options: rawOptions, + extraTrackProcessing, }: MigrationApiInput): ParsedLibrary; export function migrateLibrary({ libraryFilePath, replacePaths, outputType, options: rawOptions, + extraTrackProcessing, }: MigrationApiInput): {filePath: string}; export function migrateLibrary({ libraryFilePath, replacePaths, outputType, options: rawOptions, + extraTrackProcessing, }: MigrationApiInput): {plist: string}; export function migrateLibrary({ libraryFilePath, replacePaths, outputType, options: rawOptions, + extraTrackProcessing, }: MigrationApiInput): {filePath: string} | {plist: string} | ParsedLibrary; export function migrateLibrary({ libraryFilePath, replacePaths, outputType = MigrationOutput.WriteToFile, options: rawOptions = defaultOptions, + extraTrackProcessing, }: MigrationApiInput): {filePath: string} | {plist: string} | ParsedLibrary { const options = {...defaultOptions, ...rawOptions}; @@ -44,6 +49,7 @@ export function migrateLibrary({ const newLibrary = makeNewLibrary({ oldLibrary, replacePaths, + extraTrackProcessing, ...options, }); diff --git a/src/tests/migrate-library.test.ts b/src/tests/migrate-library.test.ts index d4c3973..fdcf9f2 100644 --- a/src/tests/migrate-library.test.ts +++ b/src/tests/migrate-library.test.ts @@ -45,9 +45,10 @@ testGroup({ outputType: MigrationOutput.PlistString, }).plist; - const parsedLibraryFile = readLibraryFile({libraryFilePath: libraryFilePath}); const reParsedLibrary = readLibraryString({libraryString: outputPlist}); + const parsedLibraryFile = readLibraryFile({libraryFilePath: libraryFilePath}); + return equal(parsedLibraryFile, reParsedLibrary); }, }); @@ -65,9 +66,48 @@ testGroup({ outputType: MigrationOutput.JsonObject, }); - const parsedLibraryFile = readLibraryFile({libraryFilePath: libraryFilePath}); + const reParsedMigratedLibraryFile = readLibraryFile({ + libraryFilePath: libraryFilePath, + }); - return equal(parsedLibraryFile, outputObject); + return equal(reParsedMigratedLibraryFile, outputObject); + }, + }); + + runTest({ + description: 'extra track processing works', + expect: true, + test: () => { + const deletedProperty = 'Play Count'; + + const outputObject = migrateLibrary({ + libraryFilePath, + replacePaths: [], + options: { + checkReplacementPaths: false, + }, + outputType: MigrationOutput.JsonObject, + extraTrackProcessing: (parsedTrack) => { + const outputTrack = {...parsedTrack}; + if (!parsedTrack.hasOwnProperty(deletedProperty)) { + throw new Error( + `Original track didn't have ${deletedProperty} anyway, can't test deleting it.`, + ); + } + delete outputTrack[deletedProperty]; + return outputTrack; + }, + }); + + return Object.keys(outputObject.Tracks).every((trackKey) => { + const track = outputObject.Tracks[trackKey]; + + if (!track) { + throw new Error(`Missing track for ${trackKey}`); + } + + return !track.hasOwnProperty(deletedProperty); + }); }, });