2016-05-23 66 views
1

輸入:咕嚕generateModule採樣模塊咕嚕任務要自動生成文件的新模塊

輸出

文件應該像產生:

  1. 模塊/sample-module/sample-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, [email protected] 
* @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()); 

回答

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, [email protected]\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',""); 

    }); 
}