cli/Directive.js

/**
 * @module flitter-cli/Directive
 */

const { Injectable } = require('flitter-di')
const ImplementationError = require('libflitter/errors/ImplementationError')

/**
 * Base class for ./flitter directives.
 * @extends module:flitter-di/src/Injectable~Injectable
 */
class Directive extends Injectable {
    /**
     * Defines the services required by this directive.
     * Includes the 'cli' and 'output' services by default.
     * @returns {Array<string>}
     */
    static get services() {
        return [...super.services, 'cli', 'output']
    }

    /**
     * Get an array of the options supported by this directive.
     * @returns {Array<module:flitter-cli/options/Option~Option>}
     */
    static options() {
        return []
    }

    /**
     * Get the name of the directive.
     * This is used by ./flitter to run this directive.
     * @returns {string}
     */
    static name() {
        throw new ImplementationError()
    }

    /**
     * Get the usage information of the directive.
     * This is displayed on the ./flitter help page.
     * @returns {string}
     */
    static help(){
        throw new ImplementationError()
    }

    /**
     * Instantiate the directive
     * @param {object} option_values - mapping of option names to option values
     */
    constructor(option_values = {}) {
        super()

        /**
         * Mapping of option names to option values for the current invocation.
         * @type {Object}
         * @private
         */
        this._options = option_values
    }

    /**
     * Handle an invocation of this directive.
     * @param {module:libflitter/app/FlitterApp~FlitterApp} app - the Flitter app
     * @param {Array<string>} argv - CLI arguments after the directive
     * @returns {Promise<void>}
     */
    async handle(app, argv){
        throw new ImplementationError()
    }

    /**
     * Outputs a [SUCCESS] message to the console.
     * @param {string} message
     */
    success(message) {
        this.output.success(message, 0)
    }

    /**
     * Outputs an [ERROR] message to the console.
     * @param {string} message
     */
    error(message) {
        this.output.error(message, 0)
    }

    /**
     * Outputs a [WARNING] message to the console.
     * @param {string} message
     */
    warning(message) {
        this.output.warn(message, 0)
    }

    /**
     * Outputs an [INFO] message to the console.
     * @param {string} message
     */
    info(message) {
        this.output.info(message, 0)
    }

    /**
     * Gets the value of the option with the specified name for the current invocation.
     * @param {string} name
     * @return {string}
     */
    option(name) {
        return this._options[name]
    }
}

module.exports = exports = Directive