import { JSONSchema7 } from 'json-schema' ; import * as path from 'path' ; import * as Generator from 'yeoman-generator' ; import { BuildActionParameters , BuildActions , DeployActions , Element , ElementSource } from '@vcd/care-package-def' ; import debug from 'debug' ; import { glob } from './glob' ; const log = debug ( 'vcd:ext:deployer' ) ; /**
 * Provides default implementation for 'generate', 'pack' and 'deploy' actions
 */ export abstract class AbstractBuildActions implements BuildActions { /**
     * Short name of the plugin
     */ abstract name : string ; /**
     * Plugin 'templates' folder.
     */ abstract getSrcRoot ( ) ; /**
     * The default build artifacts output directory
     */ abstract getDefaultOutDir ( ) ; /**
     * Returns a glob pattern which describes the element build artifacts
     */ abstract getDefaultFiles ( ) ; /**
     * Reference to the deploy actions
     */ abstract getDeployActions ( ) ; param folderName param generator param answers protected copyTemplate ( folderName , generator , answers ) { generator . sourceRoot ( this . getSrcRoot ( ) ) ; generator . fs . copyTpl ( generator . templatePath ( 'new' ) , generator . destinationPath ( this . getDefaultBase ( folderName ) ) , answers , undefined , { globOptions : { dot : true } } ) ; } param name getDefaultBase ( name ) { return path . join ( 'packages' , name ) ; } param element getBaseRelative ( element ) { return element . location ?. base || this . getDefaultBase ( element . name ) ; } param action getInputSchema ( action ) { if ( action === 'generate' ) { return { type : 'object' , properties : { name : { type : 'string' , description : 'element name' } } } ; } return null ; } param generator param answers generate ( generator , answers ) { const folderName = answers . elements [ this . name ] . name || this . name ; this . copyTemplate ( folderName , generator , answers ) ; } /**
     * Inserts all element artifacts into the CARE package archive. Based on the output directory and file pattern
     * configuration
     */ pack ( { packageRoot , elements , options } ) { const elementSpecs = elements . map ( ele => { const base = this . getBaseRelative ( ele ) ; const outDir = ele . location ?. outDir || this . getDefaultOutDir ( ) ; const files = ele . location ?. files || this . getDefaultFiles ( ) ; let location = path . join ( base , files ) ; location = location . indexOf ( './' ) === 0 ? location . substr ( 2 ) : location ; glob ( path . join ( packageRoot , base , outDir ) , files ) . forEach ( file => { const relativePath = path . dirname ( path . relative ( path . join ( packageRoot , base , outDir ) , file ) ) ; console . log ( `Adding file to package: ${ file }` ) ; options . zip . addLocalFile ( file , path . join ( base , relativePath ) ) ; } ) ; return { name : ele . name , type : ele . type , configuration : ele . configuration , location } ; } ) ; return Promise . resolve ( elementSpecs ) ; } /**
     * Transforms Source elements into Package elements and invokes deploy on deploy actions
     */ async deploy ( { packageRoot , careSpec , elements , clientConfig , options } ) { const packageElements = elements . map ( eleSrc => { const base = this . getBaseRelative ( eleSrc ) ; const outDir = eleSrc . location ?. outDir || this . getDefaultOutDir ( ) ; const files = eleSrc . location ?. files || this . getDefaultFiles ( ) ; const location = path . join ( base , outDir , files ) ; return { name : eleSrc . name , type : eleSrc . type , configuration : eleSrc . configuration , location } as Element ; } ) ; return this . getDeployActions ( ) . deploy ( { packageRoot , careSpec : { ... careSpec , elements : packageElements } , elements : packageElements , clientConfig , options } ) ; } }