Code coverage report for tasks/utils/config-helper.js

Statements: 52.58% (51 / 97)      Branches: 57.47% (50 / 87)      Functions: 35% (7 / 20)      Lines: 53.26% (49 / 92)      Ignored: none     

All files » tasks/utils/ » config-helper.js
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 158 159 160 161 162 163 164 165 166 167 168 169 170 1711 1 1 1 1 1   1   13 31                                                                                                                                       4 4 4 4         4   8 8 8 8 8 8               4 4     4     6 6     6     6     6 1     6 1   6     6           6       6 1     6 1   6 1         6 1 5         6 5 5   1       1       1
var findup = require('findup-sync');
var path = require('path');
var extend = require('util')._extend;
var fs = require('../utils/fs');
var log = require('../utils/log');
var config;
 
var helper = {
    matches: function matches(config, plugins){
        return config && config.map(function(i){
            if (plugins.indexOf(i)>-1) return i;
        }).join('');
    },
    getConfig : function(){
        if (config) return config;
        var configPath = findup('caddy.config.js');
        config = (configPath) ? require(configPath) : false;
        if (config){
            config.appRoot = configPath.replace('caddy.config.js','');
            this.createBuildPaths(config);
            this.createGlobs(config);
        }
        return config;
    },
    createGlobs : function(config) {
        config.globs = {
            'html':    '/{.,*}/!(_)*.{html,jade,ms,mustache}',
            'styles':  '/{.,*}/!(_)*.{css,scss,sass}',
            'scripts': '/{.,*}/!(_)*.js'
        };
    },
    createBuildPaths : function(config) {
        if (config.buildPaths || !config.paths){ return; }
        config.buildPaths = [];
        config.paths.source && config.buildPaths.push({ source: config.paths.source, target:config.paths.target});
        config.paths.demo && config.buildPaths.push({ source: config.paths.demo, target: config.paths.target});
    },
    normaliseCopy : function (globsArr, buildPaths, options){
        var executables = buildPaths.map(function(buildPath){
            //add any other buildPath configs onto options object
            var configOptions = JSON.parse(JSON.stringify(buildPath));
            delete configOptions.target;
            delete configOptions.source;
            options = extend(configOptions || {}, options || {});
            return globsArr.map(function(glob){
                return {
                    source: path.join(buildPath.source, glob),
                    target: buildPath.target,
                    options: options
                };
            });
        });
        executables = executables.reduce(function(a, b) {
            return a.concat(b);
        });
        return executables;
    },
    normaliseClean : function (globsArr, config, options){
        var executables = config.buildPaths.map(function(buildPath){
            //add any other buildPath configs onto options object
            var configOptions = JSON.parse(JSON.stringify(buildPath));
            delete configOptions.target;
            delete configOptions.source;
            options = extend(configOptions || {}, options || {});
            return globsArr.map(function(glob){
                return {
                    source: path.join(buildPath.target, glob),
                    target: buildPath.target,
                    options: options
                };
            });
        });
        executables = executables.reduce(function(a, b) {
            return a.concat(b);
        });
        return executables;
    },
    normaliseBuild : function (subtasks, config, source, target, options){
        Iif (Array.isArray(target)) { log.onError('target must be a string, is currently : ' + target); }
        Eif (!Array.isArray(subtasks)) subtasks = [subtasks];
        var executables;
        Iif (source){  //from node API
            executables = subtasks.map(function(subtask){
                return { subTask: subtask, source: source, target: target, options: options};
            });
        } else {
            executables = config.buildPaths.map(function(buildPath){
                //add any other buildPath configs onto options object
                var configOptions = JSON.parse(JSON.stringify(buildPath));
                delete configOptions.target;
                delete configOptions.source;
                options = extend(configOptions || {}, options || {});
                return subtasks.map(function(subtask){
                    return {
                        subTask: subtask,
                        source: path.join(buildPath.source, config.globs[subtask]),
                        target: buildPath.target,
                        options: options
                    };
                });
            });
            executables = executables.reduce(function(a, b) {
                return a.concat(b);
            });
        }
        return executables;
    },
    configCheck : function(){
        var config = this.getConfig();
        var error = [
            'Your `caddy.config.js` seems to be incorrect.'
        ];
        var warn = [
            'Your `caddy.config.js` seems to be out of date.'
        ];
        Iif (!config){
            log.onError('You must have a caddy.config.js in the root of your project.');
        }
        if (!config.pkg.version){
            error.push(' * The package.json requires as a `version` string (even "version": "0.0.0" is fine)');
        }
        //check old config
        if (!config.tasks){
            error.push(' * Please ensure there is a `tasks` object within your caddy.config.js');
        }
        Iif (!config.buildPaths){
            error.push(' * Please ensure there is a `buildPaths` object within your caddy.config.js');
        }
        config.buildPaths.forEach(function(buildPath){
            if (Array.isArray(buildPath.targets)) {
                error.push(' * Please ensure there is a `buildPaths.targets` singular and a string i.e. `buildPath.target`');
            }
        });
        //check copy config
        Iif (config.tasks && config.tasks.copy && config.tasks.copy.indexOf('server-config')>-1) {
            error.push('Please update caddy.config.js\nReplace \'server-config\' with : \'/*{CNAME,.htaccess,robots.txt,manifest.json}\'');
        }
        //check build config
        if (config.tasks && config.tasks.build && config.tasks.build.indexOf && config.tasks.build.indexOf('requirejs')>=0 && !config.requirejs){
            error.push(' * There is no scripts config object:  `requirejs:{...}`');
        }
        //check test config
        if (config.tasks && config.tasks.test && !config[config.tasks.test]){
            error.push(' * There is no test config object: `' + config.tasks.test + ': {...}`');
        }
        if (config.tasks && config.tasks.test && !Array.isArray(config.karma) && typeof config.karma!=='string'){
            error.push(' * The karma object within caddy.config.js must now be a String or an Array containing the karma config file(s) i.e. \n ' +
                ' karma: [\'./test/karma.unit.js\',\'./test/karma.functional.js\']');
        }
 
        //check release config
        if (config.tasks && config.tasks.release && config.tasks.release.indexOf('s3')>=0 && !config.s3){
            error.push(' * There is no release config object:  `s3:{...}`');
        } else Iif (config.tasks && config.tasks.release && config.tasks.release.indexOf('s3')>=0 && config.s3.profile &&
            (config.s3.secret || config.s3.accessKey)){
            error.push(' * Your s3 config need either `profile` OR `secret/accessKey` not all.');
        }
 
        if (error.length>1){
            log.onError(error.join('\n'));
            return error.join('\n');
        }
        Iif (warn.length>1){
            log.warn(warn.join('\n'));
            return warn.join('\n');
        }
        return true;
    }
};
 
module.exports = helper;