@elsikora/eslint-config
Version:
ESLint configuration vision of ElsiKora
127 lines (125 loc) • 5.59 kB
JavaScript
/**
* Factory class for generating ESLint configurations based on provided options.
* Maps configuration flags to their respective module loaders and dynamically imports
* the required config modules. Handles loading failures gracefully by logging warnings
* and returning empty configs.
* @class ConfigFactory
* @static
*/
class ConfigFactory {
static OPTIONS_TO_CONFIG_MAP = {
withCheckFile: "check-file",
withCss: "css",
withFsd: "fsd",
withI18next: "i18next",
withJavascript: "javascript",
withJsDoc: "jsdoc",
withJson: "json",
withJsx: "jsx",
withMarkdown: "markdown",
withNest: "nest",
withNext: "next",
withNode: "node",
withNoSecrets: "no-secrets",
withPackageJson: "package-json",
withPerfectionist: "perfectionist",
withPrettier: "prettier",
withReact: "react",
withRegexp: "regexp",
withSonar: "sonar",
withStorybook: "storybook",
withStylistic: "stylistic",
withTailwindCss: "tailwind-css",
withTanstack: "tanstack",
withTypeorm: "typeorm",
withTypescript: "typescript",
withUnicorn: "unicorn",
withYaml: "yaml",
};
static CONFIG_MAPPING = {
"check-file": () => import('../../infrastructure/config/check-file.js'),
css: () => import('../../infrastructure/config/css.js'),
fsd: () => import('../../infrastructure/config/fsd.js'),
i18next: () => import('../../infrastructure/config/i18next.js'),
javascript: () => import('../../infrastructure/config/javascript.js'),
jsdoc: () => import('../../infrastructure/config/jsdoc.js'),
json: () => import('../../infrastructure/config/json.js'),
jsx: () => import('../../infrastructure/config/jsx.js'),
markdown: () => import('../../infrastructure/config/markdown.js'),
nest: () => import('../../infrastructure/config/nest.js'),
next: () => import('../../infrastructure/config/next.js'),
"no-secrets": () => import('../../infrastructure/config/no-secrets.js'),
node: () => import('../../infrastructure/config/node.js'),
"package-json": () => import('../../infrastructure/config/package-json.js'),
perfectionist: () => import('../../infrastructure/config/perfectionist.js'),
prettier: () => import('../../infrastructure/config/prettier.js'),
react: () => import('../../infrastructure/config/react.js'),
regexp: () => import('../../infrastructure/config/regexp.js'),
sonar: () => import('../../infrastructure/config/sonar.js'),
storybook: () => import('../../infrastructure/config/storybook.js'),
stylistic: () => import('../../infrastructure/config/stylistic.js'),
"tailwind-css": () => import('../../infrastructure/config/tailwind-css.js'),
tanstack: () => import('../../infrastructure/config/tanstack.js'),
typeorm: () => import('../../infrastructure/config/typeorm.js'),
typescript: () => import('../../infrastructure/config/typescript.js'),
unicorn: () => import('../../infrastructure/config/unicorn.js'),
yaml: () => import('../../infrastructure/config/yaml.js'),
};
static currentOptions = null;
/**
* Creates ESLint configurations based on the provided options.
*
* This function processes the configuration options and dynamically imports
* the required ESLint configuration modules based on enabled features.
* It filters out disabled options and loads only the necessary configurations.
* @param {IConfigOptions} options - Configuration options that determine which ESLint rules to include
* @returns {Promise<Array<Linter.Config>>} A promise that resolves to an array of ESLint configurations
* @example
* // Basic usage with typescript and react
* const config = await ConfigFactory.createConfig({
* withTypescript: true,
* withReact: true
* });
* @example
* // Full-featured configuration for a modern web application
* const fullConfig = await ConfigFactory.createConfig({
* withTypescript: true,
* withReact: true,
* withEslint: true,
* withPrettier: true,
* withJsDoc: true,
* withStylistic: true
* });
*/
static async createConfig(options) {
this.currentOptions = options;
const configPromises = Object.entries(options)
.filter(([key, value]) => value === true && this.OPTIONS_TO_CONFIG_MAP[key])
.map(([key]) => {
const configName = this.OPTIONS_TO_CONFIG_MAP[key];
return this.loadConfig(configName);
});
const config = await Promise.all(configPromises);
this.currentOptions = null;
return config.flat();
}
/**
* Loads a specific ESLint configuration module by name
* @param {string} name - The name of the configuration module to load
* @returns {Promise<Array<Linter.Config>>} A promise that resolves to an array of ESLint configurations
* @private
*/
static async loadConfig(name) {
try {
// @ts-ignore
const module = await this.CONFIG_MAPPING[name]();
return module.default(this.currentOptions);
}
catch (error) {
console.warn(`Optional dependency for ${name} config is not installed:`, error);
return [];
}
}
}
export { ConfigFactory };
//# sourceMappingURL=config.factory.js.map