All files / src/serverless command.js

100% Statements 13/13
100% Branches 4/4
100% Functions 6/6
100% Lines 13/13
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    1x 1x       1x 1x       5x   5x 1x     4x   4x               4x       1x       2x       1x  
'use strict';
 
const sh = require('shelljs');
const Errors = require('../common/errors');
 
class ServerlessCommand {
    constructor(path, flags) {
        this.path = path;
        this.flags = flags;
    }
 
    __getSls() {
        const sls = sh.which('sls');
 
        if (!sls) {
            throw new Errors.ServerlessExecutableNotFoundError();
        }
 
        return {
            exec: (command, ...args) =>
                sh.exec(`cd ${this.path} && sls ${command}`, {
                    async: true,
                    silent: true
                })
        };
    }
 
    exec(command) {
        return this.__getSls().exec(`${command}`);
    }
 
    deploy() {
        return this.exec(`deploy ${this.flags}`);
    }
 
    rollback(version) {
        return this.exec(`rollback ${version ? `-t ${version}` : ''}`);
    }
}
 
module.exports = ServerlessCommand;