import { Parse , Analysis } from '../../specification' ; import { getServiceConfig } from '../../specification/version' ; import { logger } from '../../utils' ; import { ComponentExeCute , ComponentConfig , generateSynchronizeComponentExeList , synchronizeExecuteComponentList , } from '../component' ; import { emoji , checkTemplateFile } from '../../utils/common' ; import core from '../../utils/core' ; const { colors , ansiEscapes } = core ; import { HandleError } from '../../error' ; import { isEmpty , get , isNil } from 'lodash' ; export class CommandManager { protected deployParams : any ; constructor ( protected templateFile , protected method , protected customerCommandName ? , params ? ) { this . deployParams = params ; } async assemblyProjectConfig ( parse , projectName , parsedObj ) { const realVariables = await parse . getRealVariables ( parsedObj ) ; const projectConfig = getServiceConfig ( realVariables , projectName ) ; projectConfig . appName = realVariables . name ; projectConfig . ProjectName = projectName ; if ( this . deployParams ) { projectConfig . params = this . deployParams ; } return projectConfig ; } async warnEnvironmentVariables ( realVariables ) { const services = realVariables ?. services ; if ( isEmpty ( services ) ) return ; let envObj = { } ; for ( const key in services ) { const environmentVariables = get ( services , [ key , 'props' , 'function' , 'environmentVariables' ] , { } ) ; envObj = Object . assign ( { } , envObj , environmentVariables ) ; } const keys = [ ] ; for ( const key in envObj ) { if ( isNil ( envObj [ key ] ) ) { keys . push ( key ) ; } } keys . length > 0 && logger . warning ( `The value of environment variable [${ keys . join ( ', ' ) }] is undefined.` ) ; } async init ( ) { try { process . stdout . write ( 'Starting ...\n' ) ; const templateFile = checkTemplateFile ( this . templateFile ) ; if ( templateFile ) { const outPutData = { } ; const parse = new Parse ( templateFile ) ; const parsedObj = parse . getOriginalParsedObj ( ) ; const realVariables = await parse . getRealVariables ( parsedObj ) ; await this . warnEnvironmentVariables ( realVariables ) ; const analysis = new Analysis ( realVariables , parse . dependenciesMap ) ; const executeOrderList = analysis . getProjectOrder ( ) ; if ( this . customerCommandName || executeOrderList . length === 1 ) { const tempCustomerCommandName = executeOrderList [ 0 ] ; const projectConfig = await this . assemblyProjectConfig ( parse , this . customerCommandName || tempCustomerCommandName , parsedObj ) ; if ( process . env [ 'serverless_devs_temp_access' ] ) { projectConfig . Access = process . env [ 'serverless_devs_temp_access' ] ; projectConfig . access = process . env [ 'serverless_devs_temp_access' ] ; } const componentExecute = new ComponentExeCute ( { componentConfig : projectConfig , method : this . method , version : parsedObj . edition , customerCommandName : this . customerCommandName , } ) ; process . stdout . write ( ansiEscapes . eraseLines ( 2 ) ) ; const tempResult = await componentExecute . init ( ) ; if ( tempResult ) { outPutData [ projectConfig . ProjectName ] = tempResult ; } } else { const params = this . deployParams || '' ; logger . info ( `It is detected that your project has the following projects < ${ executeOrderList . join ( ',' ) } > to be execute` ) ; const componentList = generateSynchronizeComponentExeList ( { list : executeOrderList , parse , parsedObj , method : this . method , params } , this . assemblyProjectConfig . bind ( this ) ) ; const tempResult = await synchronizeExecuteComponentList ( componentList ) ; for ( const item in tempResult ) { if ( executeOrderList . includes ( item ) && tempResult [ item ] ) { outPutData [ item ] = tempResult [ item ] ; } } } const outResult = JSON . parse ( JSON . stringify ( outPutData ) ) ; Object . keys ( outPutData ) . length === 0 ? logger . success ( `End of method: ${ this . method }` ) : logger . output ( outResult ) ; } else { logger . error ( `Failed to execute:\n
  ${ emoji ( '❌' ) } Message: Cannot find s.yaml / s.yml / template.yaml / template.yml file, please check the directory ${ this . templateFile }
  ${ emoji ( '🧭' ) } If you want to use Serverless Devs, you should have a s.yaml or use [s cli] command.
      ${ emoji ( '1️⃣' ) } Yaml document: ${ colors . underline ( 'https://github.com/Serverless-Devs/docs/blob/master/zh/yaml.md' ) }
      ${ emoji ( '2️⃣' ) } Cli document: [s cli -h]
  ${ emoji ( '😈' ) } If you have questions, please tell us: ${ colors . underline ( 'https://github.com/Serverless-Devs/Serverless-Devs/issues' ) }\n` ) ; process . exit ( 1 ) ; } } catch ( error ) { await HandleError ( { error , prefix : 'Failed to execute:' } ) ; process . exit ( 1 ) ; } } }