authorgravatar for git@paperclover.netclover caruso <git@paperclover.net> 2025-12-29 06:15:52-08:00
committergravatar for git@paperclover.netclover caruso <git@paperclover.net> 2026-04-07 23:19:38-07:00
log03c98693c032d95fe0dbbb05cf485758372d37d5
tree625d48bf64af169e6cc4b313ee8c8ca4777f3171
parent6669c604a73c635a4e2b65ae98065745003fc9cc
signature Signed by SSH key SHA256:xbd+BjjhyBfwk7GVoURf9Yx0gzDerHbvYv7SddNWmAs

autofmt: some more js ecosystem stuff


1 files changed, 83 insertions(+), 58 deletions(-)

packages/autofmt/autofmt.js+83-58
...@@ -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/645100 // https://github.com/fsouza/prettierd/issues/645
...@@ -149,50 +168,66 @@ const formatters = {...@@ -149,50 +168,66 @@ const formatters = {
149};168};
150169
151// -- cli --170// -- cli --
152if (!fs.globSync) {171function 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);
156const [, bin, ...argv] = process.argv;175 }
157let inputs = [];176 const [, bin, ...argv] = process.argv;
158let globalExclude = [];177 let inputs = [];
159let gitignore = true;178 let globalExclude = [];
160let dryRun = false;179 let gitignore = true;
161let stdio = null;180 let dryRun = false;
162let excludes = [".git"];181 let stdio = null;
163while (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}
195function usage() {229
230function 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}
212if (inputs.length === 0 && !stdio) usage();
213if (stdio && inputs.length > 0) {
214 console.error("error: stdio mode only operates on one file");
215 process.exit(1);
216}
217const { sep } = path;
218247
219// -- disable warnings --248export function autofmt({ files, progress }) {
220const { emit: originalEmit } = process;249}
221const warnings = ["ExperimentalWarning"];
222process.emit = function (event, error) {
223 return event === "warning" && warnings.includes(error.name)
224 ? false
225 : originalEmit.apply(process, arguments);
226};
227250
228// -- vars --251// -- vars --
229const extToLanguage = Object.fromEntries(252const extToLanguage = Object.fromEntries(
...@@ -332,6 +355,7 @@ const syncEnd = "\u001B[?2026l";...@@ -332,6 +355,7 @@ const syncEnd = "\u001B[?2026l";
332let buffer = "";355let buffer = "";
333let statusVisible = false;356let statusVisible = false;
334const tty = process.stderr.isTTY;357const tty = process.stderr.isTTY;
358
335function writeStatus() {359function 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) {
631import * as child_process from "node:child_process";655import * as child_process from "node:child_process";
632import * as fs from "node:fs";656import * as fs from "node:fs";
633import * as path from "node:path";657import * as path from "node:path";
658import { sep } from "node:path";
634import * as readline from "node:readline";659import * as readline from "node:readline";
635import * as events from "node:events";660import * as events from "node:events";
636import process from "node:process";661import process from "node:process";