|
| 1 | +import commander from "commander"; |
| 2 | +import { fileInfo as bedrockFileInfo } from "../../lib/bedrockYaml"; |
| 3 | +import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder"; |
| 4 | +import { PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE } from "../../lib/constants"; |
| 5 | +import { hasValue } from "../../lib/validator"; |
| 6 | +import { logger } from "../../logger"; |
| 7 | +import { IBedrockFileInfo } from "../../types"; |
| 8 | + |
| 9 | +import decorator from "./create.decorator.json"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Executes the command. |
| 13 | + * |
| 14 | + * @param ringName |
| 15 | + * @param projectPath |
| 16 | + */ |
| 17 | +export const execute = async ( |
| 18 | + ringName: string, |
| 19 | + projectPath: string, |
| 20 | + exitFn: (status: number) => Promise<void> |
| 21 | +) => { |
| 22 | + if (!hasValue(ringName)) { |
| 23 | + await exitFn(1); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + try { |
| 28 | + logger.info(`project path: ${projectPath}`); |
| 29 | + |
| 30 | + checkDependencies(projectPath); |
| 31 | + |
| 32 | + // Check if ring already exists, if it does, warn and exit |
| 33 | + // Add ring to bedrock.yaml |
| 34 | + // Add ring to all linked service build pipelines' branch triggers |
| 35 | + |
| 36 | + logger.info(`Successfully created ring: ${ringName} for this project!`); |
| 37 | + await exitFn(0); |
| 38 | + } catch (err) { |
| 39 | + logger.error(`Error occurred while creating ring: ${ringName}`); |
| 40 | + logger.error(err); |
| 41 | + await exitFn(1); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +export const commandDecorator = (command: commander.Command): void => { |
| 46 | + buildCmd(command, decorator).action(async (ringName: string) => { |
| 47 | + await execute(ringName, process.cwd(), async (status: number) => { |
| 48 | + await exitCmd(logger, process.exit, status); |
| 49 | + }); |
| 50 | + }); |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * Check for bedrock.yaml |
| 55 | + * @param projectPath |
| 56 | + */ |
| 57 | +export const checkDependencies = (projectPath: string) => { |
| 58 | + const fileInfo: IBedrockFileInfo = bedrockFileInfo(projectPath); |
| 59 | + if (fileInfo.exist === false) { |
| 60 | + throw new Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE); |
| 61 | + } |
| 62 | +}; |
0 commit comments