'use strict';

/**
 * 自定义启动文件
 *
 * @author Mai
 * @date 2018/4/18
 * @version
 */
// js文件加密相关
const Uglyfy = require('uglify-es');
const fs = require('fs');
// 基类
const BaseController = require('./app/base/base_controller');
const BaseService = require('./app/base/base_service');
// 常量
const libConst = require('./app/const/lib');
const nodeConst = require('./app/const/template_node');
const paramConst = require('./app/const/template_param');
module.exports = app => {
    // app内定义基类，方便继承
    app.BaseController = BaseController;
    app.BaseService = BaseService;
    // app内定义所有所需常量
    app.libConst = libConst;
    app.nodeConst = nodeConst;
    app.paramConst = paramConst;
    app.awaitRefresh = [];

    //压缩前端js
    if (app.config.min) {
        app.minify = (file) => {
            const files = file instanceof Array ? file : [file];
            for (const f of files) {
                const fileName = app.baseDir + '/app/public/js/' + f;
                const code = fs.readFileSync(fileName, 'utf8');
                fs.writeFileSync(fileName.replace('.js', '.min.js'), Uglyfy.minify(code, { mangle: true }).code);
            }
        };
        app.minify(['spreadjs_rela/spreadjs_zh.js', 'spreadjs_rela/extend_celltype.js', 'path_tree.js', 'compare.js', 'cookies.js', 'lib_detail.js', 'template.js']);
    }
};