How to use prettier

Comprehensive prettier code examples:

How to use prettier.check:

29
30
31
32
33
34
35
36
37
38
const input = fs.readFileSync(file, 'utf8');
const withParserOptions = {
  ...options,
  parser: fileInfo.inferredParser,
};
const isPrettier = prettier.check(input, withParserOptions);
if (!isPrettier) {
  console.log(
    `\x1b[31m ${file} is no prettier, please use npm run prettier and git add !\x1b[0m`
  );

How to use prettier.getFileInfo:

7
8
9
10
11
12
13
14
15
const rootPath = getCurrentWorkspaceRootFsPath();
const ignoreOptions = {
    ignorePath: path.join(rootPath, ".prettierignore"),
};

const fileInfo = prettier.getFileInfo.sync(
    document.uri.fsPath,
    ignoreOptions
);

How to use prettier.default:

63
64
65
66
67
68
69
70
71
72
    .split("export type")
    .map(s => s.trim().split(" =")[0])
    .slice(1);
let guards = (0, utils_1.generateGuards)(typeNames, superstructNames);
console.log("\nFormatting files...");
guards = prettier_1.default.format(guards, { parser: "typescript" });
typescript = prettier_1.default.format(typescript, { parser: "typescript" });
superstruct = prettier_1.default.format(superstruct, { parser: "typescript" });
console.log("\nWriting files...");
yield fs.writeFile(outPathTS, typescript);

How to use prettier.resolveConfig:

116
117
118
119
120
121
122
123
124
125
    return undefined;
  },
});
const codeWithPropTypes = typescriptToProptypes.inject(propTypesAST, code);

const prettierConfig = prettier.resolveConfig.sync(jsPath, {
  config: path.join(workspaceRoot, 'prettier.config.js'),
});
const prettierFormat = (jsSource) =>
  prettier.format(jsSource, { ...prettierConfig, filepath: jsPath });

How to use prettier.format:

171
172
173
174
175
176
177
178
179
180
    }

    configure(getStories, module, false)
`;

const formattedFileContent = prettier.format(fileContent, { parser: 'babel' });

fs.writeFileSync(storybookRequiresLocation, formattedFileContent, {
  encoding: 'utf8',
  flag: 'w',