1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const EXTENSION_TO_PARSER = {
|
5 | ts: 'typescript',
|
6 | tsx: 'typescript',
|
7 | js: 'babylon',
|
8 | jsx: 'babylon',
|
9 | 'js.flow': 'flow',
|
10 | flow: 'flow',
|
11 | gql: 'graphql',
|
12 | graphql: 'graphql',
|
13 | graphqls: 'graphql',
|
14 | css: 'postcss',
|
15 | scss: 'postcss',
|
16 | less: 'postcss',
|
17 | stylus: 'postcss',
|
18 | markdown: 'markdown',
|
19 | md: 'markdown',
|
20 | json: 'json'
|
21 | };
|
22 | async function prettify(filePath, content) {
|
23 | try {
|
24 | const prettierPath = require.resolve('prettier');
|
25 | if (prettierPath) {
|
26 | const prettier = await Promise.resolve().then(() => require('prettier'));
|
27 | const fileExtension = path.extname(filePath).slice(1);
|
28 | const parser = EXTENSION_TO_PARSER[fileExtension];
|
29 | const { ignored } = await prettier.getFileInfo(filePath, { ignorePath: '.prettierignore' });
|
30 | if (ignored) {
|
31 | return content;
|
32 | }
|
33 | const config = await prettier.resolveConfig(filePath, { useCache: true, editorconfig: true });
|
34 | return prettier.format(content, Object.assign({ parser }, (config || {})));
|
35 | }
|
36 | return content;
|
37 | }
|
38 | catch (e) {
|
39 | return content;
|
40 | }
|
41 | }
|
42 | exports.prettify = prettify;
|
43 |
|
\ | No newline at end of file |