import commander from "commander" ; import { build as buildCmd , exit as exitCmd , populateInheritValueFromConfig , validateForRequiredValues , } from "../../lib/commandBuilder" ; import { hasValue , validateProjectNameThrowable , validateOrgNameThrowable , } from "../../lib/validator" ; import { Bedrock , Config } from "../../config" ; import { logger } from "../../logger" ; import { build as buildError , log as logError } from "../../lib/errorBuilder" ; import { errorStatusCode } from "../../lib/errorStatusCode" ; import decorator from "./append-variable-group.decorator.json" ; import { BedrockFileInfo , BedrockFile } from "../../types" ; import * as bedrockYaml from "../../lib/bedrockYaml" ; import { hasVariableGroup } from "../../lib/pipelines/variableGroup" ; import { AzureDevOpsOpts } from "../../lib/git" ; import { appendVariableGroupToPipelineYaml } from "../../lib/fileutils" ; import { SERVICE_PIPELINE_FILENAME } from "../../lib/constants" ; export interface CommandOptions { orgName : string | undefined ; personalAccessToken : string | undefined ; devopsProject : string | undefined ; } interface ConfigValues { orgName : string ; personalAccessToken : string ; devopsProject : string ; } param opts export const validateValues = ( opts ) : ConfigValues => { populateInheritValueFromConfig ( decorator , Config ( ) , opts ) ; validateForRequiredValues ( decorator , opts , true ) ; validateProjectNameThrowable ( opts . devopsProject || "" ) ; validateOrgNameThrowable ( opts . orgName || "" ) ; return { orgName : opts . orgName || "" , personalAccessToken : opts . personalAccessToken || "" , devopsProject : opts . devopsProject || "" , } ; } ; param projectPath export const checkDependencies = ( projectPath ) : void => { const fileInfo = bedrockYaml . fileInfo ( projectPath ) ; if ( fileInfo . exist === false ) { throw buildError ( errorStatusCode . VALIDATION_ERR , "project-append-variable-group-cmd-err-bedrock-yaml-missing" ) ; } } ; param variableGroupName param values export const variableGroupExists = async ( variableGroupName , values ) : Promise < boolean > => { const accessOpts = { orgName : values . orgName , personalAccessToken : values . personalAccessToken , project : values . devopsProject , } ; return await hasVariableGroup ( accessOpts , variableGroupName ) ; } ; param bedrockFile param variableGroupName export const updateServicesVariableGroups = ( bedrockFile , variableGroupName ) : void => { bedrockFile . services . forEach ( ( service ) => { const path = service . path ; appendVariableGroupToPipelineYaml ( path , SERVICE_PIPELINE_FILENAME , variableGroupName ) ; } ) ; } ; param projectPath param variableGroupName param opts param exitFn export const execute = async ( projectPath , variableGroupName , opts , exitFn ) : Promise < void > => { try { if ( ! hasValue ( variableGroupName ) ) { throw buildError ( errorStatusCode . CMD_EXE_ERR , "project-append-variable-group-cmd-err-variable-group-missing" ) ; } checkDependencies ( projectPath ) ; const values = validateValues ( opts ) ; if ( ! ( await variableGroupExists ( variableGroupName , values ) ) ) { throw buildError ( errorStatusCode . CMD_EXE_ERR , { errorKey : "project-append-variable-group-cmd-err-variable-group-invalid" , values : [ variableGroupName , values . orgName , values . devopsProject ] , } ) ; } const bedrockFile = Bedrock ( projectPath ) ; bedrockYaml . addVariableGroup ( bedrockFile , projectPath , variableGroupName ) ; updateServicesVariableGroups ( bedrockFile , variableGroupName ) ; await exitFn ( 0 ) ; } catch ( err ) { logError ( buildError ( errorStatusCode . CMD_EXE_ERR , "project-append-variable-group-cmd-failed" , err ) ) ; await exitFn ( 1 ) ; } } ; param command export const commandDecorator = ( command ) : void => { buildCmd ( command , decorator ) . action ( async ( variableGroupName , opts ) => { const projectPath = process . cwd ( ) ; await execute ( projectPath , variableGroupName , opts , async ( status ) => { await exitCmd ( logger , process . exit , status ) ; } ) ; } ) ; } ;