Vojta Holik

Vojta Holik

// Menu: Emoji picker
// Shortcut: option cmd e
// Twitter: @vjthlk
// Author: Vojta Holik
/** @type {import("@johnlindquist/kit")} */
import "@johnlindquist/kit";
const { emojis, write } = await db("emojis-db", {
emojis: [],
});
let emojiPath = tmpPath(`emoji.json`);
if (!(await isFile(emojiPath))) {
await download(
`https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json`,
tmpPath()
);
}
let emojiJson = await readJson(emojiPath);
let emojiJsonHistory = emojis.map((e) => {
return _.find(emojiJson, { emoji: e });
});
const mode = (arr) =>
arr.reduce(
(a, b, i, arr) =>
arr.filter((v) => v === a).length >= arr.filter((v) => v === b).length
? a
: b,
null
);
let mostUsed = mode(emojiJsonHistory);
let history = _.uniq(
[...emojiJsonHistory]
.filter((e) => e.description !== mostUsed.description)
.reverse()
);
let emojiList = [
...history.slice(0, 1),
mostUsed,
...history.slice(1, 5),
...emojiJson.filter((e) => {
return !history.includes(e);
}),
];
let selectedEmoji = await arg(
"Search",
emojiList.map((e) => {
return {
name: `${e.emoji} ${e.description}`,
description: `${e.category} ${!_.isEmpty(e.tags) ? "-" : ""} ${e.tags.map(
(tag) => ` ${tag}`
)}`,
value: e.emoji,
shortcode: e.tags.map((tag) => ` ${tag}`),
};
})
);
emojis.push(!selectedEmoji.name && selectedEmoji);
await write();
await setSelectedText(selectedEmoji);
// Menu: Compress Images
// Description: Compress images using imagemin
// Author: Vojta Holik
// Twitter: @vjthlk
/** @type {import("@johnlindquist/kit")} */
let imagemin = await npm("imagemin");
let imageminJpegtran = await npm("imagemin-jpegtran");
let imageminJpegRecompress = await npm("imagemin-jpeg-recompress");
let imageminPngquant = await npm("imagemin-pngquant");
let imageminSvgo = await npm("imagemin-svgo");
let imageminGifsicle = await npm("imagemin-gifsicle");
let selectedFiles = await getSelectedFile();
let filePaths;
if (selectedFiles) {
filePaths = selectedFiles.split("\n");
} else {
let droppedFiles = await drop({ placeholder: "Drop images to compress" });
filePaths = droppedFiles.map((file) => file.path);
}
for (let filePath of filePaths) {
let directory = path.dirname(filePath);
await imagemin([filePath], {
destination: directory,
plugins: [
imageminJpegtran({
arithmetic: true,
progressive: true,
}),
imageminJpegRecompress({ max: 85 }),
imageminSvgo(),
imageminGifsicle({
optimizationLevel: 2,
}),
imageminPngquant({
quality: [0.3, 0.5],
}),
],
});
}

// Menu: Tinify
// Description: Compress selected images with Tinify
// Author: Vojta Holik
// Twitter: @vjthlk
let tinify = await npm("tinify");
let fs = await import("fs");
let selectedFiles = await getSelectedFile();
tinify.key = await env("TINIFY_API_KEY", {
hint: md("get your Tinify api key at https://tinypng.com/developers"),
ignoreBlur: true,
secret: true,
});
let filePaths = selectedFiles.split("\n");
for (let filePath of filePaths) {
let directory = path.dirname(filePath);
let extension = path.extname(filePath);
let originalFileName = path.basename(filePath);
let suffix = "";
let isHD = originalFileName.includes("@2x");
let newFileName = isHD
? originalFileName
.replace("@2x", "")
.replace(extension, `${suffix}@2x${extension}`)
: originalFileName.replace(extension, `${suffix}${extension}`);
fs.readFile(filePath, (err, sourceData) => {
if (err) throw err;
tinify.fromBuffer(sourceData).toBuffer((err, resultData) => {
if (err) throw err;
fs.writeFile(`${directory}/` + newFileName, resultData, (err) => {
if (err) throw err;
});
});
});
}
<details> <summary>You can change file names and directory to best suite your workflow. Since I often work with <code>@2x</code> images I adjusted the script to correctly suffix that part.</summary>
let isHD = originalFileName.includes("@2x");
let newFileName = isHD
? originalFileName
.replace("@2x", "")
.replace(extension, `${suffix}@2x${extension}`)
: originalFileName.replace(extension, `${suffix}${extension}`);
</details>

<img src="https://p-ZmFjNlQ.b3.n0.cdn.getcloudapp.com/items/mXur6N2W/50e6f3d6-8b3d-4b24-a4c6-3ee047a9613e.gif?v=f5fe168354cb0d1e34dcbf405c7891ac" width="700" />
// Menu: Slugify file name
// Description: Slugify selected files
// Author: Vojta Holik
// Twitter: @vjthlk
let slugify = await npm("slugify")
let selectedFiles = await getSelectedFile()
const filePaths = selectedFiles.split("\n")
for (let filePath of filePaths) {
let originalFileName = path.basename(filePath)
let newFileName = slugify(originalFileName, {
lower: true,
})
let newFilePath = path.join(
path.dirname(filePath),
newFileName
)
cp(filePath, newFilePath)
}