| ... | ... | @@ -69,13 +69,6 @@ const formatters = { |
| 69 | 69 | cmd: ["treefmt", "--", "$files"], |
| 70 | 70 | stdin: (file) => ["treefmt", "--stdin", file], |
| 71 | 71 | }, |
| 72 | | // -- web ecosystem -- |
| 73 | | deno: { |
| 74 | | languages: ["javascript", "markdown", "json", "yaml", "html", "css"], |
| 75 | | files: ["deno.json", "deno.jsonc"], |
| 76 | | cmd: ["deno", "fmt", "--", "$files"], |
| 77 | | stdin: (file) => ["deno", "fmt", "--ext", path.extname(file).slice(1), "-"], |
| 78 | | }, |
| 79 | 72 | vp: { |
| 80 | 73 | languages: ["javascript", "markdown", "json", "yaml", "mdx", "html", "css"], |
| 81 | 74 | mustMatchFile: true, |
| ... | ... | @@ -88,6 +81,13 @@ const formatters = { |
| 88 | 81 | file, |
| 89 | 82 | ], |
| 90 | 83 | }, |
| 84 | // -- web ecosystem -- |
| 85 | deno: { |
| 86 | languages: ["javascript", "markdown", "json", "yaml", "html", "css"], |
| 87 | files: ["deno.json", "deno.jsonc"], |
| 88 | cmd: ["deno", "fmt", "--", "$files"], |
| 89 | stdin: (file) => ["deno", "fmt", "--ext", path.extname(file).slice(1), "-"], |
| 90 | }, |
| 91 | 91 | oxfmt: { |
| 92 | 92 | languages: ["javascript", "markdown", "json", "yaml", "mdx", "html", "css"], |
| 93 | 93 | mustMatchFile: true, |
| ... | ... | @@ -168,66 +168,50 @@ const formatters = { |
| 168 | 168 | }; |
| 169 | 169 | |
| 170 | 170 | // -- cli -- |
| 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 | | }; |
| 171 | if (!fs.globSync) { |
| 172 | console.error(`error: autofmt must be run with Node.js v22 or newer`); |
| 173 | process.exit(1); |
| 228 | 174 | } |
| 229 | | |
| 230 | | function cliUsage() { |
| 175 | const [, bin, ...argv] = process.argv; |
| 176 | let inputs = []; |
| 177 | let globalExclude = []; |
| 178 | let gitignore = true; |
| 179 | let dryRun = false; |
| 180 | let stdio = null; |
| 181 | let excludes = [".git"]; |
| 182 | while (argv.length > 0) { |
| 183 | const arg = argv.shift(); |
| 184 | if (arg === "-h" || arg === "--help") usage(); |
| 185 | else if (arg === "--no-gitignore") gitignore = false; |
| 186 | else if (arg === "--dry-run") dryRun = true; |
| 187 | else if (arg.match(/^--stdio=./)) { |
| 188 | if (stdio) { |
| 189 | console.error("error: can only pass --stdio once"); |
| 190 | usage(); |
| 191 | } |
| 192 | stdio = arg.slice("--stdio=".length); |
| 193 | } else if (arg === "--stdio") { |
| 194 | const value = argv.shift(); |
| 195 | if (!value) { |
| 196 | console.error("error: missing value for --stdio"); |
| 197 | usage(); |
| 198 | } |
| 199 | if (stdio) { |
| 200 | console.error("error: can only pass --stdio once"); |
| 201 | usage(); |
| 202 | } |
| 203 | stdio = value; |
| 204 | } else if (arg.match(/^--exclude=./)) { |
| 205 | excludes.push(arg.slice("--exclude=".length)); |
| 206 | } else if (arg === "--") { |
| 207 | inputs.push(...argv); |
| 208 | break; |
| 209 | } else if (arg.startsWith("-")) { |
| 210 | console.error("error: unknown option " + JSON.stringify(arg)); |
| 211 | usage(); |
| 212 | } else inputs.push(arg); |
| 213 | } |
| 214 | function usage() { |
| 231 | 215 | const exe = path.basename(bin); |
| 232 | 216 | console.error(`usage: ${exe} [...files or directories]`); |
| 233 | 217 | console.error(``); |
| ... | ... | @@ -244,9 +228,21 @@ function cliUsage() { |
| 244 | 228 | console.error(``); |
| 245 | 229 | process.exit(1); |
| 246 | 230 | } |
| 247 | | |
| 248 | | export function autofmt({ files, progress }) { |
| 231 | if (inputs.length === 0 && !stdio) usage(); |
| 232 | if (stdio && inputs.length > 0) { |
| 233 | console.error("error: stdio mode only operates on one file"); |
| 234 | process.exit(1); |
| 249 | 235 | } |
| 236 | const { sep } = path; |
| 237 | |
| 238 | // -- disable warnings -- |
| 239 | const { emit: originalEmit } = process; |
| 240 | const warnings = ["ExperimentalWarning"]; |
| 241 | process.emit = function (event, error) { |
| 242 | return event === "warning" && warnings.includes(error.name) |
| 243 | ? false |
| 244 | : originalEmit.apply(process, arguments); |
| 245 | }; |
| 250 | 246 | |
| 251 | 247 | // -- vars -- |
| 252 | 248 | const extToLanguage = Object.fromEntries( |
| ... | ... | @@ -355,7 +351,6 @@ const syncEnd = "\u001B[?2026l"; |
| 355 | 351 | let buffer = ""; |
| 356 | 352 | let statusVisible = false; |
| 357 | 353 | const tty = process.stderr.isTTY; |
| 358 | | |
| 359 | 354 | function writeStatus() { |
| 360 | 355 | if (!tty) return; |
| 361 | 356 | clearStatus(); |
| ... | ... | @@ -655,7 +650,6 @@ function escapeGlob(str) { |
| 655 | 650 | import * as child_process from "node:child_process"; |
| 656 | 651 | import * as fs from "node:fs"; |
| 657 | 652 | import * as path from "node:path"; |
| 658 | | import { sep } from "node:path"; |
| 659 | 653 | import * as readline from "node:readline"; |
| 660 | 654 | import * as events from "node:events"; |
| 661 | 655 | import process from "node:process"; |