Use dprint package for dtsBundler formatting rather than dprint CLI (#58625)

This commit is contained in:
Jake Bailey
2024-05-22 15:20:26 -07:00
committed by GitHub
parent e02af8e36d
commit f2aebff7a3
4 changed files with 43 additions and 12 deletions

View File

@@ -5,8 +5,9 @@
* bundle as namespaces again, even though the project is modules.
*/
import * as dprintFormatter from "@dprint/formatter";
import * as dprintTypeScript from "@dprint/typescript";
import assert, { fail } from "assert";
import cp from "child_process";
import fs from "fs";
import minimist from "minimist";
import path from "path";
@@ -475,23 +476,23 @@ if (publicContents.includes("@internal")) {
console.error("Output includes untrimmed @internal nodes!");
}
const dprintPath = path.resolve(__dirname, "..", "node_modules", "dprint", "bin.js");
const buffer = fs.readFileSync(dprintTypeScript.getPath());
const formatter = dprintFormatter.createFromBuffer(buffer);
formatter.setConfig({
indentWidth: 4,
lineWidth: 1000,
newLineKind: "auto",
useTabs: false,
}, {
quoteStyle: "preferDouble",
});
/**
* @param {string} contents
* @returns {string}
*/
function dprint(contents) {
const result = cp.execFileSync(
process.execPath,
[dprintPath, "fmt", "--stdin", "ts"],
{
stdio: ["pipe", "pipe", "inherit"],
encoding: "utf-8",
input: contents,
maxBuffer: 100 * 1024 * 1024, // 100 MB "ought to be enough for anyone"; https://github.com/nodejs/node/issues/9829
},
);
const result = formatter.formatText("dummy.d.ts", contents);
return result.replace(/\r\n/g, "\n");
}