gruntfile.js 15.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';
module.exports = function(grunt) {
21 22 23
    var buildTime = new Date().getTime(),
        distPath = './dist/',
        libPath = distPath + 'js/libs/',
24
        isDashboardDirectory = grunt.file.isDir('public'),
25
        nodeModulePath = './node_modules/',
26 27 28 29 30 31 32 33 34
        modulesPath = 'public/';
    if (!isDashboardDirectory) {
        modulesPath = '../public/'
    }

    grunt.initConfig({
        watch: {
            js: {
                files: ['public/**/*.js'],
35
                tasks: ['copy:dist']
36 37 38 39 40 41
            },
            html: {
                files: ['public/**/*.html'],
                tasks: ['copy:dist']
            },
            css: {
42
                files: ['public/**/*.scss', 'public/**/*.css'],
43
                tasks: ['copy:dist', 'sass']
44 45 46 47 48 49 50 51 52 53
            },
            image: {
                files: ['public/**/*.{ico,gif,png}'],
                tasks: ['copy:dist']
            }
        },
        connect: {
            server: {
                options: {
                    port: 9999,
54
                    base: distPath,
55 56
                    // change this to '0.0.0.0' to access the server from outside
                    hostname: '0.0.0.0',
57 58 59
                    middleware: function(connect, options, middlewares) {
                        middlewares.unshift(require('grunt-middleware-proxy/lib/Utils').getProxyMiddleware());
                        return middlewares;
60 61 62 63
                    }
                },
                proxies: [{
                    context: '/api', // the context of the data service
64
                    host: '127.0.0.1',
65
                    port: 21000, // the port that the data service is running on
66
                    https: false
67 68 69 70 71 72
                }],
            },
        },
        npmcopy: {
            js: {
                options: {
73 74
                    destPrefix: libPath,
                    srcPrefix: nodeModulePath
75 76
                },
                files: {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
                    // FileName : {"src":"dest"}
                    'jquery.min.js': { 'jquery/dist': 'jquery/js' },
                    'require.js': { 'requirejs': 'requirejs' },
                    'text.js': { 'requirejs-text': 'requirejs-text' },
                    'underscore-min.js': { 'underscore': 'underscore' },
                    'bootstrap.min.js': { 'bootstrap/dist/js': 'bootstrap/js' },
                    'backbone-min.js': { 'backbone': 'backbone' },
                    'backbone.babysitter.min.js': { 'backbone.babysitter/lib': 'backbone-babysitter' },
                    'backbone.marionette.min.js': { 'backbone.marionette/lib': 'backbone-marionette' },
                    'backbone.paginator.min.js': { 'backbone.paginator/lib': 'backbone-paginator' },
                    'backbone.wreqr.min.js': { 'backbone.wreqr/lib': 'backbone-wreqr' },
                    'backgrid.js': { 'backgrid/lib': 'backgrid/js' },
                    'backgrid-filter.min.js': { 'backgrid-filter': 'backgrid-filter/js' },
                    'backgrid-orderable-columns.js': { 'backgrid-orderable-columns': 'backgrid-orderable-columns/js' },
                    'backgrid-paginator.min.js': { 'backgrid-paginator': 'backgrid-paginator/js' },
                    'backgrid-sizeable-columns.js': { 'backgrid-sizeable-columns': 'backgrid-sizeable-columns/js' },
                    'Backgrid.ColumnManager.js': { 'backgrid-columnmanager/src': 'backgrid-columnmanager/js' },
                    'jquery-asBreadcrumbs.min.js': { 'jquery-asBreadcrumbs/dist': 'jquery-asBreadcrumbs/js' },
                    'd3.min.js': { 'd3': 'd3' },
                    'index.js': { 'd3-tip': 'd3/' },
                    'dagre-d3.min.js': { 'dagre-d3/dist': 'dagre-d3' },
                    'select2.full.min.js': { 'select2/dist/js': 'select2' },
                    'backgrid-select-all.min.js': { 'backgrid-select-all': 'backgrid-select-all' },
                    'moment.min.js': { 'moment/min': 'moment/js' },
101
                    'moment-timezone-with-data.min.js': { 'moment-timezone/builds': 'moment-timezone' },
102 103 104 105 106
                    'jquery.placeholder.js': { 'jquery-placeholder': 'jquery-placeholder/js' },
                    'platform.js': { 'platform': 'platform' },
                    'query-builder.standalone.min.js': { 'jQuery-QueryBuilder/dist/js': 'jQueryQueryBuilder/js' },
                    'daterangepicker.js': { 'bootstrap-daterangepicker': 'bootstrap-daterangepicker/js' },
                    'nv.d3.min.js': { 'nvd3/build': 'nvd3' },
107
                    'jquery.sparkline.min.js': { 'jquery-sparkline': 'sparkline' },
108 109
                    'table-dragger.js': { 'table-dragger/dist': 'table-dragger' },
                    'jstree.min.js': { 'jstree/dist': 'jstree' }
110
                }
111

112 113 114
            },
            css: {
                options: {
115 116
                    destPrefix: libPath,
                    srcPrefix: nodeModulePath
117 118
                },
                files: {
119 120 121 122 123 124 125 126 127 128 129 130
                    'bootstrap.min.css': { 'bootstrap/dist/css': 'bootstrap/css' },
                    'glyphicons-halflings-regular.woff2': { 'bootstrap/fonts': 'bootstrap/fonts' },
                    'backgrid.css': { 'backgrid/lib': 'backgrid/css' },
                    'backgrid-filter.min.css': { 'backgrid-filter': 'backgrid-filter/css' },
                    'backgrid-orderable-columns.css': { 'backgrid-orderable-columns': 'backgrid-orderable-columns/css' },
                    'backgrid-paginator.css': { 'backgrid-paginator': 'backgrid-paginator/css' },
                    'backgrid-sizeable-columns.css': { 'backgrid-sizeable-columns': 'backgrid-sizeable-columns/css' },
                    'Backgrid.ColumnManager.css': { 'backgrid-columnmanager/lib': 'backgrid-columnmanager/css' },
                    'asBreadcrumbs.min.css': { 'jquery-asBreadcrumbs/dist/css': 'jquery-asBreadcrumbs/css' },
                    'select2.min.css': { 'select2/dist/css': 'select2/css' },
                    'backgrid-select-all.min.css': { 'backgrid-select-all': 'backgrid-select-all' },
                    'font-awesome.min.css': { 'font-awesome/css': 'font-awesome/css' },
131
                    '*': [{
132 133 134 135 136
                        'expand': true,
                        'dot': true,
                        'cwd': nodeModulePath + 'font-awesome',
                        'src': ['fonts/*.*'],
                        'dest': libPath + 'font-awesome/'
137 138 139 140 141 142 143
                    }, {
                        'expand': true,
                        'dot': true,
                        'cwd': nodeModulePath + 'jstree/dist/themes/',
                        'src': ['**'],
                        'dest': libPath + 'jstree/css/'
                    }],
144 145
                    'query-builder.default.min.css': { 'jQuery-QueryBuilder/dist/css': 'jQueryQueryBuilder/css' },
                    'daterangepicker.css': { 'bootstrap-daterangepicker': 'bootstrap-daterangepicker/css' },
146
                    'nv.d3.min.css': { 'nvd3/build': 'nvd3/css' },
147 148
                }

149 150 151
            },
            license: {
                options: {
152 153
                    destPrefix: libPath,
                    srcPrefix: nodeModulePath
154 155
                },
                files: {
156 157 158 159 160 161 162 163 164 165 166 167 168
                    'LICENSE.txt': [
                        { 'jquery': 'jquery' },
                        { 'jquery-placeholder': 'jquery-placeholder' }
                    ],
                    'LICENSE': [{ 'requirejs-text': 'requirejs-text' },
                        { 'underscore': 'underscore' },
                        { 'bootstrap': 'bootstrap' },
                        { 'backgrid-columnmanager': 'backgrid-columnmanager' },
                        { 'jquery-asBreadcrumbs': 'jquery-asBreadcrumbs' },
                        { 'd3': 'd3' },
                        { 'd3-tip': 'd3/' },
                        { 'dagre-d3': 'dagre-d3' },
                        { 'platform': 'platform/' },
169
                        { 'jQuery-QueryBuilder': 'jQueryQueryBuilder/' },
170
                        { 'moment-timezone': 'moment-timezone' }
171 172 173 174 175 176
                    ],
                    'LICENSE.md': [{ 'backbone.babysitter': 'backbone-babysitter' },
                        { 'backbone.wreqr': 'backbone-wreqr' },
                        { 'nvd3': 'nvd3/' }
                    ],
                    'license.txt': [{ 'backbone.marionette': 'backbone-marionette' }],
177
                    'license': [{ 'table-dragger': 'table-dragger' }],
178 179 180 181 182 183 184 185
                    'LICENSE-MIT': [{ 'backbone.paginator': 'backbone-paginator' },
                        { 'backgrid': 'backgrid' },
                        { 'backgrid-filter': 'backgrid-filter' },
                        { 'backgrid-orderable-columns': 'backgrid-orderable-columns' },
                        { 'backgrid-paginator': 'backgrid-paginator' },
                        { 'backgrid-sizeable-columns': 'backgrid-sizeable-columns' },
                        { 'backgrid-select-all': 'backgrid-select-all' }
                    ]
186
                }
187 188
            }
        },
189 190 191 192 193 194 195 196
        rename: {
            main: {
                files: [
                    { src: [libPath + '/jstree/css/default/style.min.css'], dest: libPath + '/jstree/css/default/default-theme.min.css' },
                    { src: [libPath + '/jstree/css/default-dark/style.min.css'], dest: libPath + '/jstree/css/default-dark/default-dark-theme.min.css' },
                ]
            }
        },
197 198
        sass: {
            dist: {
199
                files: {
200 201
                    'dist/css/style.css': 'public/css/scss/style.scss',
                    'dist/css/login.css': 'public/css/scss/login.scss'
202 203 204
                }
            },
            build: {
205
                files: {
206 207
                    'dist/css/style.css': 'dist/css/scss/style.scss',
                    'dist/css/login.css': 'dist/css/scss/login.scss'
208 209 210
                }
            }
        },
211 212 213 214
        copy: {
            dist: {
                expand: true,
                cwd: modulesPath,
215 216 217 218 219 220 221
                src: ['**', '!**/scss/**'],
                dest: distPath
            },
            build: {
                expand: true,
                cwd: modulesPath,
                src: ['**'],
222 223 224 225 226 227 228 229
                dest: distPath
            }
        },
        clean: {
            build: [distPath, libPath],
            options: {
                force: true
            }
230 231
        },
        uglify: {
232 233 234 235 236 237 238 239 240 241 242 243 244 245
            buildlibs: {
                options: {
                    mangle: true,
                    compress: true,
                    beautify: false
                },
                files: [{
                    expand: true,
                    cwd: 'dist/js',
                    src: ['external_lib/**/*.js', 'libs/**/*.js'],
                    dest: 'dist/js'
                }]
            },
            buildjs: {
246
                options: {
247 248 249
                    mangle: false,
                    compress: true,
                    beautify: true
250
                },
251 252 253
                files: [{
                    expand: true,
                    cwd: 'dist/js',
254
                    src: ['**/*.js', '!libs/**', '!external_lib/**'],
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
                    dest: 'dist/js'
                }]
            }
        },
        cssmin: {
            build: {
                files: [{
                    expand: true,
                    cwd: 'dist/css',
                    src: '*.css',
                    dest: 'dist/css'
                }]
            }
        },
        htmlmin: {
            build: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true
                },
                files: [{
                    expand: true,
                    cwd: 'dist/js/templates',
                    src: '**/*.html',
                    dest: 'dist/js/templates'
                }]
            }
282 283 284 285 286 287 288 289 290
        },
        template: {
            build: {
                options: {
                    data: {
                        'bust': buildTime
                    }
                },
                files: {
291
                    [distPath + 'index.html']: [modulesPath + 'index.html.tpl']
292 293
                }
            }
294 295 296
        }
    });

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    // Dynamically add copy-task using npmcopy
    var npmCopy = grunt.config.get('npmcopy'),
        libFiles = [],
        createPath = function(options) {
            var obj = options.obj,
                fileName = options.fileName,
                pathPrefix = options.pathPrefix;
            if (obj.length) {
                for (var i in obj) {
                    createPath({
                        'obj': obj[i],
                        'libFiles': options.libFiles,
                        'pathPrefix': pathPrefix,
                        'fileName': fileName
                    });
                }
            } else {
314 315 316 317 318 319
                if (fileName == "*") {
                    options.libFiles.push(obj);
                } else {
                    key = Object.keys(obj);
                    options.libFiles.push({ 'src': pathPrefix.srcPrefix + key + "/" + fileName, 'dest': pathPrefix.destPrefix + obj[key] + "/" + fileName });
                }
320 321 322 323 324 325 326
            }
        };

    for (var key in npmCopy) {
        var options = npmCopy[key].options,
            files = npmCopy[key].files;
        for (var fileName in files) {
327 328 329 330 331 332
            createPath({
                'obj': files[fileName],
                'libFiles': libFiles,
                'pathPrefix': options,
                'fileName': fileName
            });
333 334 335 336
        }
    };
    grunt.config.set('copy.libs', { files: libFiles });

337
    grunt.loadNpmTasks('grunt-contrib-connect');
338
    grunt.loadNpmTasks('grunt-middleware-proxy');
339
    grunt.loadNpmTasks('grunt-contrib-watch');
340 341 342
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-htmlmin');
343
    grunt.loadNpmTasks('grunt-template');
344
    grunt.loadNpmTasks('grunt-contrib-rename');
345 346 347 348 349

    require('load-grunt-tasks')(grunt);

    grunt.registerTask('dev', [
        'clean',
350
        'copy:libs',
351
        'copy:dist',
352
        'rename',
353
        'sass:dist',
354
        'template',
355
        'setupProxies:server',
356
        'connect:server',
357
        'watch'
358 359 360
    ]);

    grunt.registerTask('build', [
361
        'clean',
362
        'copy:libs',
363
        'copy:build',
364
        'rename',
365 366
        'sass:build',
        'template'
367 368 369 370
    ]);

    grunt.registerTask('dev-minify', [
        'clean',
371
        'copy:libs',
372
        'copy:dist',
373
        'rename',
374
        'sass:dist',
375 376
        'uglify',
        'cssmin',
377
        'template',
378
        'setupProxies:server',
379 380
        'connect:server',
        'watch'
381 382
    ]);

383 384
    grunt.registerTask('build-minify', [
        'clean',
385
        'copy:libs',
386
        'copy:build',
387
        'rename',
388
        'sass:build',
389 390
        'uglify',
        'cssmin',
391
        'template'
392
    ]);
393
};