Skip to content

Commit

Permalink
fix: support eggjs in monorepo (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Dec 11, 2020
1 parent 87247ea commit 18c32b9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"egg-logger": "^2.4.2",
"egg-path-matching": "^1.0.1",
"extend2": "^1.0.0",
"find-up": "^5.0.0",
"midway-schedule": "^2.5.2",
"mkdirp": "^1.0.4"
},
Expand Down
32 changes: 26 additions & 6 deletions packages/web/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseNormalDir } from './utils';
import { findLernaRoot, parseNormalDir } from './utils';
import * as extend from 'extend2';
import { EggAppInfo } from 'egg';
import { BootstrapStarter } from '@midwayjs/bootstrap';
Expand Down Expand Up @@ -51,11 +51,20 @@ export const createAppWorkerLoader = () => {
this.appDir = this.app.appDir = result.appDir;
}

const result = super.getEggPaths();
const monorepoRoot = findLernaRoot();
if (monorepoRoot) {
const monorepoEgg = join(monorepoRoot, 'node_modules/egg');
if (existsSync(monorepoEgg)) {
result.push(monorepoEgg);
}
}

if (process.env.MIDWAY_EGG_PLUGIN_PATH) {
const result = super.getEggPaths();
return result.concat(process.env.MIDWAY_EGG_PLUGIN_PATH);
result.push(process.env.MIDWAY_EGG_PLUGIN_PATH);
}
return super.getEggPaths();
const pathSet = new Set(result);
return Array.from(pathSet);
}

protected getAppInfo(): EggAppInfo {
Expand Down Expand Up @@ -146,10 +155,21 @@ export const createAgentWorkerLoader = () => {
this.appDir = this.app.appDir = result.appDir;
}

const result = super.getEggPaths();
const monorepoRoot = findLernaRoot();
if (monorepoRoot) {
const monorepoEgg = join(monorepoRoot, 'node_modules/egg');
if (existsSync(monorepoEgg)) {
result.push(monorepoEgg);
}
}

if (process.env.MIDWAY_EGG_PLUGIN_PATH) {
return super.getEggPaths().concat(process.env.MIDWAY_EGG_PLUGIN_PATH);
result.push(process.env.MIDWAY_EGG_PLUGIN_PATH);
}
return super.getEggPaths();

const pathSet = new Set(result);
return Array.from(pathSet);
}

protected getAppInfo(): EggAppInfo {
Expand Down
16 changes: 16 additions & 0 deletions packages/web/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isTypeScriptEnvironment } from '@midwayjs/bootstrap';
import { basename, join } from 'path';
import { sync as findUpSync, stop } from 'find-up';

export const parseNormalDir = (baseDir: string, isTypescript = true) => {
if (isTypescript) {
Expand Down Expand Up @@ -28,3 +29,18 @@ export const parseNormalDir = (baseDir: string, isTypescript = true) => {
};
}
};

export const findLernaRoot = (findRoot = process.cwd()) => {
const userHome = process.env.HOME;
return findUpSync(
directory => {
if (findUpSync.exists(join(directory, 'lerna.json'))) {
return directory;
}
if (directory === userHome) {
return stop;
}
},
{ cwd: findRoot, type: 'directory' }
);
};
3 changes: 0 additions & 3 deletions packages/web/test/.setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
const path = require('path');

process.env.MIDWAY_TS_MODE = 'true';
process.env.MIDWAY_EGG_PLUGIN_PATH = path.join(__dirname, '../../../');
jest.setTimeout(30000);

0 comments on commit 18c32b9

Please sign in to comment.