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

标签 javascript node.js module automation gruntjs

输入:gruntgenerateModule示例模块

输出:

文件应生成如下:

  1. 模块/样本模块/样本模块.hbss
  2. 模块/sample-module/sample-module.json
  3. 模块/样本模块/样本模块.scss
  4. 模块/sample-module/sample-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',"");

    });
}

关于javascript - Grunt 任务自动为新模块生成文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37384322/

相关文章:

nginx - Centos7 Nginx mod_security - 未知指令 "ModSecurityEnabled"

javascript - 计算页面重新加载

javascript - 使用javascript更改div的高度

mongodb - 使用 Mongoose 找不到按 ObjectId 搜索的文档

node.js:使用系统调用将文件写入/tmp 目录时遇到问题

javascript - 如何特征检测浏览器是否支持动态 ES6 模块加载?

python - 动态地向现有导入模块添加功能

Javascript:Unicode 符号选择器(用于将数学符号添加到文本区域)

javascript - 清除文档后,document.write(ln) 不起作用?

javascript - 在 Javascript 中通过 WebSocket 的 HashMap