Skip to content

Instantly share code, notes, and snippets.

@sigmaSd
Last active December 19, 2022 17:54
Show Gist options
  • Save sigmaSd/5a65358fd77df63f893aacbe2cd87646 to your computer and use it in GitHub Desktop.
Save sigmaSd/5a65358fd77df63f893aacbe2cd87646 to your computer and use it in GitHub Desktop.
create helix configuration with typescript
import { stringify } from "https://deno.land/std@0.170.0/encoding/toml.ts";
import { ensureDir } from "https://deno.land/std@0.170.0/fs/ensure_dir.ts";
import * as path from "https://deno.land/std@0.170.0/path/mod.ts";
import configDir from "https://deno.land/x/dir@1.5.1/config_dir/mod.ts";
function assert(val: unknown, msg: string): asserts val {
if (val === null || val === undefined) throw new Error(msg);
}
async function main() {
const configDirPath = (() => {
const configPath = configDir();
assert(configPath, "Could not find configuration directory");
return path.join(configPath, "helix");
})();
await ensureDir(configDirPath);
const configRaw = Deno.args[0];
if (!configRaw) throw "no script given";
let config: URL;
try {
// If its a url leave it like it is
config = new URL(configRaw);
} catch {
// Otherwise consider it a file and resolve it
let configFile: string;
if (path.isAbsolute(configRaw)) configFile = configRaw;
else {
configFile = path.join(Deno.cwd(), configRaw);
}
// Make it a url so it can be imported
config = new URL("file:///" + configFile);
}
const jsConfig = await import(config.href);
const configToml = stringify(jsConfig.config());
console.log(`%cConfig.toml`, "color:blue");
console.log(configToml);
const languagesToml = stringify(jsConfig.languages());
console.log(`%clanguages.toml`, "color:blue");
console.log(languagesToml);
if (Deno.args.includes("--save")) {
console.log(
`%cSaving new configuration to ${configDirPath}`,
"color: yellow",
);
await Deno.writeTextFile(
path.join(configDirPath, "config.toml"),
configToml,
);
await Deno.writeTextFile(
path.join(configDirPath, "languages.toml"),
languagesToml,
);
}
}
if (import.meta.main) {
await main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment