Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support dependency module for standalone #155

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ plugin/dal/test/fixtures/modules/*/dal/
!plugin/orm/test/fixtures/prepare.js
!benchmark/**/*.js
plugin/tegg/test/fixtures/apps/**/*.js
!standalone/standalone/test/fixtures/**/node_modules
!standalone/standalone/test/fixtures/**/node_modules/**/*.js
13 changes: 11 additions & 2 deletions standalone/standalone/src/Runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModuleConfigUtil, ModuleReference, RuntimeConfig } from '@eggjs/tegg-common-util';
import { ModuleConfigUtil, ModuleReference, ReadModuleReferenceOptions, RuntimeConfig } from '@eggjs/tegg-common-util';
import {
EggPrototype, EggPrototypeLifecycleUtil,
LoadUnit,
Expand Down Expand Up @@ -26,6 +26,10 @@ import { ConfigSourceQualifierAttribute } from './ConfigSource';
import { ConfigSourceLoadUnitHook } from './ConfigSourceLoadUnitHook';
import { LoadUnitInnerClassHook } from './LoadUnitInnerClassHook';

export interface ModuleDependency extends ReadModuleReferenceOptions {
baseDir: string;
}

export interface RunnerOptions {
/**
* @deprecated
Expand All @@ -35,6 +39,7 @@ export interface RunnerOptions {
env?: string;
name?: string;
innerObjectHandlers?: Record<string, InnerObject[]>;
dependencies?: (string | ModuleDependency)[];
}

export class Runner {
Expand Down Expand Up @@ -62,7 +67,11 @@ export class Runner {
this.cwd = cwd;
this.env = options?.env;
this.name = options?.name;
this.moduleReferences = ModuleConfigUtil.readModuleReference(this.cwd);
const moduleDirs = (options?.dependencies || []).concat(this.cwd);
this.moduleReferences = moduleDirs.reduce((list, baseDir) => {
const module = typeof baseDir === 'string' ? { baseDir } : baseDir;
return list.concat(...ModuleConfigUtil.readModuleReference(module.baseDir, module));
}, [] as readonly ModuleReference[]);
this.moduleConfigs = {};
this.innerObjects = {
moduleConfigs: [{
Expand Down
19 changes: 19 additions & 0 deletions standalone/standalone/test/fixtures/dependency/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ContextProto, Inject } from '@eggjs/tegg';
import { Runner, MainRunner } from '@eggjs/tegg/standalone';
import { Hello } from 'dependency-2/foo';
import { ConfigSourceQualifier } from '../../../src/ConfigSource';

@ContextProto()
@Runner()
export class Foo implements MainRunner<string> {
@Inject()
hello: Hello;

@Inject()
@ConfigSourceQualifier('dependency2')
moduleConfig: any;

async main(): Promise<string> {
return this.hello.hello() + JSON.stringify(this.moduleConfig);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions standalone/standalone/test/fixtures/dependency/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "entry",
"eggModule": {
"name": "entry"
}
}
11 changes: 11 additions & 0 deletions standalone/standalone/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ describe('test/index.test.ts', () => {
});
});

describe('runner with dependency', () => {
it('should work', async () => {
const msg: string = await main(path.join(__dirname, './fixtures/dependency'), {
dependencies: [
path.join(__dirname, './fixtures/dependency/node_modules/dependency-1'),
],
});
assert(msg === 'hello!{"features":{"dynamic":{"foo":"bar"}}}');
});
});

describe('runner with inner object', () => {
it('should work', async () => {
const msg: string = await main(path.join(__dirname, './fixtures/inner-object'), {
Expand Down
Loading