Suite of AT Protocol TypeScript libraries built on web standards
20
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: skip fmt in lex build

+101 -9
+33 -5
lex/build/formatter.ts
··· 6 6 7 7 let formatterPromise: Promise<DprintFormatter> | undefined; 8 8 9 + export class GeneratedTextFormatError extends Error { 10 + constructor(readonly filePath: string, cause: unknown) { 11 + super( 12 + `Failed to format generated TypeScript file ${filePath}: ${ 13 + describeError(cause) 14 + }`, 15 + { cause }, 16 + ); 17 + this.name = "GeneratedTextFormatError"; 18 + } 19 + } 20 + 9 21 export async function formatGeneratedText( 10 22 filePath: string, 11 23 text: string, 12 24 ): Promise<string> { 13 - const formatter = await getFormatter(); 14 - return formatter.formatText({ 15 - filePath, 16 - fileText: text, 17 - }); 25 + try { 26 + const formatter = await getFormatter(); 27 + return formatter.formatText({ 28 + filePath, 29 + fileText: text, 30 + }); 31 + } catch (err) { 32 + throw new GeneratedTextFormatError(filePath, err); 33 + } 18 34 } 19 35 20 36 async function getFormatter(): Promise<DprintFormatter> { ··· 66 82 67 83 throw new TypeError("Could not load @dprint/typescript plugin"); 68 84 } 85 + 86 + function describeError(err: unknown): string { 87 + if (err instanceof Error && err.message.length > 0) { 88 + return err.message; 89 + } 90 + 91 + if (typeof err === "string" && err.length > 0) { 92 + return err; 93 + } 94 + 95 + return String(err); 96 + }
+4 -1
lex/build/lex-builder.ts
··· 25 25 out: string; 26 26 clear?: boolean; 27 27 override?: boolean; 28 + format?: boolean; 28 29 }; 29 30 30 31 export class LexBuilder { ··· 81 82 await Promise.all( 82 83 Array.from(files, async (file) => { 83 84 const filePath = resolveOutputFilePath(destination, file.getFilePath()); 84 - const content = await formatGeneratedText(filePath, file.getFullText()); 85 + const content = options.format === false 86 + ? file.getFullText() 87 + : await formatGeneratedText(filePath, file.getFullText()); 85 88 await mkdir(dirname(filePath), { recursive: true }); 86 89 await rm(filePath, { recursive: true, force: true }); 87 90 await writeFile(filePath, content, "utf8");
+6
lex/cli/build.ts
··· 50 50 { default: false }, 51 51 ) 52 52 .option( 53 + "--skip-fmt", 54 + "write generated files without running dprint formatting", 55 + { default: false }, 56 + ) 57 + .option( 53 58 "--ignore-invalid-lexicons", 54 59 "skip invalid lexicon files instead of exiting with an error", 55 60 { default: false }, ··· 77 82 lib: opts.lib, 78 83 allowLegacyBlobs: opts.allowLegacyBlobs, 79 84 pureAnnotations: opts.pureAnnotations, 85 + format: !opts.skipFmt, 80 86 ignoreInvalidLexicons: opts.ignoreInvalidLexicons, 81 87 include: opts.include, 82 88 exclude: opts.exclude,
+1 -1
lex/deno.json
··· 1 1 { 2 2 "name": "@atp/lex", 3 - "version": "0.1.0-alpha.8", 3 + "version": "0.1.0-alpha.9", 4 4 "exports": { 5 5 ".": "./mod.ts", 6 6 "./cbor": "./cbor/mod.ts",
+13 -2
lex/tests/formatter_test.ts
··· 1 - import { assertEquals } from "@std/assert"; 2 - import { formatGeneratedText } from "../build/formatter.ts"; 1 + import { assertEquals, assertRejects } from "@std/assert"; 2 + import { 3 + formatGeneratedText, 4 + GeneratedTextFormatError, 5 + } from "../build/formatter.ts"; 3 6 4 7 Deno.test("formatGeneratedText formats TypeScript output programmatically", async () => { 5 8 const formatted = await formatGeneratedText( ··· 12 15 'const value = { foo: "bar", items: [1, 2, 3] };\n', 13 16 ); 14 17 }); 18 + 19 + Deno.test("formatGeneratedText reports formatter failures with the file path", async () => { 20 + await assertRejects( 21 + async () => await formatGeneratedText("/tmp/bad.ts", "const = ;"), 22 + GeneratedTextFormatError, 23 + "Failed to format generated TypeScript file /tmp/bad.ts", 24 + ); 25 + });
+44
lex/tests/lex-builder_test.ts
··· 58 58 } 59 59 }, 60 60 }); 61 + 62 + Deno.test({ 63 + name: "save can write files without formatting generated text", 64 + async fn() { 65 + const root = await Deno.makeTempDir({ prefix: "lex-builder-" }); 66 + 67 + try { 68 + const lexicons = join(root, "lexicons"); 69 + const out = join(root, "out"); 70 + 71 + await Deno.mkdir(lexicons, { recursive: true }); 72 + await Deno.writeTextFile( 73 + join(lexicons, "com.example.echo.json"), 74 + JSON.stringify({ 75 + lexicon: 1, 76 + id: "com.example.echo", 77 + defs: { 78 + main: { 79 + type: "query", 80 + output: { 81 + encoding: "application/json", 82 + schema: { 83 + type: "object", 84 + properties: {}, 85 + }, 86 + }, 87 + }, 88 + }, 89 + }), 90 + ); 91 + 92 + const builder = new LexBuilder(); 93 + await builder.load({ lexicons }); 94 + await builder.save({ out, format: false }); 95 + 96 + assertStringIncludes( 97 + await Deno.readTextFile(join(out, "com", "example", "echo.defs.ts")), 98 + 'const $nsid = "com.example.echo";', 99 + ); 100 + } finally { 101 + await Deno.remove(root, { recursive: true }); 102 + } 103 + }, 104 + });