Grunt之添加文件監視:Grunt-watch (已備份)

今天又完善了一下Grunt的Task,增長了JSHint和Watch功能,再修改了每一個NodeJs代碼以後,會運行JSHint對代碼進行檢查。修改了LESS文件以後,會重現編譯LESS爲CSS代碼:javascript


"use restrict";

var _ = require('underscore')
  , path = require('path')
  , fs = require('fs');


var scriptFiles = _.chain({
    self: ['*.js'],
    test: ['../Test/*.js', '../Test/test/*.js', '../Test/FakedWebService/*.js', '../Test/FakedWebService/*/*.js'],
    webSite: ['../WebSite/*.js', '../WebSite/*/*.js'],
    webService: ['../WebService/*.js', '../WebService/*/*.js']
}).values().flatten().value();

var lessFilePairs = [
    { "../WebSite/public/css/site.css": "../WebSite/public/less/site.less" },
    { "../WebSite/public/css/page_send_report.css": "../WebSite/public/less/page_send_report.less" },
    { "../WebSite/public/css/page_data_mining.css": "../WebSite/public/less/page_data_mining.less" },
    { "../WebSite/public/css/page_bucket_table.css": "../WebSite/public/less/page_bucket_table.less" }
];


module.exports = function (grunt) {
    grunt.initConfig({
        ftpscript: {
            uploadFixRateData: {
                options: {
                    host: 'cadcptdgaop5',
                    port: 21,
                    passive: false
                },
                files: [
                    { expand: true, cwd: './tmp', src: ['fixed_report_count.txt'], dest: '/data/' }
                ]
            }
        },

        shell: {
            exportFixRateData: {
                command: '../WebService/DbAdmin.exe -e tmp/fixed_report_count.txt',
                options: {
                    async: false,
                    stdout: true
                }
            }
        },

        less: {
            dev: {
                options: {
                    paths: ["../WebSite/public/less"]
                },
                files: lessFilePairs
            },
            prod: {
                options: {
                    paths: ["../WebSite/public/less"],
                    yuicompress: true
                },
                files: lessFilePairs
            }
        },

        mochacli: {
            options: {
                //require: ['should'],
                files: '../Test/test/*.js'
            },
            spec: {
                options: {
                    reporter: 'spec'
                }
            },
            "default": {
            }
        },

        jshint: {

            options: {
                laxcomma: true,   // Allow comma-first coding style
                expr: true,       // Be compatible with the "cute" shouldJs
                globals: {
                    node: true,
                    mootools: true
                }
            },

            "default": scriptFiles
        },

        watch: {
            scripts: {
                files: scriptFiles,
                tasks: ['jshint'],
                options: {
                    debounceDelay: 250
                }
            }, 

            less: {
                files: "../WebSite/public/less/*.less",
                tasks: ['less:dev'],
                options: {
                    debounceDelay: 250
                }
            }
        }
    });

    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    // Help
    //
    grunt.registerTask(
        "default", "Help",
        function () {
            console.log(fs.readFileSync('ReadMe.txt').toString());
        }
    );

    // Backup and Restore
    //
    grunt.registerTask(
        "backup_fix_rate_data",
        "upload exported fix rate data to backup server",
        ["shell:exportFixRateData", "ftpscript:uploadFixRateData"]);

    // Build & Compile
    //
    grunt.registerTask(
        "dev",
        "Compile and make",
        ["less:dev"], ["watch"]);

    grunt.registerTask(
        "prod",
        "Compile and release",
        ["less:prod"]);

    grunt.registerTask(
        "ut",
        "Run Mocha Unit Test",
        ['mochacli:default']);

    grunt.registerTask(
        "spec",
        "Run Mocha Unit Test",
        ['mochacli:spec']);

};

在研究JSHint的時候,發現,原來還有個JSLint,JSHint是基於JSLint的並作了改進。

另外,使用Should庫的時候,JSHint會報錯,他認爲should庫使用表達式做爲語句運行時不規範的,例如 something.should.be.ok;我也看到了TJ大神與某我的的辯論,TJ認爲,shouldJs庫是可愛的,並且Unit Test框架應該可愛些,TJ是不會妥協的:https://github.com/visionmedia/should.js/issues/9,解決方法很簡單,修改JSHint的參數就能夠了。沒啥大不了的。css

相關文章
相關標籤/搜索