Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
add custom track processing input
Browse files Browse the repository at this point in the history
  • Loading branch information
electrovir committed Jul 3, 2021
1 parent 128967a commit c197059
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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",
Expand Down
7 changes: 5 additions & 2 deletions src/api/api-types.ts
@@ -1,3 +1,5 @@
import {ParsedTrack} from '../migration/reading/parsed-types';

export type ReplacePath = {
old: string;
new: string;
Expand All @@ -16,13 +18,13 @@ export enum MigrationOutput {
PlistString = 'plist-string',
}

export type RunTimeOptions = {
export interface RunTimeOptions extends Record<string, boolean> {
validationEnabled: boolean;
loggingEnabled: boolean;
checkReplacementPaths: boolean;
checkFiles: boolean;
removeRatingComputed: boolean;
};
}

export const defaultOptions: RunTimeOptions = {
validationEnabled: true,
Expand All @@ -37,4 +39,5 @@ export type MigrationApiInput<OutputType extends MigrationOutput = MigrationOutp
replacePaths: Readonly<Readonly<InputPath>[]>;
outputType?: OutputType;
options?: Readonly<Partial<RunTimeOptions>>;
extraTrackProcessing?: (updatedTrack: Readonly<ParsedTrack>) => Readonly<ParsedTrack>;
};
6 changes: 4 additions & 2 deletions 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';
Expand All @@ -13,13 +13,15 @@ export function makeNewLibrary({
loggingEnabled = true,
checkFiles = false,
removeRatingComputed = false,
extraTrackProcessing = (track) => track,
}: Readonly<{
oldLibrary: Readonly<ParsedLibrary>;
replacePaths: Readonly<Readonly<InputPath>[]>;
checkReplacementPaths?: boolean;
loggingEnabled?: boolean;
checkFiles?: boolean;
removeRatingComputed?: boolean;
extraTrackProcessing?: MigrationApiInput['extraTrackProcessing'];
}>): Readonly<ParsedLibrary> {
const unreplacedPaths = new Set<string>();
const replacePathUsage = replacePaths.map(() => 0);
Expand Down Expand Up @@ -117,7 +119,7 @@ export function makeNewLibrary({
}

if (!markedForDeletion) {
newTracks[trackKey] = rawNewTrack;
newTracks[trackKey] = extraTrackProcessing(rawNewTrack);
}

return newTracks;
Expand Down
6 changes: 6 additions & 0 deletions src/migration/migrate-library.ts
Expand Up @@ -9,30 +9,35 @@ export function migrateLibrary({
replacePaths,
outputType,
options: rawOptions,
extraTrackProcessing,
}: MigrationApiInput<MigrationOutput.JsonObject>): ParsedLibrary;
export function migrateLibrary({
libraryFilePath,
replacePaths,
outputType,
options: rawOptions,
extraTrackProcessing,
}: MigrationApiInput<MigrationOutput.WriteToFile>): {filePath: string};
export function migrateLibrary({
libraryFilePath,
replacePaths,
outputType,
options: rawOptions,
extraTrackProcessing,
}: MigrationApiInput<MigrationOutput.PlistString>): {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};

Expand All @@ -44,6 +49,7 @@ export function migrateLibrary({
const newLibrary = makeNewLibrary({
oldLibrary,
replacePaths,
extraTrackProcessing,
...options,
});

Expand Down
46 changes: 43 additions & 3 deletions src/tests/migrate-library.test.ts
Expand Up @@ -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);
},
});
Expand All @@ -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);
});
},
});

Expand Down

0 comments on commit c197059

Please sign in to comment.