1
Fork 0
dotfiles/.bauke/scripts/utilities.ts

96 lines
2.0 KiB
TypeScript
Raw Normal View History

import { toml } from "./dependencies.ts";
2023-07-30 15:57:38 +00:00
/**
* Check if a path exists by running {@linkcode Deno.stat} on it.
*/
export async function pathExists(path: string): Promise<boolean> {
try {
await Deno.stat(path);
return true;
} catch {
return false;
}
}
2023-07-30 15:57:38 +00:00
/**
* Run `JSON.stringify` with 2 spaces of indentation.
*/
export function stringifyJsonPretty(input: unknown): string {
return JSON.stringify(input, null, 2);
}
2023-07-30 15:57:38 +00:00
/**
* Run a command with by default inherited `stderr` and `stdout`.
*/
export async function runCommand(
command: string,
2023-06-07 13:43:59 +00:00
options: Deno.CommandOptions = {},
): Promise<void> {
await new Deno.Command(command, {
stderr: "inherit",
stdout: "inherit",
2023-06-07 13:43:59 +00:00
...options,
}).output();
}
2023-07-30 15:57:38 +00:00
/**
* Run a command and return its `stdout` output.
*/
export async function runAndReturnStdout(
command: string,
options: Deno.CommandOptions = {},
): Promise<string> {
const process = new Deno.Command(command, { stdout: "piped", ...options });
const { stdout } = await process.output();
return new TextDecoder().decode(stdout);
}
/**
* Parse the TOML frontmatter of a string, throwing an error if there is no
* frontmatter or returning the parsed frontmatter with the remaining text
* string. For example the following snippet:
*
* ```txt
* ---toml
* example_value = "Hello, world!"
* ---
*
* # Markdown
* ```
*
* Will return:
*
* ```js
* [
* {
* example_value: "Hello, world!"
* },
* "# Markdown\n"
* ]
* ```
*/
export function tomlFrontmatter<T>(
data: string,
): [T, string] {
const startMarker = "---toml\n";
const endMarker = "\n---\n";
let start = data.indexOf(startMarker);
let end = data.indexOf(endMarker);
if (start === -1 || end === -1) {
throw new Error("Missing frontmatter");
}
if (start !== 0) {
throw new Error("Frontmatter not at beginning of data");
}
start += startMarker.length;
const frontmatter = data.slice(start, end);
end += endMarker.length;
const extra = data.slice(end);
return [toml.parse(frontmatter) as T, extra.trimStart()];
}