Skip to content

Commit

Permalink
more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwilliams committed Aug 19, 2021
1 parent 4da660d commit 701dd6c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
7 changes: 3 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"name": "vscode-jest-tests",
"request": "launch",
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
"args": [
"--runInBand"
],
"args": ["--runInBand"],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand All @@ -25,7 +23,8 @@
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"sourceMaps": true
"sourceMaps": true,
"preLaunchTask": "npm: watch"
},
{
"name": "Extension Tests",
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "anki" extension will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [1.2.1]

- Added more diagnostics and logging surrounding the bug of missing note types

## [1.2.0]

- Added support for Math/Latex syntax
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "anki",
"displayName": "Anki for VSCode",
"description": "Sync notes with your locally running Anki",
"version": "1.2.0",
"version": "1.2.1",
"publisher": "jasew",
"engines": {
"vscode": "^1.47.0"
Expand Down
12 changes: 12 additions & 0 deletions src/AnkiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,16 @@ export class AnkiService {
return newCard;
});
}

// Is Anki running?
async isUp(): Promise<boolean> {
try {
// There's no native "isUp" function but modelNames is a safe
await this.modelNames();
} catch(e) {
return false;
}

return true;
}
}
34 changes: 27 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { subscriptions } from "./subscriptions";
import { AnkiFS, initFilesystem } from "./fileSystemProvider";
import { initState, getAnkiState } from "./state";
import { initialSetup } from "./initialSetup";
import { isTemplateInstalled, updateTemplate } from "./manageTemplate";

require("./resources/vscodeAnkiPlugin.scss");

Expand All @@ -42,7 +43,7 @@ export interface IContext {

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: ExtensionContext) {
export async function activate(context: ExtensionContext) {
// config
const schema = workspace.getConfiguration("anki.api").get("schema");
const hostname = workspace.getConfiguration("anki.api").get("hostname");
Expand All @@ -60,20 +61,21 @@ export function activate(context: ExtensionContext) {

const ankiExt = extensions.getExtension("jasew.anki");
const extMeta = ankiExt?.packageJSON;

// Set up logging
const logOutputChannel = window.createOutputChannel(extMeta.displayName);

const extLogger = getExtensionLogger({
extName: extMeta.displayName,
level: config.log,
logPath: context.logPath,
logOutputChannel: logOutputChannel,
});

// Initialize logger
initLogger(extLogger);


getLogger().info(`Anki Extension v${extMeta?.version} activated`);
const extContext: IContext = {
ankiService,
logger: extLogger,
Expand All @@ -82,15 +84,33 @@ export function activate(context: ExtensionContext) {
extMeta,
};

// There have been issues with the template being deleted from Anki, but the extension not knowing about it.
// We will have to check on every activation to see if its still there
// Check if ANKI is running and see if note type is installed
const isUp = await ankiService.isUp();
let templateInstalled: boolean;
if (isUp) {
getLogger().info('Anki is running, checking for note type..');
templateInstalled = await isTemplateInstalled(extContext);
getLogger().info(`Status of note type on Anki: ${templateInstalled}`);
if (!templateInstalled) {
await updateTemplate(extContext);
}
} else {
getLogger().info('Could not connect to Anki');
}

// Check to see if we need to upload assets into Anki
// If the extension has updated, that is a good time to re-upload
const globalStateVersion = context.globalState.get<string>("installedVersion") ?? "0.0.0";
getLogger().info(`Checking extension version against cache: Extension: ${extMeta.version}, Cache: ${globalStateVersion}`);
if (
semver.gt(
extMeta.version,
context.globalState.get("installedVersion") ?? "0.0.0"
globalStateVersion
)
) {
getLogger().info(`new version detected (${extMeta.version}), setting up`);
getLogger().info(`new version detected (${extMeta.version}), setting up...`);
initialSetup(extContext);
}

Expand Down
9 changes: 6 additions & 3 deletions src/manageTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export async function updateTemplate(ctx: IContext) {
}
}

// Is our note type installed on Anki?
export async function isTemplateInstalled(ctx: IContext): Promise<boolean> {
const modelNames: string[] = await ctx.ankiService.modelNames();
return modelNames.includes(CONSTANTS.defaultTemplateName);
}
/**
* Check if the template exists, if not create it, if it does exist update it
*/
Expand All @@ -39,9 +44,7 @@ export async function createOrUpdateTemplate(ctx: IContext) {
`Checking if ${CONSTANTS.defaultTemplateName} exists as a model in Anki`
);

const modelNames: string[] = await ctx.ankiService.modelNames();

if (modelNames.includes(CONSTANTS.defaultTemplateName)) {
if (await isTemplateInstalled(ctx)) {
getLogger().info(`${CONSTANTS.defaultTemplateName} found in Anki`);
await updateTemplate(ctx);
return;
Expand Down

0 comments on commit 701dd6c

Please sign in to comment.