Skip to content

Commit

Permalink
Lint: add spaces after blocks (#418)
Browse files Browse the repository at this point in the history
* lint: define rule

* lint: format files

---------

Co-authored-by: Ole Eskild Steensen <[email protected]>
  • Loading branch information
julesvirallinen and oleeskild authored Sep 25, 2023
1 parent b1dc981 commit 8dd3b87
Show file tree
Hide file tree
Showing 18 changed files with 330 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@
"@typescript-eslint/no-explicit-any": ["error"],
// allow prettier to use SmartTabs
"no-mixed-spaces-and-tabs": "off",
"padding-line-between-statements": [
"warn",
{
"blankLine": "always",
"prev": "*",
"next": [
"return",
"if",
"multiline-const",
"function",
"multiline-expression",
"multiline-let",
"block-like"
]
},
{
"blankLine": "always",
"prev": ["function"],
"next": "*"
}
],
"svelte/no-at-html-tags": "off"
}
}
30 changes: 30 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default class DigitalGarden extends Plugin {
await this.addCommands();

addIcon("digital-garden-icon", seedling);

this.addRibbonIcon(
"digital-garden-icon",
"Digital Garden Publication Center",
Expand Down Expand Up @@ -105,12 +106,14 @@ export default class DigitalGarden extends Plugin {
new Notice("Adding publish flag to note and publishing it.");
await this.setPublishFlagValue(true);
const activeFile = this.app.workspace.getActiveFile();

const event = this.app.metadataCache.on(
"changed",
async (file, _data, _cache) => {
if (file.path === activeFile?.path) {
const successfullyPublished =
await this.publishSingleNote();

if (successfullyPublished) {
await this.copyGardenUrlToClipboard();
}
Expand Down Expand Up @@ -139,30 +142,36 @@ export default class DigitalGarden extends Plugin {
name: "Publish Multiple Notes",
callback: async () => {
const statusBarItem = this.addStatusBarItem();

try {
new Notice("Processing files to publish...");
const { vault, metadataCache } = this.app;

const publisher = new Publisher(
vault,
metadataCache,
this.settings,
);

const siteManager = new DigitalGardenSiteManager(
metadataCache,
this.settings,
);

const publishStatusManager = new PublishStatusManager(
siteManager,
publisher,
);

const publishStatus =
await publishStatusManager.getPublishStatus();

const filesToPublish = publishStatus.changedNotes.concat(
publishStatus.unpublishedNotes,
);
const filesToDelete = publishStatus.deletedNotePaths;
const imagesToDelete = publishStatus.deletedImagePaths;

const statusBar = new PublishStatusBar(
statusBarItem,
filesToPublish.length +
Expand All @@ -173,27 +182,32 @@ export default class DigitalGarden extends Plugin {
let errorFiles = 0;
let errorDeleteFiles = 0;
let errorDeleteImage = 0;

new Notice(
`Publishing ${filesToPublish.length} notes, deleting ${filesToDelete.length} notes and ${imagesToDelete.length} images. See the status bar in lower right corner for progress.`,
8000,
);

for (const file of filesToPublish) {
try {
statusBar.increment();
await publisher.publish(file);
} catch {
errorFiles++;

new Notice(
`Unable to publish note ${file.name}, skipping it.`,
);
}
}

for (const filePath of filesToDelete) {
try {
statusBar.increment();
await publisher.deleteNote(filePath);
} catch {
errorDeleteFiles++;

new Notice(
`Unable to delete note ${filePath}, skipping it.`,
);
Expand All @@ -206,25 +220,29 @@ export default class DigitalGarden extends Plugin {
await publisher.deleteImage(filePath);
} catch {
errorDeleteImage++;

new Notice(
`Unable to delete image ${filePath}, skipping it.`,
);
}
}

statusBar.finish(8000);

new Notice(
`Successfully published ${
filesToPublish.length - errorFiles
} notes to your garden.`,
);

if (filesToDelete.length > 0) {
new Notice(
`Successfully deleted ${
filesToDelete.length - errorDeleteFiles
} notes from your garden.`,
);
}

if (imagesToDelete.length > 0) {
new Notice(
`Successfully deleted ${
Expand All @@ -235,6 +253,7 @@ export default class DigitalGarden extends Plugin {
} catch (e) {
statusBarItem.remove();
console.error(e);

new Notice(
"Unable to publish multiple notes, something went wrong.",
);
Expand Down Expand Up @@ -297,6 +316,7 @@ export default class DigitalGarden extends Plugin {
async copyGardenUrlToClipboard() {
try {
const { metadataCache, workspace } = this.app;

const activeFile = this.getActiveFile(workspace);

if (!activeFile) {
Expand All @@ -313,6 +333,7 @@ export default class DigitalGarden extends Plugin {
new Notice(`Note URL copied to clipboard`);
} catch (e) {
console.log(e);

new Notice(
"Unable to copy note URL to clipboard, something went wrong.",
);
Expand All @@ -323,6 +344,7 @@ export default class DigitalGarden extends Plugin {
try {
const { vault, workspace, metadataCache } = this.app;


const activeFile = this.getActiveFile(workspace);

if (!activeFile) {
Expand All @@ -332,10 +354,12 @@ export default class DigitalGarden extends Plugin {
new Notice(
"The current file is not a markdown file. Please open a markdown file and try again.",
);

return;
}

new Notice("Publishing note...");

const publisher = new Publisher(
vault,
metadataCache,
Expand All @@ -346,13 +370,16 @@ export default class DigitalGarden extends Plugin {
if (publishSuccessful) {
new Notice(`Successfully published note to your garden.`);
}

return publishSuccessful;
} catch (e) {
console.error(e);
new Notice("Unable to publish note, something went wrong.");

return false;
}
}

async setPublishFlagValue(value: boolean) {
const activeFile = this.getActiveFile(this.app.workspace);

Expand Down Expand Up @@ -392,15 +419,18 @@ export default class DigitalGarden extends Plugin {
this.app.metadataCache,
this.settings,
);

const publisher = new Publisher(
this.app.vault,
this.app.metadataCache,
this.settings,
);

const publishStatusManager = new PublishStatusManager(
siteManager,
publisher,
);

this.publishModal = new PublishModal(
this.app,
publishStatusManager,
Expand Down
20 changes: 20 additions & 0 deletions src/publisher/DigitalGardenSiteManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default class DigitalGardenSiteManager {
}

let envSettings = "";

if (theme.name !== "default") {
envSettings = `THEME=${theme.cssUrl}\nBASE_THEME=${baseTheme}`;
}
Expand All @@ -76,6 +77,7 @@ export default class DigitalGardenSiteManager {

let fileExists = true;
let currentFile = null;

try {
currentFile = await octokit.request(
"GET /repos/{owner}/{repo}/contents/{path}",
Expand Down Expand Up @@ -108,6 +110,7 @@ export default class DigitalGardenSiteManager {
// caught in copyUrlToClipboard
throw new Error("Garden base url not set");
}

const baseUrl = `https://${extractBaseUrl(
this.settings.gardenBaseUrl,
)}`;
Expand Down Expand Up @@ -137,6 +140,7 @@ export default class DigitalGardenSiteManager {
path = path.substring(1);
}
const octokit = new Octokit({ auth: this.settings.githubToken });

const response = await octokit.request(
`GET /repos/{owner}/{repo}/contents/{path}`,
{
Expand All @@ -148,11 +152,13 @@ export default class DigitalGardenSiteManager {

// @ts-expect-error data is not yet type-guarded
const content = Base64.decode(response.data.content);

return content;
}

async getNoteHashes(): Promise<Record<string, string>> {
const octokit = new Octokit({ auth: this.settings.githubToken });

// Force the cache to be updated
const response = await octokit.request(
`GET /repos/{owner}/{repo}/git/trees/{tree_sha}?recursive=${Math.ceil(
Expand All @@ -166,22 +172,26 @@ export default class DigitalGardenSiteManager {
);

const files = response.data.tree;

const notes: Array<{ path: string; sha: string }> = files.filter(
(x: { path: string; type: string }) =>
x.path.startsWith("src/site/notes/") &&
x.type === "blob" &&
x.path !== "src/site/notes/notes.json",
);
const hashes: Record<string, string> = {};

for (const note of notes) {
const vaultPath = note.path.replace("src/site/notes/", "");
hashes[vaultPath] = note.sha;
}

return hashes;
}

async getImageHashes(): Promise<Record<string, string>> {
const octokit = new Octokit({ auth: this.settings.githubToken });

// Force the cache to be updated
const response = await octokit.request(
`GET /repos/{owner}/{repo}/git/trees/{tree_sha}?recursive=${Math.ceil(
Expand All @@ -195,17 +205,20 @@ export default class DigitalGardenSiteManager {
);

const files = response.data.tree;

const images: Array<{ path: string; sha: string }> = files.filter(
(x: { path: string; type: string }) =>
x.path.startsWith("src/site/img/user/") && x.type === "blob",
);
const hashes: Record<string, string> = {};

for (const img of images) {
const vaultPath = decodeURI(
img.path.replace("src/site/img/user/", ""),
);
hashes[vaultPath] = img.sha;
}

return hashes;
}

Expand All @@ -215,6 +228,7 @@ export default class DigitalGardenSiteManager {
*/
async createPullRequestWithSiteChanges(): Promise<string> {
const octokit = new Octokit({ auth: this.settings.githubToken });

const latestRelease = await octokit.request(
"GET /repos/{owner}/{repo}/releases/latest",
{
Expand All @@ -225,6 +239,7 @@ export default class DigitalGardenSiteManager {

const templateVersion = latestRelease.data.tag_name;
const uuid = crypto.randomUUID();

const branchName =
"update-template-to-v" + templateVersion + "-" + uuid;

Expand All @@ -246,6 +261,7 @@ export default class DigitalGardenSiteManager {
branchName,
templateVersion,
);

return prUrl;
}

Expand Down Expand Up @@ -299,6 +315,7 @@ export default class DigitalGardenSiteManager {
ref: branchName,
},
);

await octokit.request(
"DELETE /repos/{owner}/{repo}/contents/{path}",
{
Expand Down Expand Up @@ -334,6 +351,7 @@ export default class DigitalGardenSiteManager {

let currentFile = {};
let fileExists = true;

try {
currentFile = await octokit.request(
"GET /repos/{owner}/{repo}/contents/{path}",
Expand All @@ -351,6 +369,7 @@ export default class DigitalGardenSiteManager {
const fileHasChanged =
// @ts-expect-error data is not yet type-guarded
latestFile.data.sha !== currentFile?.data?.sha;

if (!fileExists || fileHasChanged) {
// commit
await octokit.request(
Expand Down Expand Up @@ -448,6 +467,7 @@ export default class DigitalGardenSiteManager {
// @ts-expect-error data is not yet type-guarded
Base64.decode(pluginInfoResponse.data.content),
);

return pluginInfo as DigitalGardenPluginInfo;
}
}
Loading

0 comments on commit 8dd3b87

Please sign in to comment.