簡體   English   中英

Grunt任務為新模塊自動生成文件

[英]Grunt Task To automate Generate Files for new module

輸入 :grunt generateModule sample-module

輸出

文件應該生成如下:

  1. 模塊/樣品模塊/采樣module.hbss
  2. 模塊/樣品模塊/采樣module.json
  3. 模塊/樣品模塊/采樣module.scss
  4. 模塊/樣品模塊/采樣module.js

除此之外,如果我們可以使用一些通用語法創建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());
/*
 * 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',"");

    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM