1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| #!/usr/bin/env node
const AnphTool = (() => { let instance; init = () => { const fs = require('fs'), TAG_RMV = '<REMOVE ME AT BEGIN FB1D2631C12FE8F7EE8951663A8A1081>', NOT_FOUND = 'NOT_FOUND', DENIED = 'DENIED', EXISTED = 'EXISTED', CONTROLLER = 'controller', MODEL = 'model', CLIENTJS = 'clientJS', CSS = 'css', V_FOLDER = 'viewfolder', VIEW = 'view', ROUTER = 'router', _getModuleInfo = mdlName => { return { [CONTROLLER]: `./server/controllers/${mdlName}.js`, [MODEL]: `./server/model/${mdlName}Model.js`, [CLIENTJS]: `./client/js/dev/modules.${mdlName}.js`, [CSS]: `./client/css/dev/${mdlName}.css`, [V_FOLDER]: `./views/${mdlName}`, [VIEW]: `./views/${mdlName}/${mdlName}_view.ejs`, [ROUTER]: `./server/router/r${mdlName}.js` } }, _getMessage = (tag, path) => tag === NOT_FOUND ? `FILE NOT FOUND: ${path}` : tag === DENIED ? `ACCESS DENIED: ${path}` : tag === EXISTED ? `OPPS! EXISTED: ${path}` : '', padLeft = num => +num < 10 ? '0' + num : num, toCapilalize = lower => lower.charAt(0).toUpperCase() + lower.substr(1), _getCreatedDate = () => { let date = new Date() date = new Date(date.valueOf() + date.getTimezoneOffset() * 60000); return `${padLeft(date.getDate())}/${padLeft(date.getMonth() + 1)}/${date.getFullYear()} ${padLeft(date.getHours() + 7)}:${padLeft(date.getMinutes())}:${padLeft(date.getSeconds())}` }, _getModuleDescription = (mdlName, particularName) => [CSS, VIEW].indexOf(particularName) > -1 ? TAG_RMV : `${TAG_RMV} /* * Author: ${require('os').userInfo().username.toLocaleUpperCase()} * Create: ${_getCreatedDate()} * ${mdlName.toUpperCase()} ${particularName.toUpperCase()} */`, _sampleCode = mdlName => { return { [CONTROLLER]: `const ${toCapilalize(mdlName)}Model = require('../model/${mdlName}Model') module.exports = class ${toCapilalize(mdlName)} extends PIEPME { constructor() { super() this.affModel = new ${ toCapilalize(mdlName)}Model() } _${ mdlName} (req, res){ } }`, [MODEL]: `module.exports = class ${toCapilalize(mdlName)}Model { constructor() { this.webServ = new Service() } }`, [CLIENTJS]: `'use strict'; window.onload = function () {//call method public here //${mdlName}Plugin.getInstance().fn(); } var ${ mdlName}Plugin = (function () { var instance; function init() { function eventListener() { } return {//what to public } } return { getInstance: function () { if (!instance) //make sure only 1 object created instance = init(); return instance; } } })();`, [CSS]: '', [VIEW]: `<% - include('../layout/header', { css: ['/css/${mdlName}.css'] }); %>
<% - include('../layout/footer'); %> <script src="/js/modules.${mdlName}.js"></script>`, router: `const ${toCapilalize(mdlName)} = require('../controllers/voucher') module.exports = function (app) { this.ctrl${ toCapilalize(mdlName)} = new ${toCapilalize(mdlName)} () app.get('/${mdlName}', (req, res) => this.ctrl${toCapilalize(mdlName)}._${mdlName}(req, res)) }` } }, _checkTagRmv = path => !fs.existsSync(path) ? NOT_FOUND : fs.readFileSync(path).toString('utf-8').split("\n")[0].trim() === TAG_RMV ? true : DENIED, _generateFile = (mdlName, path, keySample, fpath) => { !!fpath && !fs.existsSync(fpath) && fs.mkdirSync(fpath)
if (!fs.existsSync(path)) { fs.writeFileSync(path, `${_getModuleDescription(mdlName, keySample)} ${_sampleCode(mdlName)[keySample]}`); return path; } else return _getMessage(EXISTED, path); }, _destroyFromPath = (xpath, folder) => { const canRemove = _checkTagRmv(xpath); if (+canRemove) { fs.unlinkSync(xpath); !!folder && fs.rmdirSync(folder) return xpath; } else return _getMessage(canRemove, xpath); }, _makeModule = mdlName => { const path = _getModuleInfo(mdlName); return [ _generateFile(mdlName, path[CONTROLLER], CONTROLLER), _generateFile(mdlName, path[MODEL], MODEL), _generateFile(mdlName, path[CLIENTJS], CLIENTJS), _generateFile(mdlName, path[CSS], CSS), _generateFile(mdlName, path[ROUTER], ROUTER), _generateFile(mdlName, path[VIEW], VIEW, path[V_FOLDER]) ]; }, _destroyModule = mdlName => { const path = _getModuleInfo(mdlName); return [ _destroyFromPath(path[CONTROLLER]), _destroyFromPath(path[MODEL]), _destroyFromPath(path[CLIENTJS]), _destroyFromPath(path[CSS]), _destroyFromPath(path[ROUTER]), _destroyFromPath(path[VIEW], path[V_FOLDER]) ]; }; return { _makeModule, _destroyModule }; } return { getInstance: () => { if (!instance) instance = init(); return instance; } } })();
|