diff --git a/packages/autofmt/autofmt.js b/packages/autofmt/autofmt.js index 8986cf9974c2abaa3f9b832f73b77d961d97e0eb..0e87b07bcc98ea32558fc048bde321aaa1e361bb 100755 --- a/packages/autofmt/autofmt.js +++ b/packages/autofmt/autofmt.js @@ -72,10 +72,29 @@ const formatters = { // -- web ecosystem -- deno: { languages: ["javascript", "markdown", "json", "yaml", "html", "css"], - files: ["dprint.json", "dprint.jsonc"], + files: ["deno.json", "deno.jsonc"], cmd: ["deno", "fmt", "--", "$files"], stdin: (file) => ["deno", "fmt", "--ext", path.extname(file).slice(1), "-"], }, + vp: { + languages: ["javascript", "markdown", "json", "yaml", "mdx", "html", "css"], + mustMatchFile: true, + files: ["node_modules/.bin/vp"], + cmd: ["node_modules/.bin/vp", "fmt", "$files"], + stdin: (file) => [ + "node_modules/.bin/vp", + "fmt", + "--stdin-filepath", + file, + ], + }, + oxfmt: { + languages: ["javascript", "markdown", "json", "yaml", "mdx", "html", "css"], + mustMatchFile: true, + files: ["node_modules/.bin/oxfmt", ".oxfmtrc.json", ".oxfmtrc.jsonc"], + cmd: ["node_modules/.bin/oxfmt", "$files"], + stdin: (file) => ["node_modules/.bin/oxfmt", `--stdin-filepath=${file}`], + }, prettierd: { // keep in mind that pretterd never exits. // https://github.com/fsouza/prettierd/issues/645 @@ -149,50 +168,66 @@ const formatters = { }; // -- cli -- -if (!fs.globSync) { - console.error(`error: autofmt must be run with Node.js v22 or newer`); - process.exit(1); +function cli() { + if (!fs.globSync) { + console.error(`error: autofmt must be run with Node.js v22 or newer`); + process.exit(1); + } + const [, bin, ...argv] = process.argv; + let inputs = []; + let globalExclude = []; + let gitignore = true; + let dryRun = false; + let stdio = null; + let excludes = [".git"]; + while (argv.length > 0) { + const arg = argv.shift(); + if (arg === "-h" || arg === "--help") cliUsage(); + else if (arg === "--no-gitignore") gitignore = false; + else if (arg === "--dry-run") dryRun = true; + else if (arg.match(/^--stdio=./)) { + if (stdio) { + console.error("error: can only pass --stdio once"); + cliUsage(); + } + stdio = arg.slice("--stdio=".length); + } else if (arg === "--stdio") { + const value = argv.shift(); + if (!value) { + console.error("error: missing value for --stdio"); + cliUsage(); + } + if (stdio) { + console.error("error: can only pass --stdio once"); + cliUsage(); + } + stdio = value; + } else if (arg.match(/^--exclude=./)) { + excludes.push(arg.slice("--exclude=".length)); + } else if (arg === "--") { + inputs.push(...argv); + break; + } else if (arg.startsWith("-")) { + console.error("error: unknown option " + JSON.stringify(arg)); + cliUsage(); + } else inputs.push(arg); + } + if (inputs.length === 0 && !stdio) cliUsage(); + if (stdio && inputs.length > 0) { + console.error("error: stdio mode only operates on one file"); + process.exit(1); + } + // -- disable warnings -- + const { emit: originalEmit } = process; + const warnings = ["ExperimentalWarning"]; + process.emit = function (event, error) { + return event === "warning" && warnings.includes(error.name) + ? false + : originalEmit.apply(process, arguments); + }; } -const [, bin, ...argv] = process.argv; -let inputs = []; -let globalExclude = []; -let gitignore = true; -let dryRun = false; -let stdio = null; -let excludes = [".git"]; -while (argv.length > 0) { - const arg = argv.shift(); - if (arg === "-h" || arg === "--help") usage(); - else if (arg === "--no-gitignore") gitignore = false; - else if (arg === "--dry-run") dryRun = true; - else if (arg.match(/^--stdio=./)) { - if (stdio) { - console.error("error: can only pass --stdio once"); - usage(); - } - stdio = arg.slice("--stdio=".length); - } else if (arg === "--stdio") { - const value = argv.shift(); - if (!value) { - console.error("error: missing value for --stdio"); - usage(); - } - if (stdio) { - console.error("error: can only pass --stdio once"); - usage(); - } - stdio = value; - } else if (arg.match(/^--exclude=./)) { - excludes.push(arg.slice("--exclude=".length)); - } else if (arg === "--") { - inputs.push(...argv); - break; - } else if (arg.startsWith("-")) { - console.error("error: unknown option " + JSON.stringify(arg)); - usage(); - } else inputs.push(arg); -} -function usage() { + +function cliUsage() { const exe = path.basename(bin); console.error(`usage: ${exe} [...files or directories]`); console.error(``); @@ -209,21 +244,9 @@ function usage() { console.error(``); process.exit(1); } -if (inputs.length === 0 && !stdio) usage(); -if (stdio && inputs.length > 0) { - console.error("error: stdio mode only operates on one file"); - process.exit(1); -} -const { sep } = path; -// -- disable warnings -- -const { emit: originalEmit } = process; -const warnings = ["ExperimentalWarning"]; -process.emit = function (event, error) { - return event === "warning" && warnings.includes(error.name) - ? false - : originalEmit.apply(process, arguments); -}; +export function autofmt({ files, progress }) { +} // -- vars -- const extToLanguage = Object.fromEntries( @@ -332,6 +355,7 @@ const syncEnd = "\u001B[?2026l"; let buffer = ""; let statusVisible = false; const tty = process.stderr.isTTY; + function writeStatus() { if (!tty) return; clearStatus(); @@ -631,6 +655,7 @@ function escapeGlob(str) { import * as child_process from "node:child_process"; import * as fs from "node:fs"; import * as path from "node:path"; +import { sep } from "node:path"; import * as readline from "node:readline"; import * as events from "node:events"; import process from "node:process";