-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(misc): avoid writing unformatted json if prettier is available
- Loading branch information
1 parent
b59a0a1
commit 8102a7f
Showing
3 changed files
with
97 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type * as Prettier from 'prettier'; | ||
|
||
let prettier: typeof Prettier | null; | ||
export function getPrettierOrNull() { | ||
if (prettier === undefined) { | ||
try { | ||
prettier = require('prettier'); | ||
} catch (e) { | ||
prettier = null; | ||
} | ||
} | ||
return prettier; | ||
} | ||
|
||
export function formatFileContentsWithPrettierIfAvailableSync< | ||
T extends string | Buffer | ||
>(path: string, contents: T, extraOptions?: Prettier.Options): string { | ||
const prettier = getPrettierOrNull(); | ||
const contentsAsString = | ||
typeof contents === 'string' ? contents : contents.toString('utf-8'); | ||
if (!prettier) { | ||
return contentsAsString; | ||
} | ||
|
||
const resolvedOptions = prettier.resolveConfig.sync(path, { | ||
editorconfig: true, | ||
}); | ||
const options: Prettier.Options = { | ||
...resolvedOptions, | ||
...extraOptions, | ||
filepath: path, | ||
}; | ||
|
||
const support = prettier.getFileInfo.sync(path, options as any); | ||
if (support.ignored || !support.inferredParser) { | ||
return contentsAsString; | ||
} | ||
|
||
return prettier.format(contentsAsString, options); | ||
} | ||
|
||
export async function formatFileContentsWithPrettierIfAvailable< | ||
T extends string | Buffer | ||
>(path: string, contents: T, extraOptions?: Prettier.Options): Promise<string> { | ||
const prettier = getPrettierOrNull(); | ||
const contentsAsString = | ||
typeof contents === 'string' ? contents : contents.toString('utf-8'); | ||
if (!prettier) { | ||
return contentsAsString; | ||
} | ||
|
||
const resolvedOptions = await prettier.resolveConfig(path, { | ||
editorconfig: true, | ||
}); | ||
const options: Prettier.Options = { | ||
...resolvedOptions, | ||
...extraOptions, | ||
filepath: path, | ||
}; | ||
|
||
const support = await prettier.getFileInfo(path, options as any); | ||
if (support.ignored || !support.inferredParser) { | ||
return contentsAsString; | ||
} | ||
|
||
return prettier.format(contentsAsString, options); | ||
} |