All files / src/commands/deploy index.js

100% Statements 39/39
100% Branches 14/14
100% Functions 13/13
100% Lines 39/39
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90    1x 1x 1x 1x 1x 1x 1x 1x       8x 8x 8x 8x 8x       8x 8x 8x         5x                 3x 6x     3x 1x   2x         3x   3x 2x   1x 1x 1x         8x 8x 1x         7x 1x           6x 3x 2x     3x   3x           1x  
'use strict';
 
const Path = require('path');
const isEmpty = require('lodash/isEmpty');
const pick = require('lodash/pick');
const Cli = require('../../cli');
const deployMultiple = require('../../serverless/deploy-multiple');
const Errors = require('../../common/errors');
const discoverServices = require('../../utils/discover-services');
const groupServices = require('../../utils/group-services');
 
class DeployCommand {
    constructor(path, argv, options, logStream) {
        this.argv = argv;
        this.options = options;
        this.cli = new Cli();
        this.basePath = path;
        this.logStream = logStream;
    }
 
    _loadServices() {
        return discoverServices(this.basePath).then(services => {
            this.services = services;
            this.serviceGroups = groupServices(this.services);
        });
    }
 
    _deploy(paths) {
        return deployMultiple(
            typeof paths === 'string' ? this._normalizePath(paths) : paths,
            this.argv,
            this.options,
            this.logStream
        );
    }
 
    _findService(query) {
        const matchServices = Object.keys(this.services).filter(
            service => service.indexOf(query) > -1
        );
 
        if (matchServices.length === 0) {
            return Promise.reject(new Errors.CantFindService(query));
        } else {
            return Promise.resolve(matchServices);
        }
    }
 
    _normalizePath(path) {
        const services = Object.keys(this.services);
 
        if (path === '.') {
            return path in this.services ? [path] : services;
        } else {
            return services
                .filter(servicePath => servicePath.indexOf(path) === 0)
                .map(path => Path.join(this.basePath, path));
        }
    }
 
    exec(service) {
        return this._loadServices().then(() => {
            if (isEmpty(this.services)) {
                return Promise.reject(
                    new Errors.NoServerlessConfigFoundError()
                );
            }
 
            if (service === 'all') {
                return deployMultiple(
                    Object.keys(this.services),
                    this.argv,
                    this.options,
                    this.logStream
                );
            } else if (service) {
                return this._findService(service).then(paths =>
                    this._deploy(paths)
                );
            } else {
                return this.cli
                    .selectService(this.serviceGroups)
                    .then(path => this._deploy(path));
            }
        });
    }
}
 
module.exports = DeployCommand;