| ... | ... | @@ -72,10 +72,29 @@ const formatters = { |
| 72 | 72 | // -- web ecosystem -- |
| 73 | 73 | deno: { |
| 74 | 74 | languages: ["javascript", "markdown", "json", "yaml", "html", "css"], |
| 75 | | files: ["dprint.json", "dprint.jsonc"], |
| 75 | files: ["deno.json", "deno.jsonc"], |
| 76 | 76 | cmd: ["deno", "fmt", "--", "$files"], |
| 77 | 77 | stdin: (file) => ["deno", "fmt", "--ext", path.extname(file).slice(1), "-"], |
| 78 | 78 | }, |
| 79 | vp: { |
| 80 | languages: ["javascript", "markdown", "json", "yaml", "mdx", "html", "css"], |
| 81 | mustMatchFile: true, |
| 82 | files: ["node_modules/.bin/vp"], |
| 83 | cmd: ["node_modules/.bin/vp", "fmt", "$files"], |
| 84 | stdin: (file) => [ |
| 85 | "node_modules/.bin/vp", |
| 86 | "fmt", |
| 87 | "--stdin-filepath", |
| 88 | file, |
| 89 | ], |
| 90 | }, |
| 91 | oxfmt: { |
| 92 | languages: ["javascript", "markdown", "json", "yaml", "mdx", "html", "css"], |
| 93 | mustMatchFile: true, |
| 94 | files: ["node_modules/.bin/oxfmt", ".oxfmtrc.json", ".oxfmtrc.jsonc"], |
| 95 | cmd: ["node_modules/.bin/oxfmt", "$files"], |
| 96 | stdin: (file) => ["node_modules/.bin/oxfmt", `--stdin-filepath=${file}`], |
| 97 | }, |
| 79 | 98 | prettierd: { |
| 80 | 99 | // keep in mind that pretterd never exits. |
| 81 | 100 | // https://github.com/fsouza/prettierd/issues/645 |
| ... | ... | @@ -149,50 +168,66 @@ const formatters = { |
| 149 | 168 | }; |
| 150 | 169 | |
| 151 | 170 | // -- cli -- |
| 152 | | if (!fs.globSync) { |
| 153 | | console.error(`error: autofmt must be run with Node.js v22 or newer`); |
| 154 | | process.exit(1); |
| 155 | | } |
| 156 | | const [, bin, ...argv] = process.argv; |
| 157 | | let inputs = []; |
| 158 | | let globalExclude = []; |
| 159 | | let gitignore = true; |
| 160 | | let dryRun = false; |
| 161 | | let stdio = null; |
| 162 | | let excludes = [".git"]; |
| 163 | | while (argv.length > 0) { |
| 164 | | const arg = argv.shift(); |
| 165 | | if (arg === "-h" || arg === "--help") usage(); |
| 166 | | else if (arg === "--no-gitignore") gitignore = false; |
| 167 | | else if (arg === "--dry-run") dryRun = true; |
| 168 | | else if (arg.match(/^--stdio=./)) { |
| 169 | | if (stdio) { |
| 170 | | console.error("error: can only pass --stdio once"); |
| 171 | | usage(); |
| 172 | | } |
| 173 | | stdio = arg.slice("--stdio=".length); |
| 174 | | } else if (arg === "--stdio") { |
| 175 | | const value = argv.shift(); |
| 176 | | if (!value) { |
| 177 | | console.error("error: missing value for --stdio"); |
| 178 | | usage(); |
| 179 | | } |
| 180 | | if (stdio) { |
| 181 | | console.error("error: can only pass --stdio once"); |
| 182 | | usage(); |
| 183 | | } |
| 184 | | stdio = value; |
| 185 | | } else if (arg.match(/^--exclude=./)) { |
| 186 | | excludes.push(arg.slice("--exclude=".length)); |
| 187 | | } else if (arg === "--") { |
| 188 | | inputs.push(...argv); |
| 189 | | break; |
| 190 | | } else if (arg.startsWith("-")) { |
| 191 | | console.error("error: unknown option " + JSON.stringify(arg)); |
| 192 | | usage(); |
| 193 | | } else inputs.push(arg); |
| 171 | function cli() { |
| 172 | if (!fs.globSync) { |
| 173 | console.error(`error: autofmt must be run with Node.js v22 or newer`); |
| 174 | process.exit(1); |
| 175 | } |
| 176 | const [, bin, ...argv] = process.argv; |
| 177 | let inputs = []; |
| 178 | let globalExclude = []; |
| 179 | let gitignore = true; |
| 180 | let dryRun = false; |
| 181 | let stdio = null; |
| 182 | let excludes = [".git"]; |
| 183 | while (argv.length > 0) { |
| 184 | const arg = argv.shift(); |
| 185 | if (arg === "-h" || arg === "--help") cliUsage(); |
| 186 | else if (arg === "--no-gitignore") gitignore = false; |
| 187 | else if (arg === "--dry-run") dryRun = true; |
| 188 | else if (arg.match(/^--stdio=./)) { |
| 189 | if (stdio) { |
| 190 | console.error("error: can only pass --stdio once"); |
| 191 | cliUsage(); |
| 192 | } |
| 193 | stdio = arg.slice("--stdio=".length); |
| 194 | } else if (arg === "--stdio") { |
| 195 | const value = argv.shift(); |
| 196 | if (!value) { |
| 197 | console.error("error: missing value for --stdio"); |
| 198 | cliUsage(); |
| 199 | } |
| 200 | if (stdio) { |
| 201 | console.error("error: can only pass --stdio once"); |
| 202 | cliUsage(); |
| 203 | } |
| 204 | stdio = value; |
| 205 | } else if (arg.match(/^--exclude=./)) { |
| 206 | excludes.push(arg.slice("--exclude=".length)); |
| 207 | } else if (arg === "--") { |
| 208 | inputs.push(...argv); |
| 209 | break; |
| 210 | } else if (arg.startsWith("-")) { |
| 211 | console.error("error: unknown option " + JSON.stringify(arg)); |
| 212 | cliUsage(); |
| 213 | } else inputs.push(arg); |
| 214 | } |
| 215 | if (inputs.length === 0 && !stdio) cliUsage(); |
| 216 | if (stdio && inputs.length > 0) { |
| 217 | console.error("error: stdio mode only operates on one file"); |
| 218 | process.exit(1); |
| 219 | } |
| 220 | // -- disable warnings -- |
| 221 | const { emit: originalEmit } = process; |
| 222 | const warnings = ["ExperimentalWarning"]; |
| 223 | process.emit = function (event, error) { |
| 224 | return event === "warning" && warnings.includes(error.name) |
| 225 | ? false |
| 226 | : originalEmit.apply(process, arguments); |
| 227 | }; |
| 194 | 228 | } |
| 195 | | function usage() { |
| 229 | |
| 230 | function cliUsage() { |
| 196 | 231 | const exe = path.basename(bin); |
| 197 | 232 | console.error(`usage: ${exe} [...files or directories]`); |
| 198 | 233 | console.error(``); |
| ... | ... | @@ -209,21 +244,9 @@ function usage() { |
| 209 | 244 | console.error(``); |
| 210 | 245 | process.exit(1); |
| 211 | 246 | } |
| 212 | | if (inputs.length === 0 && !stdio) usage(); |
| 213 | | if (stdio && inputs.length > 0) { |
| 214 | | console.error("error: stdio mode only operates on one file"); |
| 215 | | process.exit(1); |
| 216 | | } |
| 217 | | const { sep } = path; |
| 218 | 247 | |
| 219 | | // -- disable warnings -- |
| 220 | | const { emit: originalEmit } = process; |
| 221 | | const warnings = ["ExperimentalWarning"]; |
| 222 | | process.emit = function (event, error) { |
| 223 | | return event === "warning" && warnings.includes(error.name) |
| 224 | | ? false |
| 225 | | : originalEmit.apply(process, arguments); |
| 226 | | }; |
| 248 | export function autofmt({ files, progress }) { |
| 249 | } |
| 227 | 250 | |
| 228 | 251 | // -- vars -- |
| 229 | 252 | const extToLanguage = Object.fromEntries( |
| ... | ... | @@ -332,6 +355,7 @@ const syncEnd = "\u001B[?2026l"; |
| 332 | 355 | let buffer = ""; |
| 333 | 356 | let statusVisible = false; |
| 334 | 357 | const tty = process.stderr.isTTY; |
| 358 | |
| 335 | 359 | function writeStatus() { |
| 336 | 360 | if (!tty) return; |
| 337 | 361 | clearStatus(); |
| ... | ... | @@ -631,6 +655,7 @@ function escapeGlob(str) { |
| 631 | 655 | import * as child_process from "node:child_process"; |
| 632 | 656 | import * as fs from "node:fs"; |
| 633 | 657 | import * as path from "node:path"; |
| 658 | import { sep } from "node:path"; |
| 634 | 659 | import * as readline from "node:readline"; |
| 635 | 660 | import * as events from "node:events"; |
| 636 | 661 | import process from "node:process"; |