2018-09-06 13:51:18 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
2020-01-14 23:30:18 +08:00
|
|
|
const { version } = require("../package.json");
|
2018-09-06 13:51:18 +08:00
|
|
|
|
2021-07-21 09:10:34 +08:00
|
|
|
const core = require("./main/core.js");
|
|
|
|
|
const { getSupportInfo } = require("./main/support.js");
|
|
|
|
|
const getFileInfo = require("./common/get-file-info.js");
|
|
|
|
|
const sharedUtil = require("./common/util-shared.js");
|
|
|
|
|
const plugins = require("./common/load-plugins.js");
|
|
|
|
|
const config = require("./config/resolve-config.js");
|
|
|
|
|
const doc = require("./document/index.js");
|
2018-09-06 13:51:18 +08:00
|
|
|
|
2020-02-23 02:47:17 +02:00
|
|
|
function _withPlugins(
|
|
|
|
|
fn,
|
|
|
|
|
optsArgIdx = 1 // Usually `opts` is the 2nd argument
|
|
|
|
|
) {
|
|
|
|
|
return (...args) => {
|
|
|
|
|
const opts = args[optsArgIdx] || {};
|
|
|
|
|
args[optsArgIdx] = {
|
2020-01-14 20:05:28 +08:00
|
|
|
...opts,
|
2020-02-27 22:43:02 +08:00
|
|
|
plugins: plugins.loadPlugins(opts.plugins, opts.pluginSearchDirs),
|
2020-01-14 20:05:28 +08:00
|
|
|
};
|
2020-02-23 02:47:17 +02:00
|
|
|
return fn(...args);
|
2018-09-06 13:51:18 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-10 15:26:07 +09:00
|
|
|
/**
|
|
|
|
|
* @param {*} fn
|
|
|
|
|
* @param {*} optsArgIdx
|
|
|
|
|
* @returns {*}
|
|
|
|
|
*/
|
2020-02-23 02:47:17 +02:00
|
|
|
function withPlugins(fn, optsArgIdx) {
|
|
|
|
|
const resultingFn = _withPlugins(fn, optsArgIdx);
|
2018-09-06 13:51:18 +08:00
|
|
|
if (fn.sync) {
|
2021-08-09 00:00:22 +09:00
|
|
|
// @ts-expect-error
|
2020-02-23 02:47:17 +02:00
|
|
|
resultingFn.sync = _withPlugins(fn.sync, optsArgIdx);
|
2018-09-06 13:51:18 +08:00
|
|
|
}
|
|
|
|
|
return resultingFn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const formatWithCursor = withPlugins(core.formatWithCursor);
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
formatWithCursor,
|
|
|
|
|
|
|
|
|
|
format(text, opts) {
|
|
|
|
|
return formatWithCursor(text, opts).formatted;
|
|
|
|
|
},
|
|
|
|
|
|
2020-01-14 19:25:24 +08:00
|
|
|
check(text, opts) {
|
2020-01-14 23:30:18 +08:00
|
|
|
const { formatted } = formatWithCursor(text, opts);
|
2018-09-06 13:51:18 +08:00
|
|
|
return formatted === text;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
doc,
|
|
|
|
|
|
|
|
|
|
resolveConfig: config.resolveConfig,
|
|
|
|
|
resolveConfigFile: config.resolveConfigFile,
|
2020-02-27 22:43:02 +08:00
|
|
|
clearConfigCache() {
|
|
|
|
|
config.clearCache();
|
|
|
|
|
plugins.clearCache();
|
|
|
|
|
},
|
2018-09-06 13:51:18 +08:00
|
|
|
|
2020-11-23 13:57:13 +08:00
|
|
|
/** @type {typeof getFileInfo} */
|
|
|
|
|
getFileInfo: withPlugins(getFileInfo),
|
|
|
|
|
/** @type {typeof getSupportInfo} */
|
|
|
|
|
getSupportInfo: withPlugins(getSupportInfo, 0),
|
2018-09-06 13:51:18 +08:00
|
|
|
|
|
|
|
|
version,
|
|
|
|
|
|
|
|
|
|
util: sharedUtil,
|
|
|
|
|
|
2020-05-06 18:43:04 +08:00
|
|
|
// Internal shared
|
|
|
|
|
__internal: {
|
2021-07-21 09:10:34 +08:00
|
|
|
errors: require("./common/errors.js"),
|
|
|
|
|
coreOptions: require("./main/core-options.js"),
|
|
|
|
|
createIgnorer: require("./common/create-ignorer.js"),
|
|
|
|
|
optionsModule: require("./main/options.js"),
|
|
|
|
|
optionsNormalizer: require("./main/options-normalizer.js"),
|
2020-05-06 18:43:04 +08:00
|
|
|
utils: {
|
2021-07-21 09:10:34 +08:00
|
|
|
arrayify: require("./utils/arrayify.js"),
|
2021-12-24 19:34:18 +08:00
|
|
|
getLast: require("./utils/get-last.js"),
|
2022-04-10 15:26:07 +09:00
|
|
|
partition: require("./utils/partition.js"),
|
2022-02-18 16:36:53 +08:00
|
|
|
isNonEmptyArray: require("./common/util.js").isNonEmptyArray,
|
2020-05-06 18:43:04 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
2018-09-06 13:51:18 +08:00
|
|
|
/* istanbul ignore next */
|
|
|
|
|
__debug: {
|
|
|
|
|
parse: withPlugins(core.parse),
|
|
|
|
|
formatAST: withPlugins(core.formatAST),
|
|
|
|
|
formatDoc: withPlugins(core.formatDoc),
|
|
|
|
|
printToDoc: withPlugins(core.printToDoc),
|
|
|
|
|
printDocToString: withPlugins(core.printDocToString),
|
|
|
|
|
},
|
|
|
|
|
};
|