@elsikora/eslint-config
Version:
ESLint configuration vision of ElsiKora
129 lines (126 loc) • 6.94 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": () => Promise.resolve().then(function () { return require('../../infrastructure/config/check-file.js'); }),
css: () => Promise.resolve().then(function () { return require('../../infrastructure/config/css.js'); }),
fsd: () => Promise.resolve().then(function () { return require('../../infrastructure/config/fsd.js'); }),
i18next: () => Promise.resolve().then(function () { return require('../../infrastructure/config/i18next.js'); }),
javascript: () => Promise.resolve().then(function () { return require('../../infrastructure/config/javascript.js'); }),
jsdoc: () => Promise.resolve().then(function () { return require('../../infrastructure/config/jsdoc.js'); }),
json: () => Promise.resolve().then(function () { return require('../../infrastructure/config/json.js'); }),
jsx: () => Promise.resolve().then(function () { return require('../../infrastructure/config/jsx.js'); }),
markdown: () => Promise.resolve().then(function () { return require('../../infrastructure/config/markdown.js'); }),
nest: () => Promise.resolve().then(function () { return require('../../infrastructure/config/nest.js'); }),
next: () => Promise.resolve().then(function () { return require('../../infrastructure/config/next.js'); }),
"no-secrets": () => Promise.resolve().then(function () { return require('../../infrastructure/config/no-secrets.js'); }),
node: () => Promise.resolve().then(function () { return require('../../infrastructure/config/node.js'); }),
"package-json": () => Promise.resolve().then(function () { return require('../../infrastructure/config/package-json.js'); }),
perfectionist: () => Promise.resolve().then(function () { return require('../../infrastructure/config/perfectionist.js'); }),
prettier: () => Promise.resolve().then(function () { return require('../../infrastructure/config/prettier.js'); }),
react: () => Promise.resolve().then(function () { return require('../../infrastructure/config/react.js'); }),
regexp: () => Promise.resolve().then(function () { return require('../../infrastructure/config/regexp.js'); }),
sonar: () => Promise.resolve().then(function () { return require('../../infrastructure/config/sonar.js'); }),
storybook: () => Promise.resolve().then(function () { return require('../../infrastructure/config/storybook.js'); }),
stylistic: () => Promise.resolve().then(function () { return require('../../infrastructure/config/stylistic.js'); }),
"tailwind-css": () => Promise.resolve().then(function () { return require('../../infrastructure/config/tailwind-css.js'); }),
tanstack: () => Promise.resolve().then(function () { return require('../../infrastructure/config/tanstack.js'); }),
typeorm: () => Promise.resolve().then(function () { return require('../../infrastructure/config/typeorm.js'); }),
typescript: () => Promise.resolve().then(function () { return require('../../infrastructure/config/typescript.js'); }),
unicorn: () => Promise.resolve().then(function () { return require('../../infrastructure/config/unicorn.js'); }),
yaml: () => Promise.resolve().then(function () { return require('../../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 [];
}
}
}
exports.ConfigFactory = ConfigFactory;
//# sourceMappingURL=config.factory.js.map