@textlint/fixer-formatter
Version:
textlint output formatter for fixer
93 lines • 3.51 kB
JavaScript
// LICENSE : MIT
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadFormatter = loadFormatter;
exports.getFixerFormatterList = getFixerFormatterList;
const module_interop_1 = require("@textlint/module-interop");
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const debug_1 = __importDefault(require("debug"));
const resolver_1 = require("@textlint/resolver");
// formatters
const compats_js_1 = __importDefault(require("./formatters/compats.js"));
const diff_js_1 = __importDefault(require("./formatters/diff.js"));
const fixed_result_js_1 = __importDefault(require("./formatters/fixed-result.js"));
const json_js_1 = __importDefault(require("./formatters/json.js"));
const stylish_js_1 = __importDefault(require("./formatters/stylish.js"));
const builtinFormatterList = {
compats: compats_js_1.default,
diff: diff_js_1.default,
"fixed-result": fixed_result_js_1.default,
json: json_js_1.default,
stylish: stylish_js_1.default
};
const isFormatterFunction = (formatter) => {
return typeof formatter === "function";
};
const builtinFormatterNames = Object.keys(builtinFormatterList);
const debug = (0, debug_1.default)("textlint:textfix-formatter");
async function loadFormatter(formatterConfig) {
var _a;
const formatterName = formatterConfig.formatterName;
debug(`formatterName: ${formatterName}`);
if (builtinFormatterNames.includes(formatterName)) {
return {
format(results) {
return builtinFormatterList[formatterName](results, formatterConfig);
}
};
}
let formatter;
let formatterPath;
if (node_fs_1.default.existsSync(formatterName)) {
formatterPath = formatterName;
}
else if (node_fs_1.default.existsSync(node_path_1.default.resolve(process.cwd(), formatterName))) {
formatterPath = node_path_1.default.resolve(process.cwd(), formatterName);
}
else {
const pkgPath = (0, resolver_1.tryResolve)(`textlint-formatter-${formatterName}`, {
parentModule: "fixer-formatter"
}) ||
(0, resolver_1.tryResolve)(formatterName, {
parentModule: "fixer-formatter"
});
if (pkgPath) {
formatterPath = pkgPath;
}
}
if (!formatterPath) {
throw new Error(`Could not find formatter ${formatterName}`);
}
try {
const mod = (_a = (0, module_interop_1.moduleInterop)((await (0, resolver_1.dynamicImport)(formatterPath, {
parentModule: "fixer-formatter"
})).exports)) === null || _a === void 0 ? void 0 : _a.default;
if (!isFormatterFunction(mod)) {
throw new Error(`formatter should export function, but ${formatterPath} exports ${typeof mod}`);
}
formatter = mod;
}
catch (ex) {
throw new Error(`Could not find formatter ${formatterName}
See https://github.com/textlint/textlint/issues/148
${ex}`);
}
debug(`use formatter: ${formatterPath}`);
return {
format(results) {
return formatter(results, formatterConfig);
}
};
}
function getFixerFormatterList() {
return builtinFormatterNames.map((name) => {
return {
name
};
});
}
//# sourceMappingURL=index.js.map