auth/AuthUnit.js

/**
 * @module flitter-auth/AuthUnit
 */

const Unit = require('libflitter/Unit')
const ncp = require('ncp')
const path = require('path')

/**
 * Registers functionality provided by flitter-auth.
 * @extends module:libflitter/Unit~Unit
 */
class AuthUnit extends Unit {

    /**
     * Get the name of this unit.
     * @returns {string} "auth"
     */
    name(){
        return "auth"
    }

    /**
     * Deploy the resources provided by flitter-auth. That is, the controllers/views/routes/models/middleware/etc.
     * @returns {Promise<void>}
     */
    async deploy() {
        const package_dir = __dirname
        const base_dir = path.dirname(require.main.filename)
        _flitter.log("Auth deploy from: " + package_dir, 2)
        _flitter.log("To: " + path.dirname(require.main.filename), 2)

        function do_copy(from, to) {
            return new Promise(
                (resolve, reject) => {
                    ncp(from, to, (error) => {
                        if (error) reject(error)

                        resolve()
                    })
                }
            )
        }


        // controllers
        await do_copy(path.resolve(package_dir + '/deploy/controllers'), _flitter.directories.controllers)

        // models
        await do_copy(path.resolve(package_dir + '/deploy/models'), _flitter.directories.models)

        // routing - middleware
        await do_copy(path.resolve(package_dir + "/deploy/routing/middleware"), path.resolve(_flitter.directories.middleware, 'auth'))

        // routing - routers
        await do_copy(path.resolve(package_dir + "/deploy/routing/routers"), _flitter.directories.routers)

        // views
        await do_copy(path.resolve(package_dir + "/deploy/views/auth"), path.resolve(_flitter.directories.views, 'auth'))

        // views - errors
        await do_copy(path.resolve(package_dir + '/deploy/views/errors'), path.resolve(_flitter.directories.views, 'errors'))

    }
}

module.exports = exports = AuthUnit