Clean code

This commit is contained in:
Fuma Nama
2026-04-09 21:53:42 +08:00
parent c38d1aa9a8
commit d2b211d323
22 changed files with 27 additions and 25 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ async function generate() {
const out = 'content/docs/httpx';
await rimraf(out);
const content = JSON.parse((await fs.readFile('./httpx.json')).toString());
const content = JSON.parse(await fs.readFile('./httpx.json', 'utf-8'));
const converted = Python.convert(content, {
baseUrl: '/docs',
}).filter((file) => !file.path.startsWith('httpx/_transports'));
@@ -5,7 +5,7 @@ import type { Info } from './scripts/generate-info';
export default {
ssr: true,
async prerender({ getStaticPaths }) {
const info: Info = JSON.parse((await fs.readFile('.react-router/_info')).toString());
const info: Info = JSON.parse(await fs.readFile('.react-router/_info', 'utf-8'));
const paths: string[] = [];
for (const path of getStaticPaths()) {
+1 -1
View File
@@ -74,7 +74,7 @@ export async function createOrLoadConfig(file = './cli.json'): Promise<LoadedCon
const inited = await initConfig(file);
if (inited) return inited;
const content = (await fs.readFile(file)).toString();
const content = await fs.readFile(file, 'utf-8');
const configSchema = createConfigSchema();
return configSchema.parse(JSON.parse(content));
+3 -1
View File
@@ -139,7 +139,9 @@ async function initPackageJson(
return deps;
}
const packageJson: PackageJsonType = JSON.parse((await fs.readFile(packageJsonPath)).toString());
const packageJson: PackageJsonType = JSON.parse(
await fs.readFile(packageJsonPath, 'utf-8'),
);
return {
...packageJson,
@@ -31,7 +31,7 @@ export function nextUseSrc(): TemplatePlugin {
if (!this.template.value.startsWith('+next')) return;
const tsconfigPath = path.join(this.dest, 'tsconfig.json');
const content = (await fs.readFile(tsconfigPath)).toString();
const content = await fs.readFile(tsconfigPath, 'utf-8');
const config = JSON.parse(content);
if (config.compilerOptions?.paths) {
+1 -1
View File
@@ -11,7 +11,7 @@ const project = new Project({
});
export async function createSourceFile(path: string) {
return project.createSourceFile(path, (await fs.readFile(path)).toString(), {
return project.createSourceFile(path, await fs.readFile(path, 'utf-8'), {
overwrite: true,
});
}
+1 -1
View File
@@ -18,7 +18,7 @@ const project = new Project({
});
async function createSourceFile(templatePath: string) {
const content = (await fs.readFile(path.join(__dirname, templatePath))).toString();
const content = await fs.readFile(path.join(__dirname, templatePath), 'utf-8');
return project.createSourceFile('temp.ts', content, {
overwrite: true,
+1 -1
View File
@@ -49,7 +49,7 @@ export function fileGenerator({
const dest = relative
? path.resolve(ctx.cwd, path.dirname(ctx.path), file)
: path.resolve(ctx.cwd, file);
let value = await fs.readFile(dest).then((res) => res.toString());
let value = await fs.readFile(dest, 'utf-8');
if (trim) value = value.trim();
if (codeblock === false) {
+1 -1
View File
@@ -34,7 +34,7 @@ const compilerProduction = createCompiler({
});
for (const file of files) {
const raw = (await fs.readFile(path.join(dir, file))).toString();
const raw = await fs.readFile(path.join(dir, file), 'utf-8');
const { frontmatter, content } = parseFrontmatter(raw);
test(`compile: ${file}`, async () => {
+2 -2
View File
@@ -68,7 +68,7 @@ export function toNode(loader: Loader): LoadHook {
filePath,
query: Object.fromEntries(parsedUrl.searchParams.entries()),
async getSource() {
return (await fs.readFile(filePath)).toString();
return await fs.readFile(filePath, 'utf-8');
},
development: false,
compiler: {
@@ -198,7 +198,7 @@ export function toBun(loader: Loader) {
};
if (loader.bun?.load) {
return loader.bun.load(readFileSync(filePath).toString(), input);
return loader.bun.load(readFileSync(filePath, 'utf-8'), input);
}
const result = loader.load(input);
@@ -203,7 +203,7 @@ export function remarkInclude(this: Processor): Transformer<Root, Root> {
const { _getProcessor = () => this, _compiler } = parent.data;
let content: string;
try {
content = (await fs.readFile(targetPath)).toString();
content = await fs.readFile(targetPath, 'utf-8');
} catch (e) {
throw new Error(
`failed to read file ${targetPath}\n${e instanceof Error ? e.message : String(e)}`,
+1 -1
View File
@@ -46,7 +46,7 @@ export default function jsonSchema({ insert = false }: JSONSchemaOptions = {}):
if (!match) return;
let obj: object;
try {
const content = (await fs.readFile(file)).toString();
const content = await fs.readFile(file, 'utf-8');
obj = content.length > 0 ? JSON.parse(content) : {};
} catch {
return;
+1 -1
View File
@@ -44,7 +44,7 @@ export async function dynamic<Config, TC extends InternalTypeConfig>(
const body: Record<string, () => Promise<unknown>> = {};
async function compile({ info, data }: LazyEntry<unknown>) {
let content = (await fs.readFile(info.fullPath)).toString();
let content = await fs.readFile(info.fullPath, 'utf-8');
content = fumaMatter(content).content;
const compiled = await buildMDX(core, collection, {
+1 -1
View File
@@ -272,7 +272,7 @@ function createDocMethods(
if (type === 'raw') {
const fs = await import('node:fs/promises');
return (await fs.readFile(info.fullPath)).toString();
return await fs.readFile(info.fullPath, 'utf-8');
}
const data = await load();
+1 -1
View File
@@ -10,7 +10,7 @@ export function createFSCache() {
const cached = map.get(fullPath);
if (cached) return cached;
const read = fs.readFile(fullPath).then((s) => s.toString());
const read = fs.readFile(fullPath, 'utf-8');
map.set(fullPath, read);
return read;
},
+2 -2
View File
@@ -115,7 +115,7 @@ async function md(
absolutePath: string,
file: string,
): Promise<RawPage> {
const content = (await fs.readFile(absolutePath)).toString();
const content = await fs.readFile(absolutePath, 'utf-8');
const parsed = grayMatter({ content });
// use default frontmatter if invalid in Fumadocs' spec
@@ -140,7 +140,7 @@ async function json(
absolutePath: string,
file: string,
): Promise<RawMeta | undefined> {
const content = (await fs.readFile(absolutePath)).toString();
const content = await fs.readFile(absolutePath, 'utf-8');
const parsed = JSON.parse(content);
const result = metaSchema.loose().safeParse(parsed);
+1 -1
View File
@@ -16,7 +16,7 @@ export function createFileSystemCache(dir: string): Cache {
},
async read(key) {
try {
return (await fs.readFile(path.join(dir, `${key}.json`))).toString();
return await fs.readFile(path.join(dir, `${key}.json`), 'utf-8');
} catch {
return;
}
+2 -2
View File
@@ -38,13 +38,13 @@ export function createFileSystemTypesCache(
if (!fs.existsSync(filePath)) {
return null;
}
return JSON.parse(fs.readFileSync(filePath, { encoding: 'utf-8' }));
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
},
write(code, data) {
const hash = createHash('SHA256').update(code).digest('hex').slice(0, 12);
const filePath = path.join(dir, `${hash}.json`);
const json = JSON.stringify(data);
fs.writeFileSync(filePath, json, { encoding: 'utf-8' });
fs.writeFileSync(filePath, json, 'utf-8');
},
};
}
+1 -1
View File
@@ -16,7 +16,7 @@ export function createFileSystemGeneratorCache(dir: string): Cache {
},
async read(hash) {
try {
return JSON.parse((await fs.readFile(path.join(dir, `${hash}.json`))).toString());
return JSON.parse(await fs.readFile(path.join(dir, `${hash}.json`), 'utf-8'));
} catch {
return;
}
+1 -1
View File
@@ -125,7 +125,7 @@ export function createGenerator(options: GeneratorOptions = {}) {
options: GenerateOptions = {},
) {
const fullPath = path.resolve(file.path);
const content = file.content ?? (await fs.readFile(fullPath)).toString();
const content = file.content ?? (await fs.readFile(fullPath, 'utf-8'));
let cacheKey: string | undefined;
if (cache) {
cacheKey = generateHash(`${file.path}:${name}:${content}:${packageVersion}`);
+1 -1
View File
@@ -55,7 +55,7 @@ export async function getTypeTableOutput(
let content = '';
if (file) {
content = (await fs.readFile(file)).toString();
content = await fs.readFile(file, 'utf-8');
}
if (type && type.split('\n').length > 1) {
+1 -1
View File
@@ -41,7 +41,7 @@ test('Run on MDX files', async () => {
const output = await processor.process({
path: file,
value: (await fs.readFile(file)).toString(),
value: await fs.readFile(file, 'utf-8'),
});
await expect(String(output.value)).toMatchFileSnapshot('./fixtures/test.output.js');
});