maybe-prettier v1.0.1
maybe-prettier
Format code using the Prettier API if it's installed and configured locally in the current project.
Install
$ npm install maybe-prettier
Import
Node.js / CommonJS:
const { format } = require('maybe-prettier')
ESNext / TypeScript:
import { format } from 'maybe-prettier'
Usage
maybe-prettier
will format source code only if prettier
is installed locally
and a prettier
configuration file is present in the project. Otherwise, the
input source code is returned unmodified.
const formattedCode = await format({
fileName: '/my-project/hello-world.js',
source: 'console.log("Hello world!")'
})
Signature
/**
* @param context - Context
* @param options - Options
* @returns Formatted source.
*/
export async function format(
context: Context,
options: Options = {}
): Promise<string> {
Context
cwd
Type: string
Default: process.cwd()
Current working directory (used for discovering the prettier config file).
fileName
Type: string
The name of the current file.
source
Type: string
The source code of current file.
Options
You do not need to explicitly define options. Options are inferred from the file name.
This is only for advanced users familiar with the Prettier API.
fileInfo
Type: Prettier.FileInfoOptions
Passed directly to Prettier.
resolveConfig:
Type: Prettier.ResolveConfigOptions
Passed directly to Prettier.