All files / src/serverless/deploy-multiple index.js

100% Statements 53/53
100% Branches 20/20
100% Functions 15/15
100% Lines 52/52
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157    1x 1x 1x 1x 1x 1x 1x 1x   1x                             36x   1x 26x 17x 9x 7x   2x       1x 11x 11x 27x 26x 26x   26x         11x     1x 11x   11x   27x     26x                 7x         7x     19x   19x         2x   17x 17x     19x     19x 19x                               11x   5x 5x   5x 2x 2x   2x     2x                                         1x   2x     3x     6x 6x      
'use strict';
 
const logUpdate = require('log-update');
const chalk = require('chalk');
const lastIndexOf = require('lodash/lastIndexOf');
const Listr = require('listr');
const ListrVerboseRenderer = require('listr-verbose-renderer');
const deployOne = require('./deploy-one');
const rollback = require('./rollback');
const createStatusStream = require('./status-stream');
 
const colors = [
    'green',
    'yellow',
    'blue',
    'magenta',
    'cyan',
    'white',
    'gray',
    'greenBright',
    'yellowBright',
    'blueBright',
    'magentaBright',
    'cyanBright'
];
 
const getColor = index => colors[index % colors.length];
 
const getStatus = deploySummary => {
    if (deploySummary.isSkipped) {
        return chalk.yellow('[skipped]');
    } else if (deploySummary.isFailed) {
        return chalk.red('[failed]');
    } else {
        return chalk.green('[completed]');
    }
};
 
const showSummary = (paths, ctx) => {
    let output = '';
    paths.forEach((path, index) => {
        if (ctx[path]) {
            const color = getColor(index);
            const { info, isSkipped, isFailed } = ctx[path];
 
            output += `${chalk[color](`[${path}]`)} ${getStatus(
                ctx[path]
            )}:\n${info}`;
        }
    });
    logUpdate(output);
};
 
module.exports = (paths, flags, config, logStream) => {
    const deployedPaths = [];
 
    const tasks = new Listr(
        paths.map((path, index) => {
            return {
                title: path,
                task: (ctx, task) => {
                    return deployOne({
                        path,
                        flags,
                        logStream,
                        stdout:
                            config.verbose &&
                            createStatusStream(path, getColor(index), task)
                    })
                        .catch(err => {
                            ctx[path] = {
                                info: err.log,
                                isFailed: true
                            };
 
                            return Promise.reject(err);
                        })
                        .then(output => {
                            let isSkipped = false;
                            // check if service was deployed
                            if (
                                output.indexOf(
                                    'Serverless: Stack update finished...'
                                ) > -1
                            ) {
                                deployedPaths.push(path);
                            } else {
                                isSkipped = true;
                                task.skip();
                            }
 
                            const infoIndex = output.lastIndexOf(
                                'Service Information'
                            );
                            const info = output.substring(infoIndex);
                            ctx[path] = {
                                info,
                                isSkipped
                            };
                        });
                }
            };
        }),
        {
            concurrent: !config.runInBand,
            exitOnError: Boolean(config.exitOnFailure),
            renderer: config.verbose && ListrVerboseRenderer,
            collapse: false
        }
    ).run();
 
    return tasks
        .catch(err => {
            console.log('Deployment failed');
            showSummary(paths, err.context);
 
            if (config.rollbackOnFailure && deployedPaths.length > 0) {
                console.log('Rolling back');
                return new Listr(
                    deployedPaths.map((path, index) => {
                        return {
                            title: `rolling back ${path}`,
                            task: (ctx, task) =>
                                rollback({
                                    path,
                                    logStream,
                                    stdout:
                                        config.verbose &&
                                        createStatusStream(
                                            path,
                                            getColor(index),
                                            task
                                        )
                                })
                        };
                    }),
                    {
                        concurrent: !config.runInBand,
                        exitOnError: false,
                        renderer: config.verbose && ListrVerboseRenderer
                    }
                )
                    .run()
                    .catch(err => {
                        console.error(err);
                    })
                    .then(() => Promise.reject(err));
            }
 
            return Promise.reject(err);
        })
        .then(ctx => {
            console.log('\nDeployment completed successfuly\n');
            showSummary(paths, ctx);
        });
};