Skip to content

Commit

Permalink
refactor(logs): refactors logs handle
Browse files Browse the repository at this point in the history
also fix #213
  • Loading branch information
Mara-Li committed Aug 28, 2023
1 parent bf0145a commit 9834393
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 94 deletions.
28 changes: 15 additions & 13 deletions src/GitHub/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
GitHubPublisherSettings,
RepoFrontmatter,
} from "../settings/interface";
import { noticeLog } from "../utils";
import { logs, noticeLog } from "../utils";
import { FilesManagement } from "./files";


Expand Down Expand Up @@ -79,14 +79,15 @@ export class GithubBranch extends FilesManagement {
sha: shaMainBranch,
}
);
noticeLog(i18next.t("publish.branch.success", {branchStatus: branch.status, repo: repoFrontmatter}),
this.settings
noticeLog(
this.settings,
i18next.t("publish.branch.success", {branchStatus: branch.status, repo: repoFrontmatter})
);
return branch.status === 201;
} catch (e) {
// catch the old branch
try {
noticeLog(e, this.settings);
logs(this.settings, e);
const allBranch = await this.octokit.request(
"GET /repos/{owner}/{repo}/branches",
{
Expand All @@ -97,10 +98,10 @@ export class GithubBranch extends FilesManagement {
const mainBranch = allBranch.data.find(
(branch: { name: string }) => branch.name === branchName
);
noticeLog(i18next.t("publish.branch.alreadyExists", {branchName, repo: repoFrontmatter}), this.settings);
noticeLog(this.settings, i18next.t("publish.branch.alreadyExists", {branchName, repo: repoFrontmatter}));
return !!mainBranch;
} catch (e) {
noticeLog(e, this.settings);
logs(this.settings, e);
return false;
}
}
Expand Down Expand Up @@ -132,10 +133,10 @@ export class GithubBranch extends FilesManagement {
);
return PR.data.number;
} catch (e) {
noticeLog(e, this.settings);
logs(this.settings, e);
try {
const PR = await this.octokit.request(
"GET" + " /repos/{owner}/{repo}/pulls",
"GET /repos/{owner}/{repo}/pulls",
{
owner: repoFrontmatter.owner,
repo: repoFrontmatter.repo,
Expand All @@ -145,9 +146,10 @@ export class GithubBranch extends FilesManagement {
return PR.data[0].number;
} catch (e) {
noticeLog(
i18next.t("publish.branch.error", {error: e, repo: repoFrontmatter}),
this.settings
this.settings,
i18next.t("publish.branch.error", {error: e, repo: repoFrontmatter})
);
logs(this.settings, e);
return 0;
}
}
Expand Down Expand Up @@ -207,7 +209,7 @@ export class GithubBranch extends FilesManagement {
);
return branch.status === 200;
} catch (e) {
noticeLog(e, this.settings);
logs(this.settings, e);
new Notice(i18next.t("error.mergeconflic"));
return false;
}
Expand Down Expand Up @@ -262,7 +264,7 @@ export class GithubBranch extends FilesManagement {
}
return true;
} catch (e) {
noticeLog(e, this.settings);
logs(this.settings, e);
new Notice(i18next.t("error.errorConfig", {repo: repoFrontmatter})
);
return false;
Expand Down Expand Up @@ -311,7 +313,7 @@ export class GithubBranch extends FilesManagement {
});
//@ts-ignore
if (repoExist.status === 200) {
noticeLog(i18next.t("commands.checkValidity.repoExistsTestBranch", {repo: repo}), this.settings);
noticeLog(this.settings, i18next.t("commands.checkValidity.repoExistsTestBranch", {repo}));

const branchExist = await this.octokit.request("GET /repos/{owner}/{repo}/branches/{branch}", {
owner: repo.owner,
Expand Down
12 changes: 6 additions & 6 deletions src/GitHub/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
MonoRepoProperties,
RepoFrontmatter,
} from "../settings/interface";
import { noticeLog, trimObject, verifyRateLimitAPI} from "../utils";
import { logs, noticeLog, trimObject, verifyRateLimitAPI} from "../utils";
import {isAttachment} from "../utils/data_validation_test";
import { FilesManagement } from "./files";

Expand Down Expand Up @@ -128,11 +128,11 @@ async function deleteFromGithubOneRepo(
try {
if (!checkingIndex) {
noticeLog(
`trying to delete file : ${file.file} from ${repo.owner}/${repo.repo}`,
settings
settings,
`trying to delete file : ${file.file} from ${repo.owner}/${repo.repo}`
);
const reponse = await octokit.request(
"DELETE" + " /repos/{owner}/{repo}/contents/{path}",
"DELETE /repos/{owner}/{repo}/contents/{path}",
{
owner: repo.owner,
repo: repo.repo,
Expand All @@ -151,7 +151,7 @@ async function deleteFromGithubOneRepo(
}
}
} catch (e) {
if (!(e instanceof DOMException)) noticeLog(e, settings);
if (!(e instanceof DOMException)) logs(settings, e);
}
}
}
Expand Down Expand Up @@ -300,7 +300,7 @@ async function checkIndexFiles(
}
} catch (e) {
if (!(e instanceof DOMException)) {
noticeLog(e, settings);
logs(settings, e);
return false;
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
LinkedNotes,
RepoFrontmatter, Repository,
} from "../settings/interface";
import {getRepoFrontmatter, log, noticeLog} from "../utils";
import {getRepoFrontmatter, logs} from "../utils";
import { isAttachment, isShared } from "../utils/data_validation_test";
import Publisher from "./upload";

Expand Down Expand Up @@ -85,7 +85,7 @@ export class FilesManagement extends Publisher {
}
}

log(files);
logs(this.settings, files);
return files;
}

Expand Down Expand Up @@ -228,10 +228,10 @@ export class FilesManagement extends Publisher {
embedList.push(thisEmbed);
}
} catch (e) {
noticeLog(e, this.settings);
noticeLog(
`Error with this links : ${embedCache.link}`,
this.settings
logs(this.settings, e);
logs(
this.settings,
`Error with this links : ${embedCache.link}`
);
}
}
Expand Down Expand Up @@ -264,10 +264,10 @@ export class FilesManagement extends Publisher {
);
if (imageLink) imageList.push(this.imageSharedOrNote(imageLink as TFile, frontmatterSourceFile) as TFile);
} catch (e) {
noticeLog(e, this.settings);
noticeLog(
`Error with this file : ${embed.displayText}`,
this.settings
logs(this.settings, e);
logs(
this.settings,
`Error with this file : ${embed.displayText}`
);
}
}
Expand Down Expand Up @@ -337,7 +337,7 @@ export class FilesManagement extends Publisher {
}
}
} catch (e) {
noticeLog(e, this.settings);
logs(this.settings, e);
}
return filesInRepo;
}
Expand Down Expand Up @@ -525,9 +525,9 @@ export class FilesManagement extends Publisher {
) {
const vaultEditedTime = new Date(fileInVault.stat.mtime);
if (repoEditedTime && vaultEditedTime > repoEditedTime) {
noticeLog(
`edited file : ${fileInVault.path} / ${vaultEditedTime} vs ${repoEditedTime}`,
this.settings
logs(
this.settings,
`edited file : ${fileInVault.path} / ${vaultEditedTime} vs ${repoEditedTime}`
);
newFiles.push(fileInVault);
}
Expand Down
17 changes: 9 additions & 8 deletions src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import {
getFrontmatterSettings,
getRepoFrontmatter,
logs,
noticeLog,
} from "../utils";
import {
Expand Down Expand Up @@ -141,7 +142,7 @@ export default class Publisher {
}
statusBar.finish(8000);
} catch (e) {
noticeLog(e, this.settings);
logs(this.settings, e);
new Notice(
(i18next.t("error.errorPublish", {repo: repoFrontmatter}))
);
Expand Down Expand Up @@ -225,7 +226,7 @@ export default class Publisher {
return false;
}
try {
noticeLog("Publishing file: " + file.path, this.settings);
noticeLog(this.settings, `Publishing file: ${file.path}`);
fileHistory.push(file);
const frontmatterSettings = getFrontmatterSettings(
frontmatter,
Expand Down Expand Up @@ -262,7 +263,7 @@ export default class Publisher {
multiRepMsg += `[${repo.owner}/${repo.repo}/${repo.branch}] `;
}
const msg = `Publishing ${file.name} to ${multiRepMsg}`;
noticeLog(msg, this.settings);
noticeLog(this.settings, msg);
const fileDeleted: Deleted[] = [];
const updated: UploadedFiles[][] = [];
const fileError: string[] = [];
Expand Down Expand Up @@ -295,7 +296,7 @@ export default class Publisher {
}
return {deleted: fileDeleted[0], uploaded: updated[0], error: fileError};
} catch (e) {
noticeLog(e, this.settings);
logs(this.settings, e);
return false;
}
}
Expand Down Expand Up @@ -328,8 +329,8 @@ export default class Publisher {
) {
const repo = properties.frontmatter.repo;
noticeLog(
`Upload ${file.name}:${path} on ${repo.owner}/${repo.repo}:${branchName}`,
this.settings
this.settings,
`Upload ${file.name}:${path} on ${repo.owner}/${repo.repo}:${branchName}`
);
let deleted: Deleted = {
success: false,
Expand Down Expand Up @@ -437,8 +438,8 @@ export default class Publisher {
}
} catch {
noticeLog(
i18next.t("error.normal"),
this.settings
this.settings,
i18next.t("error.normal")
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createListEdited,
getRepoFrontmatter,
getSettingsOfMetadataExtractor,
logs,
noticeLog,
noticeMessage} from "../utils";
import {checkRepositoryValidityWithRepoFrontmatter} from "../utils/data_validation_test";
Expand Down Expand Up @@ -232,7 +233,7 @@ export async function shareOneNote(
}
} catch (error) {
if (!(error instanceof DOMException)) {
noticeLog(error, settings);
logs(settings, error);
new Notice(
(i18next.t("error.errorPublish", {repo: getRepoFrontmatter(settings, repository, metadataCache.getFileCache(file)?.frontmatter)}))
);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/suggest_other_repo_commands_modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defaultRepo } from "src/utils/data_validation_test";

import GithubPublisherPlugin from "../main";
import {FolderSettings, Repository} from "../settings/interface";
import {noticeLog} from "../utils";
import {logs} from "../utils";
import {
createLinkOnActiveFile,
deleteCommands, repositoryValidityActiveFile, shareActiveFile,
Expand Down Expand Up @@ -188,7 +188,7 @@ export class SuggestOtherRepoCommandsModal extends FuzzySuggestModal<GithubPubli
repositoryValidityActiveFile(this.plugin, this.branchName, this.repo);
break;
}
noticeLog(`run command ${item.commands}`, this.plugin.settings);
logs(this.plugin.settings, `run command ${item.commands}`);
}
}

10 changes: 5 additions & 5 deletions src/conversion/convert_text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
LinkedNotes,
MultiProperties,
} from "../settings/interface";
import {log, noticeLog} from "../utils";
import {logs} from "../utils";
import {bakeEmbeds} from "./bakeEmbed";
import {getDataviewPath} from "./file_path";
import findAndReplaceText from "./find_and_replace_text";
Expand Down Expand Up @@ -48,7 +48,7 @@ export function addHardLineBreak(
}
return text;
} catch (e) {
noticeLog(e, settings);
logs(settings, e);
return text;
}
}
Expand Down Expand Up @@ -77,7 +77,7 @@ async function addTagsToYAML(text: string, toAdd: string[], settings: GitHubPubl
];
delete yamlObject.tag;
} catch (e) {
noticeLog(e, settings);
logs(settings, e);
}
}
if (yamlObject.tags) {
Expand All @@ -91,7 +91,7 @@ async function addTagsToYAML(text: string, toAdd: string[], settings: GitHubPubl
]),
];
} catch (e) {
noticeLog(e, settings);
logs(settings, e);
}
} else {
yamlObject.tags = toAdd;
Expand Down Expand Up @@ -269,7 +269,7 @@ export async function convertDataviewQueries(
const inlineJsDataViewRegex = new RegExp(`\`${escapeRegex(inlineJsQueryPrefix)}(.+?)\``, "gsm");
const inlineJsMatches = text.matchAll(inlineJsDataViewRegex);
if (!matches && !inlineMatches && !dataviewJsMatches && !inlineJsMatches) {
log("No dataview queries found");
logs(properties.settings, "No dataview queries found");
return replacedText;
}
const error = i18next.t("error.dataview");
Expand Down
11 changes: 11 additions & 0 deletions src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@
},
"title": "Copy link"
},
"dev": {
"desc": "Advanced user only. \nWill display as \"Notice\" according to the previous setting.",
"title": "Display developper logs"
},
"editorMenu": {
"desc": "Add a sharing commands in the right-click menu",
"title": "Editor Menu"
Expand All @@ -427,6 +431,13 @@
"desc": "Add an sharing commands in the file menu",
"title": "File Menu"
},
"head": {
"copyLinks": "Link building & copy",
"log": "Logs",
"menu": "Menu",
"other": "Others",
"share": "Share & sharing key"
},
"logNoticeHeader": {
"desc": "On mobile, it can be hard to debug the plugin. Enable this option to log every error in a Notice.",
"title": "Notice every error"
Expand Down
Loading

0 comments on commit 9834393

Please sign in to comment.