/* This file has been generated by ox-ui-module generator.
 * Please only apply minor changes (better no changes at all) to this file
 * if you want to be able to run the generator again without much trouble.
 *
 * If you really have to change this file for whatever reason, try to contact
 * the core team and describe your use-case. May be, your changes can be
 * integrated into the templates to be of use for everybody.
 */
'use strict';

module.exports = function (grunt) {

    grunt.registerTask('serve', ['connect:server:keepalive']);

    if (grunt.isPeerDependencyInstalled('appserver')) {
        var appserver = require('appserver');
        var _ = require('underscore');

        grunt.config.merge({
            connect: {
                server: {
                    options: {
                        port: grunt.config('local.appserver.port') || 8337,
                        protocol: grunt.config('local.appserver.protocol') || 'http',
                        base: ['build/'],
                        livereload: grunt.config('local.appserver.livereload') || true,
                        middleware: function (connect, options, middlewares) {
                            var config = grunt.config().local.appserver;
                            if (config.server === '') {
                                grunt.log.error('Server not specified in grunt/local.conf.json');
                                grunt.log.writeln('Hint: If this is a new setup you may want to run `grunt show-config:local --output grunt/local.conf.json` and change its values according to your setup.');
                                grunt.fail.fatal('Please adjust your local.conf.json');
                            }

                            config.prefixes = (config.prefixes || []).concat(options.base);
                            config.manifests = (config.manifests || []).concat(options.base + '/manifests/');

                            config.prefixes = _.uniq(config.prefixes);
                            config.manifests = [].concat.apply(config.manifests, config.prefixes.map(function (prefix) {
                                return prefix + '/manifests/';
                            }));
                            config.manifests = _.uniq(config.manifests);

                            config = appserver.tools.unifyOptions(config);

                            // Todo: Take care of middlewares by connect
                            middlewares = [];

                            middlewares.push(appserver.middleware.appsload(config));
                            middlewares.push(appserver.middleware.manifests(config));
                            middlewares.push(appserver.middleware.login(config));
                            middlewares.push(appserver.middleware.localfiles(config));
                            middlewares.push(appserver.middleware.proxy(config));
                            return middlewares;
                        }
                    }
                }
            }
        });
    }

    grunt.util.registerDummyTask('connect', ['grunt-contrib-connect', 'appserver']);
    if (grunt.isPeerDependencyInstalled('grunt-contrib-connect')) {
        grunt.loadNpmTasks('grunt-contrib-connect');
    }
};

@@ -471,16 +471,320 @@
 grunt) %7B
+%0A    function tryRead(file) %7B%0A        try %7B%0A            //file found, return it%0A            return grunt.file.read(file);%0A        %7D catch (e) %7B%0A            //PEM data injected directly, return it%0A            if (/%5E---/.test(file)) return file;%0A        %7D%0A        //no data found%0A        return null;%0A    %7D
 %0A%0A    gr
@@ -3339,16 +3339,505 @@
     %7D);%0A
+%0A        var key = tryRead(grunt.config('local.appserver.key') %7C%7C 'ssl/host.key');%0A        var cert = tryRead(grunt.config('local.appserver.cert') %7C%7C 'ssl/host.crt');%0A        var ca = tryRead(grunt.config('local.appserver.ca') %7C%7C 'ssl/rootCA.crt');%0A%0A        if (key && cert && ca) %7B%0A            grunt.config.set('connect.server.options.key', key);%0A            grunt.config.set('connect.server.options.cert', cert);%0A            grunt.config.set('connect.server.options.ca', ca);%0A        %7D%0A%0A
     %7D%0A%0A 

support custom https key configuration for connect