Skip to content

Commit

Permalink
fix: format types files with prettier (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa authored Dec 17, 2024
1 parent 0516005 commit e526816
Show file tree
Hide file tree
Showing 3 changed files with 1,066 additions and 10 deletions.
19 changes: 13 additions & 6 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import mimeScore, { FACET_SCORES } from 'mime-score';
import { mkdir, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import * as prettier from 'prettier';

const MIME_DB_URL =
'https://raw.githubusercontent.com/jshttp/mime-db/master/db.json';
Expand All @@ -23,6 +24,8 @@ type MimeScoreEntry = Omit<MimeEntry, 'extensions'> & {
score: number;
};

const PRETTIER_OPTIONS = await prettier.resolveConfig(__dirname);

function normalizeTypes(types: MimeDatabase) {
const cloned: Record<string, MimeScoreEntry> = {};
const byExtension: Record<string, MimeScoreEntry> = {};
Expand Down Expand Up @@ -94,12 +97,16 @@ async function writeTypesFile(name: string, types: Record<string, string[]>) {
const filepath = path.join(dirpath, `${name}.ts`);
await mkdir(dirpath, { recursive: true });

await writeFile(
filepath,
`const types : {[key: string]: string[]} = ${JSON.stringify(types)};
Object.freeze(types);
export default types;`,
);
let source = `const types : {[key: string]: string[]} = ${JSON.stringify(types)};
Object.freeze(types);
export default types;`;

source = await prettier.format(source, {
parser: 'typescript',
...PRETTIER_OPTIONS,
});

await writeFile(filepath, source);
}

async function main() {
Expand Down
Loading

0 comments on commit e526816

Please sign in to comment.