2023-01-11 14:39:51 +00:00
|
|
|
import { parse } from "https://deno.land/std@0.167.0/encoding/toml.ts";
|
2022-12-09 11:26:08 +00:00
|
|
|
import { TextDecoder } from "https://deno.land/std@0.167.0/node/util.ts";
|
|
|
|
|
2023-02-22 10:31:26 +00:00
|
|
|
export function stringifyJsonPretty(input: unknown): string {
|
|
|
|
return JSON.stringify(input, null, 2);
|
|
|
|
}
|
|
|
|
|
2022-12-09 11:26:08 +00:00
|
|
|
export async function runAndReturnStdout(
|
|
|
|
options: Deno.RunOptions,
|
|
|
|
): Promise<string> {
|
|
|
|
const process = Deno.run({ stdout: "piped", ...options });
|
|
|
|
return new TextDecoder().decode(await process.output());
|
|
|
|
}
|
2023-01-11 14:39:51 +00:00
|
|
|
|
|
|
|
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 [parse(frontmatter) as T, extra.trimStart()];
|
|
|
|
}
|