<commit_before>const getConfigFn = require('think-config').getConfigFn;
const Logger = require('think-logger3');
const assert = require('assert');
const Loader = require('think-loader');
const path = require('path');
const helper = require('think-helper');
const Crontab = require('think-crontab');
const cluster = require('cluster');
const thinkCluster = require('think-cluster');

const debug = require('debug')('think');

require('./think.js');
// ThinkJS root path
const thinkPath = path.join(__dirname, '..');

/**
 * think loader
 * @param {Object} options 
 */
const thinkLoader = class {
  constructor(options = {}){
    this.options = options;
  }
  /**
   * init path
   */
  initPath(){
    think.ROOT_PATH = this.options.ROOT_PATH;
    think.APP_PATH = this.options.APP_PATH;
    if(this.options.env){
      think.app.env = this.options.env;
    }
    if(this.options.proxy){
      think.app.proxy = this.options.proxy;
    }
  }
  /**
   * load app data
   */
  loadData(){
    let config = think.loader.loadConfig(think.app.env);
    let modules = think.loader.modules;

    think.config = getConfigFn(config, modules.length > 0);
    let loggerConfig = helper.parseAdapterConfig(think.config('logger'));
    think.logger = new Logger(loggerConfig);
    
    //add data to koa application
    think.app.modules = modules;
    think.app.controllers = think.loader.loadController();
    think.app.logics = think.loader.loadLogic();
    think.app.models = think.loader.loadModel();
    think.app.services = think.loader.loadService();
    think.app.routers = think.loader.loadRouter();
  }
  /**
   * load middleware
   */
  loadMiddleware(){
    const middlewares = think.loader.loadMiddleware(think.app);
    if(debug.enabled){
      think.logger.info(middlewares);
    }
    middlewares.forEach(middleware => {
      think.app.use(middleware);
    });
  }
  /**
   * load extend
   */
  loadExtend(){
    let exts = think.loader.loadExtend();
    const list = [
      ['think', think],
      ['application', think.app],
      ['context', think.app.context],
      ['request', think.app.request],
      ['response', think.app.response],
      ['controller', think.Controller.prototype],
      ['logic', think.Logic.prototype]
    ];
    list.forEach(item => {
      if(!exts[item[0]]) return;
      Loader.extend(item[1], exts[item[0]]);
    });
  }
  /**
   * load crontab
   */
  loadCrontab(){
    const crontab = think.loader.loadCrontab();
    const instance = new Crontab(crontab, think.app);
    instance.runTask();
  }
  /**
   * load all data
   */
  loadAll(){
    let type = 'master';
    this.initPath();
    think.loader = new Loader(think.APP_PATH, thinkPath, think.app);
    this.loadData();
    if(!cluster.isMaster){
      this.loadExtend();
      this.loadMiddleware();
      this.loadCrontab();
      type = thinkCluster.isAgent ? 'agent' : 'worker';
    }
    think.loader.loadBootstrap(type);
  }
}

module.exports = thinkLoader;<commit_msg>Change load extend before load data<commit_after>const getConfigFn = require('think-config').getConfigFn;
const Logger = require('think-logger3');
const assert = require('assert');
const Loader = require('think-loader');
const path = require('path');
const helper = require('think-helper');
const Crontab = require('think-crontab');
const cluster = require('cluster');
const thinkCluster = require('think-cluster');

const debug = require('debug')('think');

require('./think.js');
// ThinkJS root path
const thinkPath = path.join(__dirname, '..');

/**
 * think loader
 * @param {Object} options 
 */
const thinkLoader = class {
  constructor(options = {}){
    this.options = options;
  }
  /**
   * init path
   */
  initPath(){
    think.ROOT_PATH = this.options.ROOT_PATH;
    think.APP_PATH = this.options.APP_PATH;
    if(this.options.env){
      think.app.env = this.options.env;
    }
    if(this.options.proxy){
      think.app.proxy = this.options.proxy;
    }
  }
  /**
   * load app data
   */
  loadData(){
    
    //add data to koa application
    think.app.modules = think.loader.modules;
    think.app.controllers = think.loader.loadController();
    think.app.logics = think.loader.loadLogic();
    think.app.models = think.loader.loadModel();
    think.app.services = think.loader.loadService();
    think.app.routers = think.loader.loadRouter();
  }
  /**
   * load middleware
   */
  loadMiddleware(){
    const middlewares = think.loader.loadMiddleware(think.app);
    if(debug.enabled){
      think.logger.info(middlewares);
    }
    middlewares.forEach(middleware => {
      think.app.use(middleware);
    });
  }
  /**
   * load extend
   */
  loadExtend(){
    let exts = think.loader.loadExtend();
    const list = [
      ['think', think],
      ['application', think.app],
      ['context', think.app.context],
      ['request', think.app.request],
      ['response', think.app.response],
      ['controller', think.Controller.prototype],
      ['logic', think.Logic.prototype]
    ];
    list.forEach(item => {
      if(!exts[item[0]]) return;
      Loader.extend(item[1], exts[item[0]]);
    });
  }
  /**
   * load crontab
   */
  loadCrontab(){
    const crontab = think.loader.loadCrontab();
    const instance = new Crontab(crontab, think.app);
    instance.runTask();
  }
  /**
   * load all data
   */
  loadAll(){
    let type = 'master';
    this.initPath();
    think.loader = new Loader(think.APP_PATH, thinkPath, think.app);
    think.config = getConfigFn(think.loader.loadConfig(think.app.env), think.loader.modules.length > 0);
    think.logger = new Logger(helper.parseAdapterConfig(think.config('logger')));

    if(!cluster.isMaster){
      this.loadExtend();
      this.loadData();
      this.loadMiddleware();
      this.loadCrontab();
      type = thinkCluster.isAgent ? 'agent' : 'worker';
    }
    think.loader.loadBootstrap(type);
  }
}

module.exports = thinkLoader;
{'fraction_non_alphanumeric': 0.16310613767378773, 'fraction_numerical': 0.00169548999660902, 'mean_word_length': 3.079529737206086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2073865', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1872, 'n_words': 339}