cli/directives/DeployDirective.js

/**
 * @module flitter-cli/directives/DeployDirective
 */

const Directive = require('../Directive')
const PositionalOption = require('../options/PositionalOption')

/**
 * Directive to run one-time deployment functions provided by the units.
 * 
 * @extends module:flitter-cli/Directive~Directive
 */
class DeployDirective extends Directive {

    /**
     * Get the name of the directive.
     * @returns {string} "deploy"
     */
    static name(){
        return "deploy"
    }

    /**
     * Get the usage information for the directive.
     * @returns {string}
     */
    static help(){
        return "run a unit deployment script: <deploy name>"
    }

    /**
     * Get the CLI flag options for this directive.
     * @returns {Array<module:flitter-cli/options/Option~Option>}
     */
    static options() {
        const deployments = this.prototype.cli.loaded_deployments
        const deployment_option = new PositionalOption('deployment name', 'name of the deployment to be run')
        deployment_option.whitelist(...Object.keys(deployments))

        return [deployment_option]
    }

    /**
     * Handle an invocation of this directive. Runs the deployment associated with the specified unit.
     * @param {module:libflitter/app/FlitterApp~FlitterApp} app - the Flitter app
     * @param {Array} argv - the CLI arguments after the directive
     * @returns {Promise<void>}
     */
    async handle(app, argv){
        const deployments = this.cli.loaded_deployments
        let success = false
        let names = "\t"
        
        for ( let deployment_name in deployments ){
            names += deployment_name+", "
            if ( deployment_name === argv[0] ){

                await deployments[deployment_name](app, argv.splice(1))
                this.success(`Deployed ${deployment_name}.`)
                success = true
                break
                
            }
        }
        
        if ( !success ){
            this.error('Please specify a valid deployment name.')
            this.info('The following deployments are registered with Flitter:')
            this.info(`\t${names.slice(0, -2)}`)
        }
    }
    
}

module.exports = exports = DeployDirective