@elsikora/setup-wizard
Version:
Setup Wizard - CLI scaffolding utility
100 lines (97 loc) • 4.68 kB
JavaScript
import { FRAMEWORK_CONFIG } from '../../domain/constant/framework-config.constant.js';
import { EPackageJsonDependencyType } from '../../domain/enum/package-json-dependency-type.enum.js';
/**
* Service for detecting and working with frameworks in a project.
* Provides methods to identify frameworks based on files and dependencies.
*/
class FrameworkService {
/** File system service for performing file operations */
FILE_SYSTEM_SERVICE;
/** Service for working with package.json */
PACKAGE_JSON_SERVICE;
/**
* Initializes a new instance of the FrameworkService.
* @param fileSystemService - Service for file system operations
* @param packageJsonService - Service for managing package.json
*/
constructor(fileSystemService, packageJsonService) {
this.PACKAGE_JSON_SERVICE = packageJsonService;
this.FILE_SYSTEM_SERVICE = fileSystemService;
}
/**
* Detects frameworks used in the current project.
* Checks for framework indicators like specific files or dependencies.
* @returns Promise resolving to an array of detected framework configurations
*/
async detect() {
const detectedFrameworks = [];
const frameworkEntries = Object.entries(FRAMEWORK_CONFIG);
for (const [, config] of frameworkEntries) {
if (await this.isFrameworkDetected(config)) {
detectedFrameworks.push(config);
}
}
return detectedFrameworks;
}
/**
* Extracts and returns unique ESLint features from a list of frameworks.
* @param frameworks - Array of framework configurations
* @returns Array of unique ESLint features from all frameworks
*/
getFeatures(frameworks) {
return [...new Set(frameworks.flatMap((f) => f.features))];
}
/**
* Gets ignore patterns for linting based on framework configurations.
* @param frameworks - Array of framework configurations
* @returns Array of file patterns to ignore during linting
*/
getIgnorePatterns(frameworks) {
return [...new Set(frameworks.flatMap((f) => [...f.ignorePath.directories.map((directory) => `${directory}/**/*`), ...f.ignorePath.patterns]))];
}
/**
* Gets paths to lint based on framework configurations.
* Currently returns the root directory, but could be extended to use framework-specific paths.
* @param frameworks - Array of framework configurations
* @returns Array of paths to lint
*/
getLintPaths(frameworks) {
return ["./"];
// return Array.from(new Set(frameworks.flatMap((f) => f.lintPaths)));
}
/**
* Checks if framework-specific files exist in the project.
* @param config - Framework configuration to check
* @returns Promise resolving to true if any framework-specific files are found
*/
async checkFileIndicators(config) {
if (!config.fileIndicators?.length) {
return false;
}
const fileChecks = config.fileIndicators.map((file) => this.FILE_SYSTEM_SERVICE.isPathExists(file));
const results = await Promise.all(fileChecks);
return results.some(Boolean);
}
/**
* Checks if framework-specific packages are installed in the project.
* @param config - Framework configuration to check
* @returns Promise resolving to true if any framework-specific packages are found
*/
async checkPackageIndicators(config) {
const [dependencies, devDependencies] = await Promise.all([this.PACKAGE_JSON_SERVICE.getDependencies(EPackageJsonDependencyType.PROD), this.PACKAGE_JSON_SERVICE.getDependencies(EPackageJsonDependencyType.DEV)]);
const { dependencies: depIndicators = [], devDependencies: developmentDepIndicators = [], either = [], } = config.packageIndicators;
return depIndicators.some((packageName) => packageName in dependencies) || developmentDepIndicators.some((packageName) => packageName in devDependencies) || either.some((packageName) => packageName in dependencies || packageName in devDependencies);
}
/**
* Determines if a framework is used in the project by checking files and packages.
* @param config - Framework configuration to check
* @returns Promise resolving to true if the framework is detected
*/
async isFrameworkDetected(config) {
const [hasRequiredFiles, hasRequiredPackages] = await Promise.all([this.checkFileIndicators(config), this.checkPackageIndicators(config)]);
return hasRequiredFiles || hasRequiredPackages;
}
}
export { FrameworkService };
//# sourceMappingURL=framework.service.js.map