All files / src/utils discover-services.js

100% Statements 25/25
100% Branches 10/10
100% Functions 8/8
100% Lines 24/24
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    1x 1x 1x   1x       1x 9x 1x     8x 8x   10x   10x 10x 5x     10x 5x   5x               1x 4x   4x 4x 5x 5x 5x     4x      
'use strict';
 
const Path = require('path');
const fs = require('fs-extra');
const { containsServerlessConfig } = require('../common/utils');
 
const ignorePaths = [
    'node_modules'
];
 
const discover = (basePath, hash) => {
    if (ignorePaths.some(path => basePath.indexOf(path) > -1)) {
        return Promise.resolve();
    }
 
    return fs.readdir(basePath).then(files => {
        return Promise.all(
            files.map(file => {
                const filePath = Path.join(basePath, file);
 
                return fs.lstat(filePath).then(stats => {
                    if (stats.isFile() && containsServerlessConfig(file)) {
                        hash[basePath] = Path.join(basePath, file);
                    }
 
                    if (stats.isDirectory()) {
                        return discover(filePath, hash);
                    } else {
                        return Promise.resolve();
                    }
                });
            })
        );
    });
};
 
module.exports = basePath => {
    const hash = {};
 
    return discover(basePath, hash).then(() => {
        Object.keys(hash).forEach(key => {
            const hashKey = key.replace(basePath, '').replace(/^\//, '') || '.';
            hash[hashKey] = hash[key];
            delete hash[key];
        });
 
        return hash;
    });
};