UNPKG

4.8 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14 return new (P || (P = Promise))(function (resolve, reject) {
15 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
18 step((generator = generator.apply(thisArg, _arguments || [])).next());
19 });
20};
21var __generator = (this && this.__generator) || function (thisArg, body) {
22 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24 function verb(n) { return function (v) { return step([n, v]); }; }
25 function step(op) {
26 if (f) throw new TypeError("Generator is already executing.");
27 while (_) try {
28 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29 if (y = 0, t) op = [op[0] & 2, t.value];
30 switch (op[0]) {
31 case 0: case 1: t = op; break;
32 case 4: _.label++; return { value: op[1], done: false };
33 case 5: _.label++; y = op[1]; op = [0]; continue;
34 case 7: op = _.ops.pop(); _.trys.pop(); continue;
35 default:
36 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40 if (t[2]) _.ops.pop();
41 _.trys.pop(); continue;
42 }
43 op = body.call(thisArg, _);
44 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46 }
47};
48Object.defineProperty(exports, "__esModule", { value: true });
49var path = require("path");
50var EXTENSION_TO_PARSER = {
51 ts: 'typescript',
52 tsx: 'typescript',
53 js: 'babylon',
54 jsx: 'babylon',
55 'js.flow': 'flow',
56 flow: 'flow',
57 gql: 'graphql',
58 graphql: 'graphql',
59 graphqls: 'graphql',
60 css: 'postcss',
61 scss: 'postcss',
62 less: 'postcss',
63 stylus: 'postcss',
64 markdown: 'markdown',
65 md: 'markdown',
66 json: 'json'
67};
68function prettify(filePath, content) {
69 return __awaiter(this, void 0, void 0, function () {
70 var prettierPath, prettier, fileExtension, parser, ignored, config, e_1;
71 return __generator(this, function (_a) {
72 switch (_a.label) {
73 case 0:
74 _a.trys.push([0, 4, , 5]);
75 prettierPath = require.resolve('prettier');
76 if (!prettierPath) return [3 /*break*/, 3];
77 prettier = require('prettier');
78 fileExtension = path.extname(filePath).slice(1);
79 parser = EXTENSION_TO_PARSER[fileExtension];
80 return [4 /*yield*/, prettier.getFileInfo(filePath, { ignorePath: '.prettierignore' })];
81 case 1:
82 ignored = (_a.sent()).ignored;
83 if (ignored) {
84 return [2 /*return*/, content];
85 }
86 return [4 /*yield*/, prettier.resolveConfig(filePath, { useCache: true, editorconfig: true })];
87 case 2:
88 config = _a.sent();
89 return [2 /*return*/, prettier.format(content, __assign({ parser: parser }, (config || {})))];
90 case 3: return [2 /*return*/, content];
91 case 4:
92 e_1 = _a.sent();
93 return [2 /*return*/, content];
94 case 5: return [2 /*return*/];
95 }
96 });
97 });
98}
99exports.prettify = prettify;
100//# sourceMappingURL=prettier.js.map
\No newline at end of file