Last active
December 19, 2022 17:54
-
-
Save sigmaSd/5a65358fd77df63f893aacbe2cd87646 to your computer and use it in GitHub Desktop.
create helix configuration with typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { stringify } from "https://deno.land/[email protected]/encoding/toml.ts"; | |
import { ensureDir } from "https://deno.land/[email protected]/fs/ensure_dir.ts"; | |
import * as path from "https://deno.land/[email protected]/path/mod.ts"; | |
import configDir from "https://deno.land/x/[email protected]/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