UNPKG

mm_os

Version:

这是超级美眉服务端框架,用于快速构建应用程序。

197 lines (187 loc) 5.01 kB
const compressing = require('compressing'); const Item = require('mm_machine').Item; /** * 指令驱动类 * @extends {Item} * @class */ class Drive extends Item { /** * 构造函数 * @param {String} dir 当前目录 * @constructor */ constructor(dir) { super(dir, __dirname); this.default_file = "./component.json"; // 默认启用热更新 this.mode = 3; /* 通用项 */ // 配置参数 this.config = { // 应用于,根据应用而判断使用 "app": "home", // 名称, 用于动态增删改配置 "name": "demo", // 标题, 用于查询时 "title": "示例组件", // 描述, 用于介绍该组件的作用 "description": "暂无描述", // 执行顺序, 数值越小的越优先显示 "sort": 100, // 分组, 可以将特有的分一个组, 方便用户查询 "group": "default", // 分类, 例如: 查询生活类、监测类、便民类 "type": "默认", // 脚本文件 "func_file": "./component.js", // 渲染 "template": "./component.html", // 状态, 0为未开启 1为开启 "state": 1, // 配置 "options": [], // 定位 由后台指定的展示位置 "location": [] } } }; /** * 创建一个参数模型 */ Drive.prototype.model = function() { return { // 应用于,根据应用而判断使用 "app": "home", // 名称, 用于动态增删改配置 "name": "demo", // 标题, 用于查询时 "title": "示例组件", // 描述, 用于介绍该组件的作用 "description": "暂无描述", // 执行顺序, 数值越小的越优先显示 "sort": 100, // 分组, 可以将特有的分一个组, 方便用户查询 "group": "default", // 分类, 例如: 查询生活类、监测类、便民类 "type": "监测", // 脚本文件, 非必传,可和渲染文件二选一传 "func_file": "./component.js", // 渲染, 非必传,可和脚本文件二选一传 "template": "./component.html", // 状态, 0为未开启 1为开启 "state": 1, // 配置 "options": [], // 定位 由后台指定的展示位置 "location": [{ "path": "/admin/index", "position": "main", "available": 1 }] } }; /** * 新建配置文件 * @param {String} 文件 */ Drive.prototype.new_config = function(file) { var fl = __dirname + "/config.tpl.json"; if (fl.hasFile()) { var text = fl.loadText(); if (text) { var l = $.slash; var arr = file.split(l); arr = arr.slice(arr.indexOf('app')); var app = arr[1]; var name = file.dirname(); text = text.replaceAll('{0}', name); text = text.replaceAll('{1}', app); file.saveText(text); } } }; /** * 新建脚本 * @param {String} file 文件 */ Drive.prototype.new_script = function(file) { var fl = __dirname + "/script.js"; if (fl.hasFile()) { var text = fl.loadText(); if (text) { var l = $.slash; var arr = file.split(l); var name = arr[arr.length - 2]; text = text.replaceAll('{0}', name); file.saveText(text); } } var file_html = "./component.html".fullname(file.dirname()); if (!file_html.hasFile()) { var flh = __dirname + "/component.html"; if (flh.hasFile()) { var html = flh.loadText(); file_html.saveText(html); } } }; /** * 获取配置参数 * @param {Object} 设置配置 * @return {Object} 返回配置参数 */ Drive.prototype.design_option = function(body) { var cg = this.config; if (Array.isArray(body)) { cg.options = body; } else { var options = cg.options || []; var option = options.getObj({ name: body.name }); if (option) { Object.assign(option, body); } else { options.push(body); } cg.options = options; } } /** * 挂件模板 * @param {Object} ctx 请求上下文 * @param {Object} db 数据管理器,如: { next: async function{}, ret: {} } * @return {Object} 执行结果 */ Drive.prototype.tpl = async function(ctx, db) { var html_file = this.config.template || "./component.html"; return html_file.fullname(this.dir).loadText(); }; /** * 组件主函数 * @param {Object} ctx 请求上下文 * @param {Object} db 数据管理器,如: { next: async function{}, ret: {} } * @return {Object} 执行结果 */ Drive.prototype.main = async function(ctx, db) { var model = {}; var html_file = this.config.template || "./component.html"; return db.tpl.view(html_file.fullname(this.dir), model); }; /** * 压缩主题模板 * @param {String} 要压缩的目录 * @returns {String} 打包成功返回压缩包文件地址 */ Drive.prototype.zip = async function(zip_dir = "/static/file/zip/") { var dir = this.filename.dirname(); var file = ("./" + this.config.name + '.zip').fullname(zip_dir); file.addDir(); var done = await compressing.zip.compressDir(dir, file); if (file.hasFile()) { return file; } return null; } module.exports = Drive;