Skip to content

Commit

Permalink
feat: support dependency module for standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
gxkl committed Sep 18, 2023
1 parent ea75fdc commit 483d6e7
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ 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
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';

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (14)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (14)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (16)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-windows (14)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (16)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (18)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-windows (16)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (18)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-ubuntu (20)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-windows (18)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-macos (20)

Unable to resolve path to module 'dependency-2/foo'

Check failure on line 3 in standalone/standalone/test/fixtures/dependency/foo.ts

View workflow job for this annotation

GitHub Actions / Runner-windows (20)

Unable to resolve path to module '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.

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

0 comments on commit 483d6e7

Please sign in to comment.