Skip to content

Commit

Permalink
refactor: use tslog for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Oct 4, 2024
1 parent bc94f86 commit 37ca281
Show file tree
Hide file tree
Showing 20 changed files with 145 additions and 173 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
},
"dependencies": {
"dedent": "^1.5.3",
"ts-deepmerge": "^6.2.1"
"ts-deepmerge": "^6.2.1",
"tslog": "^4.9.3"
},
"peerDependencies": {
"@types/electron": "npm:@ophidian/electron-types@^24.3.1"
Expand Down
29 changes: 15 additions & 14 deletions src/GitHub/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class GithubBranch extends FilesManagement {
constructor(octokit: Octokit, plugin: Enveloppe) {
super(octokit, plugin);
this.console = plugin.console;

}

/**
Expand Down Expand Up @@ -45,6 +46,7 @@ export class GithubBranch extends FilesManagement {
throw new Error(
`No main branch found for ${prop.repo}, please check the branch name in the settings`
);

}
try {
const shaMainBranch = mainBranch!.commit.sha;
Expand All @@ -54,8 +56,7 @@ export class GithubBranch extends FilesManagement {
ref: `refs/heads/${this.branchName}`,
sha: shaMainBranch,
});
this.console.notif(
{},
this.console.info(
i18next.t("publish.branch.success", { branchStatus: branch.status, repo: prop })
);
return branch.status === 201;
Expand All @@ -72,16 +73,15 @@ export class GithubBranch extends FilesManagement {
const mainBranch = allBranch.data.find(
(branch: { name: string }) => branch.name === this.branchName
);
this.console.notif(
{},
this.console.info(
i18next.t("publish.branch.alreadyExists", {
branchName: this.branchName,
repo: prop,
})
);
return !!mainBranch;
} catch (e) {
this.console.notif({ e: true }, e);
this.console.error(e);
return false;
}
}
Expand All @@ -106,7 +106,7 @@ export class GithubBranch extends FilesManagement {
});
return pr.data.number;
} catch (e) {
this.console.logs({ e: true }, e);
this.console.trace(e);
//trying to get the last open PR number
try {
const pr = await this.octokit.request("GET /repos/{owner}/{repo}/pulls", {
Expand All @@ -117,8 +117,7 @@ export class GithubBranch extends FilesManagement {
return pr.data[0]?.number || 0;
} catch (e) {
// there is no open PR and impossible to create a new one
this.console.notif(
{ e: true },
this.console.info(
i18next.t("publish.branch.error", { error: e, repo: prop })
);
return 0;
Expand All @@ -144,7 +143,7 @@ export class GithubBranch extends FilesManagement {
);
return branch.status === 200;
} catch (e) {
this.console.logs({ e: true }, e);
this.console.trace(e);
return false;
}
}
Expand Down Expand Up @@ -177,7 +176,7 @@ export class GithubBranch extends FilesManagement {
);
return branch.status === 200;
} catch (e) {
this.console.notif({ e: true }, e);
this.console.warn(e);
new Notice(i18next.t("error.mergeconflic"));
return false;
}
Expand Down Expand Up @@ -222,7 +221,7 @@ export class GithubBranch extends FilesManagement {
}
return true;
} catch (e) {
this.console.logs({ e: true }, e);
this.console.warn(e);
new Notice(i18next.t("error.errorConfig", { repo: prop }));
return false;
}
Expand All @@ -241,6 +240,7 @@ export class GithubBranch extends FilesManagement {
async checkRepository(prop: Properties | Properties[], silent = true): Promise<void> {
prop = Array.isArray(prop) ? prop : [prop];
for (const repo of prop) {
this.console.logger.silly(repo);
try {
const repoExist = await this.octokit
.request("GET /repos/{owner}/{repo}", {
Expand All @@ -259,8 +259,7 @@ export class GithubBranch extends FilesManagement {
});
//@ts-ignore
if (repoExist.status === 200) {
this.console.notif(
{},
this.console.info(
i18next.t("commands.checkValidity.repoExistsTestBranch", { repo })
);

Expand All @@ -272,6 +271,7 @@ export class GithubBranch extends FilesManagement {
})
.catch((e) => {
//check the error code
this.console.logger.warn(e);
if (e.status === 404) {
new Notice(
i18next.t("commands.checkValidity.inBranch.error404", { repo })
Expand All @@ -282,13 +282,14 @@ export class GithubBranch extends FilesManagement {
);
}
});
this.console.logger.info(branchExist);
//@ts-ignore
if (branchExist.status === 200 && !silent) {
new Notice(i18next.t("commands.checkValidity.success", { repo }));
}
}
} catch (e) {
this.console.logs({ e: true }, e);
this.console.logger.fatal(e);
new Notice(i18next.t("commands.checkValidity.error", { repo }));
break;
}
Expand Down
18 changes: 8 additions & 10 deletions src/GitHub/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function deleteFromGithubOneRepo(
return { success: false, deleted: [], undeleted: [] };
}
if (filesInRepo.length === 0) {
pconsole.logs({}, `No file to delete in ${repo.owner}/${repo.repo}`);
pconsole.trace(`No file to delete in ${repo.owner}/${repo.repo}`);
return { success: false, deleted: [], undeleted: [] };
}
const allSharedFiles = filesManagement.getAllFileWithPath(
Expand Down Expand Up @@ -126,11 +126,10 @@ async function deleteFromGithubOneRepo(
: false;
try {
if (!checkingIndex) {
pconsole.notif(
{},
pconsole.trace(
`trying to delete file : ${file.file} from ${repo.owner}/${repo.repo}`
);
const reponse = await octokit.request(
const response = await octokit.request(
"DELETE /repos/{owner}/{repo}/contents/{path}",
{
owner: repo.owner,
Expand All @@ -141,7 +140,7 @@ async function deleteFromGithubOneRepo(
branch: branchName,
}
);
if (reponse.status === 200) {
if (response.status === 200) {
deletedSuccess++;
result.deleted.push(file.file);
} else {
Expand All @@ -150,7 +149,7 @@ async function deleteFromGithubOneRepo(
}
}
} catch (e) {
if (!(e instanceof DOMException)) pconsole.logs({ e: true }, e);
if (!(e instanceof DOMException)) pconsole.fatal(true, e);
}
}
}
Expand Down Expand Up @@ -288,7 +287,7 @@ async function checkIndexFiles(
}
} catch (e) {
if (!(e instanceof DOMException)) {
plugin.console.notif({ e: true }, e);
plugin.console.fatal(e);
return false;
}
}
Expand Down Expand Up @@ -362,9 +361,8 @@ function cleanDryRun(
? indexFileDryRun(file as TFile, app.metadataCache)
: false;
if (!indexFile) {
console.notif(
{},
`[DRYRUN] trying to delete file : ${file.path} from ${dryRunFolderPath}`
console.trace(
`[DRY-RUN] trying to delete file : ${file.path} from ${dryRunFolderPath}`
);
vault.trash(file, false);
deletedSuccess++;
Expand Down
15 changes: 7 additions & 8 deletions src/GitHub/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class FilesManagement extends Publisher {
sharedFile.push(file);
}
} catch (e) {
this.console.logs({ e: true }, e);
this.console.trace(e);
}
}
return sharedFile;
Expand Down Expand Up @@ -75,7 +75,7 @@ export class FilesManagement extends Publisher {
files.push(file as TFile);
}
} catch (e) {
this.console.logs({ e: true }, e);
this.console.trace(e);
}
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@ export class FilesManagement extends Publisher {
linkedFiles.push(thisLinkedFile);
}
} catch (e) {
this.console.logs({}, e);
this.console.trace(e);
}
}
}
Expand Down Expand Up @@ -283,7 +283,7 @@ export class FilesManagement extends Publisher {
embedList.push(thisEmbed);
}
} catch (e) {
this.console.logs({ e: true }, `Error with this links : ${embedCache.link}`, e);
this.console.debug(`Error with this links : ${embedCache.link}`, e);
}
}
return [...new Set(embedList)];
Expand Down Expand Up @@ -345,7 +345,7 @@ export class FilesManagement extends Publisher {
fromWhat
) as TFile;
} catch (e) {
this.console.logs({ e: true }, `Error with this file : ${embed.displayText}`, e);
this.console.debug(`Error with this file : ${embed.displayText}`, e);
}
return undefined;
}
Expand Down Expand Up @@ -409,7 +409,7 @@ export class FilesManagement extends Publisher {
}
}
} catch (e) {
this.console.logs({ e: true }, e);
this.console.debug(e);
}
return filesInRepo;
}
Expand Down Expand Up @@ -596,8 +596,7 @@ export class FilesManagement extends Publisher {
if (fileInVault && isMarkdown) {
const vaultEditedTime = new Date(fileInVault.stat.mtime);
if (repoEditedTime && vaultEditedTime > repoEditedTime) {
this.console.logs(
{},
this.console.debug(
`edited file : ${fileInVault.path} / ${vaultEditedTime} vs ${repoEditedTime}`
);
newFiles.push(fileInVault);
Expand Down
30 changes: 14 additions & 16 deletions src/GitHub/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ export default class Publisher {
} catch (e) {
new Notice(i18next.t("error.unablePublishNote", { file: file.name }));
fileError.push(file.name);
this.console.logs({ e: true }, e);
this.console.fatal(e);
}
}
statusBar.finish(8000);
} catch (e) {
this.console.logs({ e: true }, e);
this.console.notifError(prop);
this.console.fatal(e);
this.console.noticeErrorUpload(prop);
statusBar.error(prop);
}
}
Expand Down Expand Up @@ -190,7 +190,7 @@ export default class Publisher {
return false;
}
try {
this.console.logs({}, `Publishing file: ${file.path}`);
this.console.fatal(`Publishing file: ${file.path}`);
fileHistory.push(file);
const frontmatterSettingsFromFile = getFrontmatterSettings(
frontmatter,
Expand Down Expand Up @@ -235,7 +235,7 @@ export default class Publisher {
multiRepMsg += `[${repo.owner}/${repo.repo}/${repo.branch}] `;
}
const msg = `Publishing ${file.name} to ${multiRepMsg}`;
this.console.logs({}, msg);
this.console.trace(msg);
const fileDeleted: Deleted[] = [];
const updated: UploadedFiles[][] = [];
const fileError: string[] = [];
Expand Down Expand Up @@ -268,7 +268,7 @@ export default class Publisher {
}
return { deleted: fileDeleted[0], uploaded: updated[0], error: fileError };
} catch (e) {
this.console.logs({ e: true }, e);
this.console.fatal(e);
return false;
}
}
Expand Down Expand Up @@ -307,8 +307,7 @@ export default class Publisher {
});
embedFiles = await this.cleanLinkedImageIfAlreadyInRepo(embedFiles, properties);
const repo = properties.frontmatter.prop;
this.console.notif(
{},
this.console.trace(
`Upload ${file.name}:${path} on ${repo.owner}/${repo.repo}:${this.branchName}`
);
const notifMob = this.console.noticeMobile(
Expand Down Expand Up @@ -338,14 +337,14 @@ export default class Publisher {
],
};
}
const embeded = await this.statusBarForEmbed(
const embedded = await this.statusBarForEmbed(
embedFiles,
fileHistory,
deepScan,
properties
);

const embeddedUploaded = embeded.uploaded;
const embeddedUploaded = embedded.uploaded;
embeddedUploaded.push(uploaded);
if (autoclean || repo.dryRun.autoclean) {
deleted = await deleteFromGithub(true, this.branchName, shareFiles, {
Expand All @@ -357,7 +356,7 @@ export default class Publisher {
return {
deleted,
uploaded: embeddedUploaded,
error: embeded.error,
error: embedded.error,
};
}

Expand Down Expand Up @@ -418,7 +417,7 @@ export default class Publisher {
result.isUpdated = true;
}
} catch {
this.console.logs({}, i18next.t("error.normal"));
this.console.trace(i18next.t("error.normal"));
}

payload.message = msg;
Expand Down Expand Up @@ -524,7 +523,7 @@ export default class Publisher {
const contentBase64 = Base64.encode(text).toString();
return await this.upload(contentBase64, path, title, prop);
} catch (e) {
this.console.notif({ e: true }, e);
this.console.fatal(e);
return undefined;
}
}
Expand Down Expand Up @@ -667,8 +666,7 @@ export default class Publisher {
) {
newLinkedFiles.push(file);
} else
this.console.logs(
{},
this.console.trace(
i18next.t("error.alreadyExists", { file: file.name })
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/purge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function purge(
if (PublisherManager.settings.plugin.displayModalRepoEditing)
new ListChangedFiles(PublisherManager.plugin.app, deleted).open();
} catch (e) {
PublisherManager.plugin.console.notif({ e: true }, e);
PublisherManager.plugin.console.fatal(e);
}
}

Expand Down
Loading

0 comments on commit 37ca281

Please sign in to comment.