|
| 1 | +import chalk from 'chalk'; |
| 2 | +import { Command } from 'commander'; |
| 3 | +import * as fs from 'fs'; |
| 4 | + |
| 5 | +import { Input } from '../commands/command.input'; |
| 6 | +import { buildProject } from '../core/project/builder/project.builder'; |
| 7 | +import { ProjectRepository } from '../core/project/persistence/repository'; |
| 8 | +import { |
| 9 | + ADD_ACTION_SUCCESS, |
| 10 | + PROJECT_ALREADY_EXISTS, |
| 11 | + PROJECT_DETAILS_HELP, |
| 12 | + RUN_COMMAND_HELP, |
| 13 | +} from '../core/ui/messages'; |
| 14 | +import { AbstractAction } from './abstract.action'; |
| 15 | +import { createInputsFromAlias, getProjectAlias } from './input.handler'; |
| 16 | + |
| 17 | +export class AddAction extends AbstractAction { |
| 18 | + constructor(private repository: ProjectRepository) { |
| 19 | + super(); |
| 20 | + } |
| 21 | + |
| 22 | + public mountInputs(alias: string, command: Command): Input[] { |
| 23 | + const inputs = createInputsFromAlias(alias); |
| 24 | + inputs.push({ name: 'path', value: command.path }); |
| 25 | + |
| 26 | + return inputs; |
| 27 | + } |
| 28 | + |
| 29 | + public async handle(inputs: Input[]) { |
| 30 | + const projectAlias = getProjectAlias(inputs); |
| 31 | + await this._ensureProjectDoesNotExists(projectAlias); |
| 32 | + |
| 33 | + const projectPath = this._getProjectPath(inputs); |
| 34 | + const project = await buildProject(projectAlias, projectPath); |
| 35 | + |
| 36 | + await this.repository.create(project); |
| 37 | + |
| 38 | + console.log(ADD_ACTION_SUCCESS(project)); |
| 39 | + console.log(RUN_COMMAND_HELP(project)); |
| 40 | + } |
| 41 | + |
| 42 | + private async _ensureProjectDoesNotExists( |
| 43 | + projectAlias: string, |
| 44 | + ): Promise<void> { |
| 45 | + const project = await this.repository.findOne(projectAlias); |
| 46 | + |
| 47 | + if (!!project) { |
| 48 | + const errorMessage = PROJECT_ALREADY_EXISTS(projectAlias); |
| 49 | + |
| 50 | + console.error(errorMessage); |
| 51 | + console.info(PROJECT_DETAILS_HELP(projectAlias)); |
| 52 | + |
| 53 | + throw new Error(errorMessage); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private _getProjectPath(inputs: Input[]): string { |
| 58 | + const pathInput: Input = inputs.find( |
| 59 | + (input) => input.name === 'path', |
| 60 | + ) as Input; |
| 61 | + |
| 62 | + if (!pathInput) { |
| 63 | + throw new Error('No path found in command input'); |
| 64 | + } |
| 65 | + |
| 66 | + const path = pathInput.value as string; |
| 67 | + |
| 68 | + if (!fs.existsSync(path)) { |
| 69 | + const errorMessage = `\nPath "${path}" not exists or is unacessible\n`; |
| 70 | + |
| 71 | + console.error(chalk.red(errorMessage)); |
| 72 | + |
| 73 | + throw new Error(errorMessage); |
| 74 | + } |
| 75 | + |
| 76 | + if (!fs.lstatSync(path).isDirectory()) { |
| 77 | + const errorMessage = '\nPath must be a directory\n'; |
| 78 | + |
| 79 | + console.error(chalk.red(errorMessage)); |
| 80 | + |
| 81 | + throw new Error(errorMessage); |
| 82 | + } |
| 83 | + |
| 84 | + return path; |
| 85 | + } |
| 86 | +} |
0 commit comments