Skip to content

Commit

Permalink
refactor: moduleInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhennann committed Dec 15, 2023
1 parent e9443f0 commit ee22ffa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/egg-born-backend/lib/framework/meta.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const MetaClassFn = require('./metaClass.js');
const MetaUtilFn = require('./util.js');
const MetaEnvFn = require('../module/metaEnv.js');

module.exports = function () {
module.exports = function (app) {
// meta
const meta = {};

Expand All @@ -11,6 +12,9 @@ module.exports = function () {
// util
meta.util = MetaUtilFn();

// env
MetaEnvFn(app, meta);

// meta
return meta;
};
7 changes: 6 additions & 1 deletion packages/egg-born-backend/lib/framework/moduleInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ const Module = require('module');
const ModuleInfo = require('@zhennann/module-info');
const MetaFn = require('./meta.js');

let __patched = false;
module.exports = function (app) {
// only once
if (__patched) return;
__patched = true;

// meta
const meta = MetaFn();
const meta = MetaFn(app);

// compile
const originalCompile = Module.prototype._compile;
Expand Down
10 changes: 3 additions & 7 deletions packages/egg-born-backend/lib/module/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const uuid = require('uuid');
const utilFn = require('../utils/util.js');
const mockUtilFn = require('../utils/mockUtil.js');
const reloadFn = require('./reload.js');
const metaEnvFn = require('./metaEnv.js');

module.exports = function (loader) {
// meta
Expand All @@ -15,13 +16,8 @@ module.exports = function (loader) {
meta.inApp = loader.app.type === 'application';
meta.inAgent = loader.app.type === 'agent';

// isProd
meta.isProd =
loader.app.config.env !== 'local' && loader.app.config.env !== 'unittest' && loader.app.config.env !== 'test';
// isTest
meta.isTest = loader.app.config.env === 'unittest' || loader.app.config.env === 'test';
// isLocal
meta.isLocal = loader.app.config.env === 'local';
// env
metaEnvFn(loader.app, meta);

// util
meta.util = utilFn(loader.app);
Expand Down
8 changes: 8 additions & 0 deletions packages/egg-born-backend/lib/module/metaEnv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function (app, meta) {
// isProd
meta.isProd = app.config.env !== 'local' && app.config.env !== 'unittest' && app.config.env !== 'test';
// isTest
meta.isTest = app.config.env === 'unittest' || app.config.env === 'test';
// isLocal
meta.isLocal = app.config.env === 'local';
};

0 comments on commit ee22ffa

Please sign in to comment.