alacritty-theme-switch
Version:
CLI utility for switching Alacritty color themes
111 lines (110 loc) ⢠4.09 kB
JavaScript
import "../_dnt.polyfills.js";
import * as dntShim from "../_dnt.shims.js";
import { bold, getArgs, getHomeDir, interactiveThemesSelection, printHelp, printVersion, } from "./cli.js";
import { clearThemesCommand } from "./commands/clear-themes.js";
import { downloadThemesCommand } from "./commands/download-themes.js";
import { createThemeManager } from "./theme-manager/theme-manager.js";
const args = getArgs(dntShim.Deno.args, getHomeDir(dntShim.Deno.build.os), dntShim.Deno.build.os);
// Show help and quit
if (args.help) {
printHelp();
dntShim.Deno.exit(0);
}
// Show version and quit
if (args.version) {
printVersion();
dntShim.Deno.exit(0);
}
// Handle download-themes subcommand
if (args.command === "download-themes") {
console.log(`Downloading themes from ${bold(args.url)}@${bold(args.ref)}`);
console.log(`Output directory: ${bold(args.themes)}`);
await downloadThemesCommand({
repositoryUrl: args.url,
outputPath: args.themes,
ref: args.ref,
}).match((downloadedThemes) => {
console.log(`\nSuccessfully downloaded ${bold(downloadedThemes.length.toString())} theme(s): ā
`);
downloadedThemes.forEach((theme) => {
console.log(` - ${theme.label}`);
});
console.log(`\nš These themes are made possible by the open-source community.`);
console.log(` Consider supporting the authors at ${bold(args.url)}`);
dntShim.Deno.exit(0);
}, (error) => {
console.error("Failed to download themes! ā");
console.error(error);
dntShim.Deno.exit(1);
});
}
// Handle clear-themes subcommand
if (args.command === "clear-themes") {
const decision = confirm(`Are you sure you want to delete all themes from ${bold(args.themes)}?`);
if (!decision) {
console.log("Cancelled. ā
");
dntShim.Deno.exit(0);
}
console.log(`Clearing all themes from ${bold(args.themes)}...`);
await clearThemesCommand({
themesPath: args.themes,
}).match((deletedPaths) => {
console.log(`Successfully deleted ${bold(deletedPaths.length.toString())} theme(s) ā
`);
dntShim.Deno.exit(0);
}, (error) => {
if (error._tag === "NoThemesFoundError") {
console.log("No themes found to delete. ā
");
dntShim.Deno.exit(0);
}
console.error("Failed to clear themes! ā");
console.error(error);
dntShim.Deno.exit(1);
});
}
// We're in theme management territory now -> create a manager
const managerResult = await createThemeManager({
configPath: args.config,
themesDirPath: args.themes,
backupPath: args.backup,
});
if (managerResult.isErr()) {
if (managerResult.error._tag === "NoThemesFoundError") {
console.log("No themes found. Use `ats download-themes` to download some.");
dntShim.Deno.exit(0);
}
console.error("Failed to create theme manager! ā");
console.error(managerResult.error);
dntShim.Deno.exit(1);
}
const manager = managerResult.value;
// Handle --select flag which skips the interactive prompt
if (args.select !== undefined) {
await manager
.applyThemeByFilename(args.select)
.match((appliedTheme) => {
console.log(`Applied theme ${bold(appliedTheme.label)} ā
`);
dntShim.Deno.exit(0);
}, (error) => {
console.log("Failed to apply theme! ā");
console.error(error);
dntShim.Deno.exit(1);
});
dntShim.Deno.exit(0);
}
// Else display interactive prompt
await interactiveThemesSelection(manager)
.andThen((selectedTheme) => manager.applyTheme(selectedTheme))
.match((appliedTheme) => {
console.log(`Applied theme ${bold(appliedTheme.label)} ā
`);
dntShim.Deno.exit(0);
}, (error) => {
// Handle (SIGINT) Ctrl+C gracefully
if (error._tag === "ExitPromptError") {
console.log("Cancelled. ā
");
dntShim.Deno.exit(0);
}
console.log("Failed to apply theme! ā");
console.error(error);
dntShim.Deno.exit(1);
});
dntShim.Deno.exit(0);