UNPKG

@julr/tooling-configs

Version:

Shared tooling configs: TSConfig, OXC (oxlint + oxfmt)

128 lines (125 loc) 4.06 kB
#!/usr/bin/env node import color from "picocolors"; import * as p from "@clack/prompts"; import { cancel, confirm, intro, isCancel, log, multiselect } from "@clack/prompts"; import { join } from "node:path"; import { existsSync } from "node:fs"; import { installPackage } from "@antfu/install-pkg"; import { mkdir, readFile, writeFile } from "node:fs/promises"; //#region src/cli/update_oxc.ts const oxlintConfigContent = `import { defineConfig } from 'oxlint' import { julrPreset } from '@julr/tooling-configs/oxc/lint' export default defineConfig({ extends: [julrPreset()], }) `; const oxfmtConfigContent = `import { julrPreset } from '@julr/tooling-configs/oxc/fmt' export default julrPreset() `; const vscodeSettings = { "oxc.typeAware": true, "oxc.fmt.experimental": true, "editor.defaultFormatter": "oxc.oxc-vscode" }; const oxcScripts = { "format": "oxfmt --write .", "lint": "oxlint", "lint:fix": "oxlint --fix" }; const oxcPackages = [ "oxfmt", "oxlint", "@julr/tooling-configs" ]; async function updateOxc(result) { if (!result.tools.includes("oxc")) return; const cwd = process.cwd(); await writeFile(join(cwd, "oxlint.config.ts"), oxlintConfigContent); await writeFile(join(cwd, "oxfmt.config.ts"), oxfmtConfigContent); await updateVscodeSettings(cwd); await addScriptsToPackageJson(cwd); await installPackage(oxcPackages, { dev: true, cwd }); p.log.success("OXC configured (oxlint + oxfmt) 🦀"); } async function updateVscodeSettings(cwd) { const vscodePath = join(cwd, ".vscode"); const settingsPath = join(vscodePath, "settings.json"); if (!existsSync(vscodePath)) await mkdir(vscodePath); let settings = {}; if (existsSync(settingsPath)) { const content = await readFile(settingsPath, "utf-8"); settings = JSON.parse(content); } for (const [key, value] of Object.entries(vscodeSettings)) if (!(key in settings)) settings[key] = value; await writeFile(settingsPath, JSON.stringify(settings, null, 2)); } async function addScriptsToPackageJson(cwd) { const pkgPath = join(cwd, "package.json"); const pkg = JSON.parse(await readFile(pkgPath, "utf-8")); pkg.scripts ??= {}; for (const [key, value] of Object.entries(oxcScripts)) pkg.scripts[key] = value; await writeFile(pkgPath, JSON.stringify(pkg, null, 2)); } //#endregion //#region package.json var version = "6.1.1"; //#endregion //#region src/cli/update_pkg.json.ts async function updatePkgJson(_) { const pathPackageJSON = join(process.cwd(), "package.json"); const userPkgJson = JSON.parse(await readFile(pathPackageJSON, "utf-8")); userPkgJson.devDependencies ??= {}; userPkgJson.devDependencies["@julr/tooling-configs"] = `^${version}`; await writeFile(pathPackageJSON, JSON.stringify(userPkgJson, null, 2)); p.log.success("Package.json updated 🤠"); } //#endregion //#region src/cli/update_tsconfig.ts const tsConfigContent = ` { "extends": "@julr/tooling-configs/tsconfigs/tsconfig.node", "compilerOptions": { "rootDir": "./", "outDir": "./build" } } `; async function updateTsconfig(result) { if (!result.tools.includes("tsconfig")) return; await writeFile(join(process.cwd(), "tsconfig.json"), tsConfigContent); p.log.success("Typescript configured 👹"); } //#endregion //#region src/cli/index.ts function exit() { cancel("Cancelled"); return process.exit(0); } async function main() { const cwd = process.cwd(); intro(color.blue("@julr/tooling-configs")); log.info(`You are about to configure @julr/tooling-configs in the current directory: ${color.green(cwd)}`); if (isCancel(await confirm({ message: `Continue ?` }))) return exit(); const tools = await multiselect({ message: "Select tools to configure", options: [{ value: "tsconfig", label: "TypeScript" }, { value: "oxc", label: "OXC (oxlint + oxfmt)" }], required: true }); if (isCancel(tools)) return exit(); await updatePkgJson({ tools }); await updateTsconfig({ tools }); await updateOxc({ tools }); log.success("All done. Make sure to install the dependencies with `pnpm install."); } main(); //#endregion export {};