forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (54 loc) · 1.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"use strict";
const version = require("./package.json").version;
const core = require("./src/main/core");
const getSupportInfo = require("./src/main/support").getSupportInfo;
const getFileInfo = require("./src/common/get-file-info");
const sharedUtil = require("./src/common/util-shared");
const loadPlugins = require("./src/common/load-plugins");
const config = require("./src/config/resolve-config");
const doc = require("./src/doc");
// Luckily `opts` is always the 2nd argument
function _withPlugins(fn) {
return function() {
const args = Array.from(arguments);
const opts = args[1] || {};
args[1] = Object.assign({}, opts, {
plugins: loadPlugins(opts.plugins, opts.pluginSearchDirs)
});
return fn.apply(null, args);
};
}
function withPlugins(fn) {
const resultingFn = _withPlugins(fn);
if (fn.sync) {
resultingFn.sync = _withPlugins(fn.sync);
}
return resultingFn;
}
const formatWithCursor = withPlugins(core.formatWithCursor);
module.exports = {
formatWithCursor,
format(text, opts) {
return formatWithCursor(text, opts).formatted;
},
check: function(text, opts) {
const formatted = formatWithCursor(text, opts).formatted;
return formatted === text;
},
doc,
resolveConfig: config.resolveConfig,
resolveConfigFile: config.resolveConfigFile,
clearConfigCache: config.clearCache,
getFileInfo: withPlugins(getFileInfo),
getSupportInfo: withPlugins(getSupportInfo),
version,
util: sharedUtil,
/* istanbul ignore next */
__debug: {
parse: withPlugins(core.parse),
formatAST: withPlugins(core.formatAST),
formatDoc: withPlugins(core.formatDoc),
printToDoc: withPlugins(core.printToDoc),
printDocToString: withPlugins(core.printDocToString)
}
};