textlint
Version:
The pluggable linting tool for text and markdown.
54 lines • 2.21 kB
JavaScript
import { loadPackagesFromRawConfig } from "@textlint/config-loader";
import { TextlintKernelDescriptor } from "@textlint/kernel";
import { normalizeTextlintPluginKey, normalizeTextlintRuleKey } from "@textlint/utils";
import debug_ from "debug";
import { loadFromDirAsESM } from "../engine/rule-loader";
const debug = debug_("textlint:cli-loader");
export const loadCliDescriptor = async (cliOptions) => {
var _a, _b, _c, _d, _e;
const rawRules = Object.create(null);
const rawPlugins = Object.create(null);
// --rule
(_a = cliOptions.rule) === null || _a === void 0 ? void 0 : _a.forEach((rule) => {
rawRules[normalizeTextlintRuleKey(rule)] = true;
});
// --preset
(_b = cliOptions.preset) === null || _b === void 0 ? void 0 : _b.forEach((preset) => {
//:support --preset=jtf-style
if (preset.includes("preset-")) {
rawRules[preset] = true;
}
else {
rawRules[`preset-${preset}`] = true;
}
});
// --plugin
(_c = cliOptions.plugin) === null || _c === void 0 ? void 0 : _c.forEach((plugin) => {
rawPlugins[normalizeTextlintPluginKey(plugin)] = true;
});
const result = await loadPackagesFromRawConfig({
node_moduleDir: cliOptions.rulesBaseDirectory,
rawConfig: {
...(Object.keys(rawRules).length > 0 ? { rules: rawRules } : {}),
...(Object.keys(rawPlugins).length > 0 ? { plugins: rawPlugins } : {})
}
});
if (!result.ok) {
debug("loadPackagesFromRawConfig failed: %o", result);
throw new Error("Failed to load packages", {
cause: result.error
});
}
// --rulesdir
const rulesDirRules = (await Promise.all((_e = (_d = cliOptions.rulesdir) === null || _d === void 0 ? void 0 : _d.map((rulesdir) => {
debug("Loading rules from %o", rulesdir);
return loadFromDirAsESM(rulesdir);
})) !== null && _e !== void 0 ? _e : [])).flat();
const { rules, plugins, filterRules } = result.config;
return new TextlintKernelDescriptor({
rules: [...rules, ...rulesDirRules],
plugins,
filterRules
});
};
//# sourceMappingURL=CliLoader.js.map