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 = {
7272 // -- web ecosystem --
7373 deno: {
7474 languages: ["javascript", "markdown", "json", "yaml", "html", "css"],
75 files: ["dprint.json", "dprint.jsonc"],
75 files: ["deno.json", "deno.jsonc"],
7676 cmd: ["deno", "fmt", "--", "$files"],
7777 stdin: (file) => ["deno", "fmt", "--ext", path.extname(file).slice(1), "-"],
7878 },
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 },
7998 prettierd: {
8099 // keep in mind that pretterd never exits.
81100 // https://github.com/fsouza/prettierd/issues/645
......@@ -149,50 +168,66 @@ const formatters = {
149168};
150169
151170// -- cli --
152if (!fs.globSync) {
153 console.error(`error: autofmt must be run with Node.js v22 or newer`);
154 process.exit(1);
155}
156const [, bin, ...argv] = process.argv;
157let inputs = [];
158let globalExclude = [];
159let gitignore = true;
160let dryRun = false;
161let stdio = null;
162let excludes = [".git"];
163while (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);
171function 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 };
194228}
195function usage() {
229
230function cliUsage() {
196231 const exe = path.basename(bin);
197232 console.error(`usage: ${exe} [...files or directories]`);
198233 console.error(``);
......@@ -209,21 +244,9 @@ function usage() {
209244 console.error(``);
210245 process.exit(1);
211246}
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 --
220const { emit: originalEmit } = process;
221const warnings = ["ExperimentalWarning"];
222process.emit = function (event, error) {
223 return event === "warning" && warnings.includes(error.name)
224 ? false
225 : originalEmit.apply(process, arguments);
226};
248export function autofmt({ files, progress }) {
249}
227250
228251// -- vars --
229252const extToLanguage = Object.fromEntries(
......@@ -332,6 +355,7 @@ const syncEnd = "\u001B[?2026l";
332355let buffer = "";
333356let statusVisible = false;
334357const tty = process.stderr.isTTY;
358
335359function writeStatus() {
336360 if (!tty) return;
337361 clearStatus();
......@@ -631,6 +655,7 @@ function escapeGlob(str) {
631655import * as child_process from "node:child_process";
632656import * as fs from "node:fs";
633657import * as path from "node:path";
658import { sep } from "node:path";
634659import * as readline from "node:readline";
635660import * as events from "node:events";
636661import process from "node:process";