Skip to content

Commit

Permalink
perf: cache plugin-theme-search results
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Aug 11, 2023
1 parent 6a174eb commit 9828242
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 29 deletions.
26 changes: 14 additions & 12 deletions scripts/append-to-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ app.includeStandardAdditions = true;

//──────────────────────────────────────────────────────────────────────────────

/**
* @param {string} text
* @param {string} absPath
*/
function appendToFile(text, absPath) {
ObjC.import("stdlib");
const app = Application.currentApplication();
app.includeStandardAdditions = true;
text = text.replaceAll("'", "`"); // ' in text string breaks echo writing method
app.doShellScript(`echo '${text}' >> '${absPath}'`); // use single quotes to prevent running of input such as "$(rm -rf /)"
/** @param {string} filepath @param {string} text */
function writeToFile(filepath, text) {
const str = $.NSString.alloc.initWithUTF8String(text);
str.writeToFileAtomicallyEncodingError(filepath, true, $.NSUTF8StringEncoding, null);
}

/** @param {string} path */
function readFile(path) {
const data = $.NSFileManager.defaultManager.contentsAtPath(path);
const str = $.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding);
return ObjC.unwrap(str);
}

//──────────────────────────────────────────────────────────────────────────────
Expand All @@ -25,7 +26,8 @@ function appendToFile(text, absPath) {
function run(argv) {
const vaultPath = $.getenv("vault_path");

const content = $.getenv("prefix") + argv[0];
const toAppend = $.getenv("prefix") + argv[0];
const absolutePath = vaultPath + "/" + $.getenv("relative_path");
appendToFile(content, absolutePath);
const content = readFile(absolutePath) + "\n" + toAppend;
writeToFile(absolutePath, content);
}
76 changes: 59 additions & 17 deletions scripts/plugin-and-theme-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function readFile(path) {
return ObjC.unwrap(str);
}

/** @param {string} filepath @param {string} text */
function writeToFile(filepath, text) {
const str = $.NSString.alloc.initWithUTF8String(text);
str.writeToFileAtomicallyEncodingError(filepath, true, $.NSUTF8StringEncoding, null);
}

/** @param {string} appId */
function SafeApplication(appId) {
try {
Expand All @@ -49,18 +55,46 @@ const discordReadyLinks = ["Discord", "Discord PTB", "Discord Canary"].some((dis
SafeApplication(discordApp)?.frontmost(),
);

function ensureCacheFolderExists() {
const finder = Application("Finder");
const cacheDir = $.getenv("alfred_workflow_cache");
if (!finder.exists(Path(cacheDir))) {
console.log("Cache Dir does not exist and is created.");
const cacheDirBasename = $.getenv("alfred_workflow_bundleid");
const cacheDirParent = cacheDir.slice(0, -cacheDirBasename.length);
finder.make({
new: "folder",
at: Path(cacheDirParent),
withProperties: { name: cacheDirBasename },
});
}
}

/** @param {string} path */
function cacheIsOutdated(path) {
const cacheAgeThresholdHours = 24;
ensureCacheFolderExists();
const cacheObj = Application("System Events").aliases[path];
if (!cacheObj.exists()) return true;
const cacheAgeMins = (+new Date() - cacheObj.creationDate()) / 1000 / 60 / 60;
return cacheAgeMins > cacheAgeThresholdHours;
}

//──────────────────────────────────────────────────────────────────────────────

/** @type {AlfredRun} */
// rome-ignore lint/correctness/noUnusedVariables: Alfred run
function run() {
// PERF cache results
const cachePath = $.getenv("alfred_workflow_cache") + "/plugin-cache.json";
if (!cacheIsOutdated(cachePath)) return readFile(cachePath);

//───────────────────────────────────────────────────────────────────────────

const vaultPath = $.getenv("vault_path");
const configFolder = $.getenv("config_folder");
const vaultNameEnc = encodeURIComponent(vaultPath.replace(/.*\//, ""));

/** @type{AlfredItem[]} */
const jsonArray = [];

const pluginJSON = onlineJSON(
"https://raw.githubusercontent.com/obsidianmd/obsidian-releases/master/community-plugins.json",
);
Expand All @@ -76,7 +110,7 @@ function run() {
`find '${vaultPath}/${configFolder}/themes/' -name '*.css' || true`,
);
const currentTheme = app.doShellScript(
`grep "cssTheme" ${vaultPath}/${configFolder}/appearance.json" | head -n1 | cut -d'"' -f4 || true`,
`grep "cssTheme" "${vaultPath}/${configFolder}/appearance.json" | head -n1 | cut -d'"' -f4 || true`,
);

const deprecated = JSON.parse(readFile("./data/deprecated-plugins.json"));
Expand All @@ -85,7 +119,7 @@ function run() {
//───────────────────────────────────────────────────────────────────────────

// add PLUGINS to the JSON
pluginJSON.forEach(
const plugins = pluginJSON.map(
(/** @type {{ id: any; name: any; description: string; author: any; repo: any; }} */ plugin) => {
const id = plugin.id;
const name = plugin.name;
Expand All @@ -111,7 +145,7 @@ function run() {
let downloadsStr = "";
if (downloadsJSON[id]) {
const downloads = downloadsJSON[id].downloads;
downloadsStr = insert1000sep(downloads) + " ";
downloadsStr = insert1000sep(downloads) + "↓ · ";
}

// check whether already installed / deprecated
Expand All @@ -120,21 +154,24 @@ function run() {
if (installedPlugins.includes(id)) icons += " ✅";
if (deprecatedPlugins.includes(id)) {
icons += " ⚠️";
subtitleIcons = "deprecated ";
subtitleIcons = "deprecated · ";
}

// Better matching for some plugins
const URImatcher = name.includes("URI") ? "URL" : "";
const matcher = `plugin ${URImatcher} ${alfredMatcher(name)} ${alfredMatcher(author)} ${alfredMatcher(
id,
)} ${alfredMatcher(description)}`;
const subtitle = downloadsStr + subtitleIcons + description + " · by " + author;

// create json for Alfred
jsonArray.push({
/** @type {AlfredItem} */
const alfredItem = {
title: name + icons,
subtitle: downloadsStr + subtitleIcons + description + " — by " + author,
subtitle: subtitle,
arg: openURI,
uid: id,
match: `plugin ${URImatcher} ${alfredMatcher(name)} ${alfredMatcher(author)} ${alfredMatcher(
id,
)} ${alfredMatcher(description)}`,
match: matcher,
mods: {
cmd: { arg: githubURL },
ctrl: { arg: id },
Expand All @@ -148,12 +185,13 @@ function run() {
subtitle: "⌥: Copy Link" + isDiscordReady,
},
},
});
};
return alfredItem;
},
);

// add THEMES to the JSON
themeJSON.forEach(
const themes = themeJSON.map(
(
/** @type {{ name: any; author: any; repo: any; branch: any; screenshot: string; modes: string | string[]; }} */ theme,
) => {
Expand Down Expand Up @@ -186,7 +224,8 @@ function run() {
else if (installedThemes.includes(name)) installedIcon = " ✅";

// create json for Alfred
jsonArray.push({
/** @type {AlfredItem} */
return {
title: name + installedIcon,
subtitle: `${modes} by ${author}`,
match: `theme ${alfredMatcher(author)} ${alfredMatcher(name)}`,
Expand All @@ -207,9 +246,12 @@ function run() {
subtitle: "⌥: Copy Obsidian URI for Theme" + isDiscordReady,
},
},
});
};
},
);

return JSON.stringify({ items: jsonArray });
const alfredResponse = JSON.stringify({ items: [...plugins, ...themes] });
writeToFile(cachePath, alfredResponse);

return alfredResponse;
}

0 comments on commit 9828242

Please sign in to comment.