Grunt任务为新模块自动生成文件

时间:2016-05-23 06:51:41

标签: javascript node.js module automation gruntjs

输入:grunt generateModule sample-module

输出

应该生成

文件,如:

  1. module / sample-module / sample-module.hbss
  2. module / sample-module / sample-module.json
  3. module / sample-module / sample-module.scss
  4. 模块/样品模块/样品module.js
  5. 除此之外,如果我们可以使用某些通用语法创建Js文件,例如

    /*jslint forin: true, sloppy: true, unparam: true, vars: true, white: true, nomen: true, plusplus:true */
    /*global window, document, jQuery, console */
    
    /*
     * sample-module.js
     * [ Description of the sample module script ]
     *
     * @project:    project
     * @date:       xxxx-xx-xx
     * @author:     Anuj Sachan, asachan@sapient.com
     * @namespaces: sunSpr
     */
    
    var sunSpr = window.sunSpr || {};
    
    /**
     * @namespace SampleModule
     * @memberof sunSpr
     * @property {null} property - description of property
     */
    sunSpr.SampleModule = (function (window, $, namespace) {
        'use strict';
    
        // public methods
        var init,
    
        // private methods
            _privateMethod,
    
        // properties
            property = null;
    
        /**
         * @method _privateMethod
         * @description Description of _privateMethod
         * @memberof sunSpr.SampleModule
         */
        _privateMethod = function () {
            return property;
        };
    
        /**
         * @method init
         * @description Description of init
         * @memberof sunSpr.SampleModule
         * @example
         * sunSpr.SampleModule.init()
         */
        init = function () {
            return _privateMethod();
        };
    
        // Public API
        return {
            init: init
        };
    
    }(this, jQuery, 'sunSpr'));
    
    jQuery(sunSpr.SampleModule.init());
    

1 个答案:

答案 0 :(得分:0)

/*
 * Automatic Module Creator
 *
 * @project:    
 * @date:       xxxx-xx-xx
 * @author:     Anuj Sachan,
 * @licensor:   
 * @namespaces: 
 */


var fs = require('fs'),
    path = require('path'); 
module.exports = function(grunt){
    'use strict';

    grunt.registerTask('module',function(){
        var moduleName=grunt.option('moduleName'),
            filePath="app/modules/",
            PagesPath="app/pages/",
            hbsContent='<section id="'+moduleName+'" class="">\n</section>';

        /*
         * write content to  Javscript Files default template
         */

        var jsFileGlobalDocInfo="/*\n* sample-module.js\n* [ Description of the sample module script ]\n*\n* @project:    SunSpr\n* @date:       xxxx-xx-xx\n* @author:     name, name@sapient.com\n* @licensor:   SAPIENNITRO\n* @namespaces: SunSpr\n*/",
            jsModuleDeclaration="var SunSpr = window.SunSpr || {};",
            jsClassDocInfo="/**\n* @namespace SampleModule\n* @memberof SunSpr\n* @property {null} property - description of property\n*/",
            jsModuleNameClass="SunSpr."+moduleName.replace("sunSpr-","").replace(/-|\s/g,"")+" = (function (window, $, namespace) {",
            jsMethodBody="'use strict';",
            jsMethodVars="// public methods\n\t var init,\n\t// private methods\n\t_privateMethod,\n\t// properties\n\tproperties=null;",
            jsMethodFunctionDeclarations="init =function () {\n\t};\n\n\t// Public API\n\treturn {\n\t init: init\n\t};\n}\n(this, jQuery, 'SunSpr'));\n\njQuery(sunSpr."+moduleName.replace("sunSpr-","").replace(/-|\s/g,"")+".init());",
            jsFileContent=jsFileGlobalDocInfo+'\n\n'+jsModuleDeclaration+'\n\n'+jsClassDocInfo+'\n'+jsModuleNameClass+'\n\t'+jsMethodBody+'\n\t'+jsMethodVars+'\n\n\t'+jsMethodFunctionDeclarations;

        /*
         * write content to  HBSS  Files Page
         */

        var modulePageMeta='---\n{\n\t"title"\t: "SunSpr",\n\t"site"\t: {\n\t"title" :'+ '"'+moduleName.substr(0,1).toUpperCase()+moduleName.substr(1,moduleName.length)+'"\n\t}\n}\n---',
            modulePageBody="{{>header}}\n<main>\n<div class='sunspr-wrap-sections'>\n\t{{>"+moduleName+"}}\n</div>\n</main>\n{{>footer}}",
            modulePageContent=modulePageMeta+"\n\n"+modulePageBody;


        if(!moduleName){
            throw new Error("Please provide a name for the module!");
        }

         /*
         * Create module & Pages HBSS
         */

        grunt.file.write(filePath+moduleName+"/"+moduleName+'.hbs',hbsContent);
        grunt.file.write(PagesPath+moduleName.replace("sunSpr-","").replace(/-|\s/g,"")+'.hbs',modulePageContent);

        /*
         * Creatring Module Files
         */

        grunt.file.write(filePath+moduleName+"/js/"+moduleName+'.js',jsFileContent);
        grunt.file.write(filePath+moduleName+"/data/"+moduleName+'.json',"{\n}");
        grunt.file.write(filePath+moduleName+"/sass/"+moduleName+'.scss',"");

    });
}