import { Obj } from './type' export async function formatCode({ filepath, str, }: { filepath: string str: string }): Promise { // 如果格式化失败, 则返回原始内容 try { // 动态引入 prettier, 免得 prettier 找不到配置文件对应的包而报错 const prettier = await import('prettier') const config = await prettier.resolveConfig(filepath) return prettier.format(str, { filepath, ...config }) } catch (err) { return str } } export async function formatJson({ filepath, obj, }: { filepath: string obj: T }): Promise { return formatCode({ str: JSON.stringify(obj, null, 2), filepath }) }