Skip to content

Commit

Permalink
feat: report details during the sync
Browse files Browse the repository at this point in the history
So the cli could output the details.
  • Loading branch information
timgreen committed Mar 10, 2023
1 parent f3afd3a commit 33b9446
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
31 changes: 30 additions & 1 deletion packages/core/src/actions/ankiconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ApiOrigin,
} from "@autoanki/anki-connect";

import { IDeck, INoteType } from "../model";
import { IDeck, INoteType, MediaInfo } from "../model";

function createDefaultCardTemplateForNoteType(
noteType: INoteType,
Expand All @@ -31,9 +31,25 @@ export interface SyncConfig {
origin?: ApiOrigin;
}

export interface Reporter {
startModelCreation: () => void;
endModelCreation: () => void;
createdModel: (modelName: string) => void;
updatedModelTemplates: (modelName: string) => void;
updatedModelStyling: (modelName: string) => void;
startStoreMedia: () => void;
endStoreMedia: () => void;
storingMedia: (mediaName: string, mediaInfo: MediaInfo) => void;
startNotes: () => void;
endNotes: (newInserted: number) => void;
increaseUpdatedNote: (total: number) => void;
insertingNotes: (total: number) => void;
}

export async function ankiConnectSync(
deck: IDeck,
config?: SyncConfig,
reporter?: Reporter,
): Promise<Array<NoteTypes.NoteId | undefined>> {
await invoke({
action: "createDeck",
Expand All @@ -54,6 +70,7 @@ export async function ankiConnectSync(
existingModelNames.add(modelName);
}

reporter?.startModelCreation();
// createModel defined in the frontmatter
const models = deck.frontmatterConfig?.models || {};
for (const modelName in models) {
Expand All @@ -74,6 +91,7 @@ export async function ankiConnectSync(
},
origin: config?.origin,
});
reporter?.createdModel(modelName);
existingModelNames.add(modelName);
} else {
if (config?.updateModelTemplates) {
Expand All @@ -88,6 +106,7 @@ export async function ankiConnectSync(
},
origin: config?.origin,
});
reporter?.updatedModelTemplates(modelName);
}
if (config?.updateModelStyling && model.css) {
await invoke({
Expand All @@ -101,6 +120,7 @@ export async function ankiConnectSync(
},
origin: config?.origin,
});
reporter?.updatedModelStyling(modelName);
}
}
}
Expand All @@ -120,11 +140,14 @@ export async function ankiConnectSync(
},
origin: config?.origin,
});
reporter?.createdModel(note.modelName);
existingModelNames.add(note.modelName);
}
}
reporter?.endModelCreation();

// store medias
reporter?.startStoreMedia();
const skipMediaNames: Set<string> = new Set(
config?.overwriteExistingMedias
? []
Expand All @@ -142,6 +165,7 @@ export async function ankiConnectSync(
continue;
}
const media = note.medias[mediaName];
reporter?.storingMedia(mediaName, media);
await invoke({
action: "storeMediaFile",
version: 6,
Expand All @@ -154,7 +178,9 @@ export async function ankiConnectSync(
});
}
}
reporter?.endStoreMedia();

reporter?.startNotes();
const notesToUpdate = deck.notes.filter((note) => note.noteId);
const notesToInsert = deck.notes.filter((note) => !note.noteId);

Expand All @@ -170,8 +196,10 @@ export async function ankiConnectSync(
},
origin: config?.origin,
});
reporter?.increaseUpdatedNote(notesToUpdate.length);
}

reporter?.insertingNotes(notesToInsert.length);
const newNoteIds = await invoke({
action: "addNotes",
version: 6,
Expand All @@ -190,6 +218,7 @@ export async function ankiConnectSync(
notesToInsert[index].noteId = newNoteId;
}
});
reporter?.endNotes(newNoteIds.filter((id) => !!id).length);

return deck.notes.map((n) => n.noteId);
}
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ankiConnectSync } from "./actions/ankiconnect";
export { ankiConnectSync, Reporter } from "./actions/ankiconnect";
export * as model from "./model";
export { updateNoteId } from "./actions/note-id";
export { Parser } from "./parser";

0 comments on commit 33b9446

Please sign in to comment.