Skip to content

Instantly share code, notes, and snippets.

@scarf005
Last active June 6, 2023 13:43
Show Gist options
  • Save scarf005/9b38a5165626719a73c571f7cd49171a to your computer and use it in GitHub Desktop.
Save scarf005/9b38a5165626719a73c571f7cd49171a to your computer and use it in GitHub Desktop.
initialize deno with vscode settings, no semicolon, and line width 100
#!/usr/bin/env -S deno run --allow-read --allow-write --allow-run --allow-env --unstable
import { Command } from "https://deno.land/x/cliffy@v0.25.7/command/mod.ts"
import { dirname, join } from "https://deno.land/std@0.190.0/path/mod.ts"
import { c, p } from "https://deno.land/x/copb@v1.0.1/mod.ts"
const mapKeys =
<A>(f: (k: string) => string) => (record: Readonly<Record<string, A>>): Record<string, A> =>
Object.fromEntries(Object.entries(record).map(([k, v]) => [f(k), v]))
const mapValues =
<A, B>(f: (a: A) => B) => (record: Readonly<Record<string, A>>): Record<string, B> =>
Object.fromEntries(Object.entries(record).map(([k, v]) => [k, f(v)]))
const flattenEntries =
<A, B>(f: (entry: [string, A]) => B) => (record: Readonly<Record<string, A>>): B[] =>
Object.entries(record).map(f)
const writeTextFileMkdir = async (path: string, data: string) => {
await Deno.mkdir(dirname(path), { recursive: true })
await Deno.writeTextFile(path, data)
}
// deno-fmt-ignore
const writeAll = (pwd: string) => c(p
(mapKeys((k) => join(pwd, k)))
(mapValues((v) => JSON.stringify(v, null, 2) + "\n"))
(flattenEntries(([k, v]) => writeTextFileMkdir(k, v)))
)
const jsx = {
jsx: "react-jsx",
jsxImportSource: "https://esm.sh/preact",
}
const config = (useJsx: boolean) => ({
fmt: { semiColons: false, lineWidth: 100 },
compilerOptions: {
exactOptionalPropertyTypes: true,
...(useJsx ? jsx : {}),
},
})
const makeSettingsJson = () => {
const formatter = { "editor.defaultFormatter": "denoland.vscode-deno" }
const langs = ["json", "jsonc", "markdown", "typescript", "typescriptreact"]
const formatters = Object.fromEntries(
langs.map((lang) => [`[${lang}]`, formatter]),
)
return {
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true,
"deno.config": "./deno.jsonc",
...formatters,
}
}
const main = new Command()
.name("initdeno")
.option("--jsx", "Enable JSX support")
.description(
"Minimal alternative for deno init that only populates .vscode and deno.jsonc",
)
.action(({ jsx = false }) => {
writeAll(Deno.cwd())({
".vscode/settings.json": makeSettingsJson(),
"deno.jsonc": config(jsx),
})
console.log("Done.")
})
if (import.meta.main) {
await main.parse(Deno.args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment