@elsikora/setup-wizard
Version:
Setup Wizard - CLI scaffolding utility
133 lines (130 loc) • 5.78 kB
JavaScript
import { GITIGNORE_CONFIG } from '../../domain/constant/gitignore-config.constant.js';
import { EModule } from '../../domain/enum/module.enum.js';
/**
* Service for setting up and managing .gitignore file.
* Provides functionality to create a comprehensive .gitignore file
* that helps prevent unwanted files from being tracked by Git.
*/
class GitignoreModuleService {
/** CLI interface service for user interaction */
CLI_INTERFACE_SERVICE;
/** Configuration service for managing app configuration */
CONFIG_SERVICE;
/** File system service for file operations */
FILE_SYSTEM_SERVICE;
/**
* Initializes a new instance of the GitignoreModuleService.
* @param cliInterfaceService - Service for CLI user interactions
* @param fileSystemService - Service for file system operations
* @param configService - Service for managing app configuration
*/
constructor(cliInterfaceService, fileSystemService, configService) {
this.CLI_INTERFACE_SERVICE = cliInterfaceService;
this.FILE_SYSTEM_SERVICE = fileSystemService;
this.CONFIG_SERVICE = configService;
}
/**
* Handles existing .gitignore setup.
* Checks for existing .gitignore file and asks if user wants to replace it.
* @returns Promise resolving to true if setup should proceed, false otherwise
*/
async handleExistingSetup() {
try {
const existingGitignore = await this.FILE_SYSTEM_SERVICE.isOneOfPathsExists([".gitignore"]);
if (!existingGitignore) {
return true;
}
const shouldReplace = await this.CLI_INTERFACE_SERVICE.confirm(`An existing .gitignore file was found (${existingGitignore}). Would you like to replace it?`);
if (!shouldReplace) {
this.CLI_INTERFACE_SERVICE.warn("Keeping existing .gitignore file.");
return false;
}
try {
await this.FILE_SYSTEM_SERVICE.deleteFile(existingGitignore);
this.CLI_INTERFACE_SERVICE.success("Deleted existing .gitignore file.");
return true;
}
catch (error) {
this.CLI_INTERFACE_SERVICE.handleError("Failed to delete existing .gitignore file", error);
return false;
}
}
catch (error) {
this.CLI_INTERFACE_SERVICE.handleError("Failed to check existing .gitignore setup", error);
return false;
}
}
/**
* Installs and configures .gitignore.
* Generates a new .gitignore file with common patterns.
* @returns Promise resolving to the module setup result
*/
async install() {
try {
if (!(await this.shouldInstall())) {
return { wasInstalled: false };
}
if (!(await this.handleExistingSetup())) {
return { wasInstalled: false };
}
const setupResult = await this.generateNewGitignore();
this.displaySetupSummary(setupResult.isSuccess, setupResult.error);
return { wasInstalled: true };
}
catch (error) {
this.CLI_INTERFACE_SERVICE.handleError("Failed to complete .gitignore installation", error);
throw error;
}
}
/**
* Determines if .gitignore should be installed.
* Asks the user if they want to generate a .gitignore file.
* Uses the saved config value as default if it exists.
* @returns Promise resolving to true if the module should be installed, false otherwise
*/
async shouldInstall() {
try {
return await this.CLI_INTERFACE_SERVICE.confirm("Do you want to generate .gitignore file for your project?", await this.CONFIG_SERVICE.isModuleEnabled(EModule.GITIGNORE));
}
catch (error) {
this.CLI_INTERFACE_SERVICE.handleError("Failed to get user confirmation", error);
return false;
}
}
/**
* Displays a summary of the setup results.
* Lists what was included in the generated .gitignore file.
* @param isSuccess - Whether the setup was successful
* @param error - Optional error if setup failed
*/
displaySetupSummary(isSuccess, error) {
const summary = [];
if (isSuccess) {
summary.push("Successfully created configuration:", "✓ .gitignore file");
}
else {
summary.push("Failed configuration:", `✗ .gitignore - ${error?.message ?? "Unknown error"}`);
}
summary.push("", "The .gitignore configuration includes:", "- Build outputs and dependencies", "- Common IDEs and editors", "- Testing and coverage files", "- Environment and local config files", "- System and temporary files", "- Framework-specific files", "- Lock files", "", "You can customize it further by editing .gitignore");
this.CLI_INTERFACE_SERVICE.note("Gitignore Setup Summary", summary.join("\n"));
}
/**
* Generates a new .gitignore file.
* @returns Promise resolving to an object indicating success or failure with optional error
*/
async generateNewGitignore() {
this.CLI_INTERFACE_SERVICE.startSpinner("Generating .gitignore file...");
try {
await this.FILE_SYSTEM_SERVICE.writeFile(".gitignore", GITIGNORE_CONFIG);
this.CLI_INTERFACE_SERVICE.stopSpinner(".gitignore file generated");
return { isSuccess: true };
}
catch (error) {
this.CLI_INTERFACE_SERVICE.stopSpinner();
return { error: error, isSuccess: false };
}
}
}
export { GitignoreModuleService };
//# sourceMappingURL=gitignore-module.service.js.map