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