@modern-js/plugin 1.1.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/js/modern/manager/async.js +53 -26
  3. package/dist/js/modern/manager/index.js +2 -1
  4. package/dist/js/modern/manager/sync.js +73 -58
  5. package/dist/js/modern/manager/types.js +0 -0
  6. package/dist/js/modern/waterfall/async.js +6 -7
  7. package/dist/js/modern/waterfall/sync.js +2 -3
  8. package/dist/js/modern/workflow/async.js +3 -4
  9. package/dist/js/modern/workflow/parallel.js +2 -2
  10. package/dist/js/modern/workflow/sync.js +2 -3
  11. package/dist/js/node/manager/async.js +51 -23
  12. package/dist/js/node/manager/index.js +13 -0
  13. package/dist/js/node/manager/sync.js +78 -56
  14. package/dist/js/node/manager/types.js +0 -0
  15. package/dist/js/node/waterfall/async.js +6 -7
  16. package/dist/js/node/waterfall/sync.js +2 -3
  17. package/dist/js/node/workflow/async.js +3 -4
  18. package/dist/js/node/workflow/parallel.js +2 -2
  19. package/dist/js/node/workflow/sync.js +2 -3
  20. package/dist/js/treeshaking/manager/async.js +57 -32
  21. package/dist/js/treeshaking/manager/index.js +2 -1
  22. package/dist/js/treeshaking/manager/sync.js +79 -60
  23. package/dist/js/treeshaking/manager/types.js +0 -0
  24. package/dist/js/treeshaking/waterfall/async.js +6 -7
  25. package/dist/js/treeshaking/waterfall/sync.js +2 -3
  26. package/dist/js/treeshaking/workflow/async.js +4 -5
  27. package/dist/js/treeshaking/workflow/parallel.js +2 -2
  28. package/dist/js/treeshaking/workflow/sync.js +2 -3
  29. package/dist/types/manager/async.d.ts +60 -21
  30. package/dist/types/manager/index.d.ts +2 -1
  31. package/dist/types/manager/sync.d.ts +74 -43
  32. package/dist/types/manager/types.d.ts +41 -0
  33. package/dist/types/waterfall/async.d.ts +2 -2
  34. package/jest.config.js +8 -0
  35. package/modern.config.js +1 -9
  36. package/package.json +10 -5
  37. package/tests/async.test.ts +132 -14
  38. package/tests/fixtures/async/core/index.ts +12 -4
  39. package/tests/fixtures/async/dynamic/foo.ts +2 -2
  40. package/tests/fixtures/sync/core/index.ts +9 -4
  41. package/tests/fixtures/sync/dynamic/foo.ts +2 -2
  42. package/tests/pipeline.test.ts +2 -2
  43. package/tests/sync.test.ts +126 -13
  44. package/tests/tsconfig.json +1 -3
  45. package/tests/waterfall.test.ts +2 -2
  46. package/tests/workflow.test.ts +2 -2
  47. package/tsconfig.json +1 -3
  48. package/src/index.ts +0 -5
  49. package/src/manager/async.ts +0 -248
  50. package/src/manager/index.ts +0 -3
  51. package/src/manager/runner.ts +0 -15
  52. package/src/manager/sync.ts +0 -458
  53. package/src/waterfall/async.ts +0 -109
  54. package/src/waterfall/index.ts +0 -2
  55. package/src/waterfall/sync.ts +0 -110
  56. package/src/workflow/async.ts +0 -96
  57. package/src/workflow/index.ts +0 -3
  58. package/src/workflow/parallel.ts +0 -97
  59. package/src/workflow/sync.ts +0 -82
@@ -0,0 +1,41 @@
1
+ import type { Pipeline, Container, MaybeAsync, Middleware, AsyncPipeline } from 'farrow-pipeline';
2
+ import type { Brook, Waterfall, AsyncBrook, AsyncWaterfall } from '../waterfall';
3
+ import type { Worker, Workflow, AsyncWorker, AsyncWorkflow, ParallelWorkflow } from '../workflow';
4
+ /** All hook types. */
5
+
6
+ export declare type Hook = Waterfall<any> | AsyncWaterfall<any> | Workflow<any, any> | AsyncWorkflow<any, any> | ParallelWorkflow<any> | Pipeline<any, any> | AsyncPipeline<any, any>;
7
+ export declare type HooksMap = Record<string, Hook>;
8
+ /** Extract the type of callback function from a hook. */
9
+
10
+ export declare type ToThread<P extends Hook> = P extends Workflow<infer I, infer O> ? Worker<I, O> : P extends AsyncWorkflow<infer I, infer O> ? AsyncWorker<I, O> : P extends ParallelWorkflow<infer I, infer O> ? AsyncWorker<I, O> : P extends Waterfall<infer I> ? Brook<I> : P extends AsyncWaterfall<infer I> ? AsyncBrook<I> : P extends Pipeline<infer I, infer O> ? Middleware<I, O> : P extends AsyncPipeline<infer I, infer O> ? Middleware<I, MaybeAsync<O>> : never;
11
+ /** Extract types of callback function from hooks. */
12
+
13
+ export declare type ToThreads<PS> = { [K in keyof PS]: PS[K] extends Hook ? ToThread<PS[K]> : PS[K] extends void ? void : never };
14
+ /** Extract run method from a hook. */
15
+
16
+ export declare type RunnerFromHook<P extends Hook> = P extends Waterfall<infer I> ? Waterfall<I>['run'] : P extends AsyncWaterfall<infer I> ? AsyncWaterfall<I>['run'] : P extends Workflow<infer I, infer O> ? Workflow<I, O>['run'] : P extends AsyncWorkflow<infer I, infer O> ? AsyncWorkflow<I, O>['run'] : P extends ParallelWorkflow<infer I, infer O> ? ParallelWorkflow<I, O>['run'] : P extends Pipeline<infer I, infer O> ? Pipeline<I, O>['run'] : P extends AsyncPipeline<infer I, infer O> ? AsyncPipeline<I, O>['run'] : never;
17
+ /** Extract all run methods from hooks. */
18
+
19
+ export declare type ToRunners<PS> = { [K in keyof PS]: PS[K] extends Hook ? RunnerFromHook<PS[K]> : PS[K] extends void ? void : never };
20
+ /** All options to define a plugin. */
21
+
22
+ export declare type PluginOptions<Hooks, Setup = undefined> = {
23
+ name?: string;
24
+ pre?: string[];
25
+ post?: string[];
26
+ setup?: Setup;
27
+ rivals?: string[];
28
+ required?: string[];
29
+ usePlugins?: PluginOptions<Hooks, Setup>[];
30
+ registerHook?: Partial<Hooks>;
31
+ };
32
+ /** Options of manager.init method. */
33
+
34
+ export declare type InitOptions = {
35
+ container?: Container;
36
+ };
37
+ /** Common api of setup function. */
38
+
39
+ export declare type CommonAPI<Hooks> = {
40
+ useHookRunners: () => ToRunners<Hooks>;
41
+ };
@@ -2,7 +2,7 @@ import { MaybeAsync, Container } from 'farrow-pipeline';
2
2
  declare const ASYNC_WATERFALL_SYMBOL: unique symbol;
3
3
  export declare type AsyncBrook<I = unknown> = (I: I) => MaybeAsync<I>;
4
4
  export declare type AsyncBrookInput<I = unknown> = AsyncBrook<I> | {
5
- middlware: AsyncBrook<I>;
5
+ middleware: AsyncBrook<I>;
6
6
  };
7
7
  export declare type AsyncBrooks<I = unknown> = AsyncBrook<I>[];
8
8
  export declare type AsyncBrookInputs<I = unknown> = AsyncBrookInput<I>[];
@@ -14,7 +14,7 @@ export declare type RunAsyncWaterfallOptions<I = unknown> = {
14
14
  export declare type AsyncWaterfall<I> = {
15
15
  run: (input: I, options?: RunAsyncWaterfallOptions<I>) => MaybeAsync<I>;
16
16
  use: (...I: AsyncBrookInputs<I>) => AsyncWaterfall<I>;
17
- middlware: AsyncBrook<I>;
17
+ middleware: AsyncBrook<I>;
18
18
  [ASYNC_WATERFALL_SYMBOL]: true;
19
19
  };
20
20
  export declare type AsyncWaterfall2AsyncBrook<P extends AsyncWaterfall<any>> = P extends AsyncWaterfall<infer I> ? AsyncBrook<I> : never;
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ const sharedConfig = require('@scripts/jest-config');
2
+
3
+ /** @type {import('@jest/types').Config.InitialOptions} */
4
+ module.exports = {
5
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
+ ...sharedConfig,
7
+ rootDir: __dirname,
8
+ };
package/modern.config.js CHANGED
@@ -1,10 +1,2 @@
1
1
  /** @type {import('@modern-js/module-tools').UserConfig} */
2
- module.exports = {
3
- testing: {
4
- jest: {
5
- collectCoverage: true,
6
- collectCoverageFrom: ['./src/**/*.ts'],
7
- coveragePathIgnorePatterns: ['/node_modules/'],
8
- },
9
- },
10
- };
2
+ module.exports = {};
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.1.2",
14
+ "version": "1.3.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -20,12 +20,14 @@
20
20
  "exports": {
21
21
  ".": {
22
22
  "node": {
23
+ "jsnext:source": "./src/index.ts",
23
24
  "import": "./dist/js/modern/index.js",
24
25
  "require": "./dist/js/node/index.js"
25
26
  },
26
27
  "default": "./dist/js/treeshaking/index.js"
27
28
  },
28
29
  "./node": {
30
+ "jsnext:source": "./node.js",
29
31
  "default": "./node.js"
30
32
  }
31
33
  },
@@ -37,8 +39,9 @@
37
39
  "@types/jest": "^26",
38
40
  "@types/node": "^14",
39
41
  "typescript": "^4",
40
- "@modern-js/plugin-testing": "^1.1.0",
41
- "@modern-js/module-tools": "^1.1.0"
42
+ "@scripts/build": "0.0.0",
43
+ "jest": "^27",
44
+ "@scripts/jest-config": "0.0.0"
42
45
  },
43
46
  "sideEffects": false,
44
47
  "publishConfig": {
@@ -47,7 +50,9 @@
47
50
  },
48
51
  "scripts": {
49
52
  "new": "modern new",
53
+ "dev": "modern build --watch",
50
54
  "build": "modern build",
51
- "test": "modern test --passWithNoTests"
52
- }
55
+ "test": "jest --passWithNoTests"
56
+ },
57
+ "readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
53
58
  }
@@ -7,19 +7,20 @@ import {
7
7
  createContext,
8
8
  createContainer,
9
9
  } from 'farrow-pipeline';
10
- import { main } from './fixtures/async/core';
10
+ import type { PluginOptions, AsyncSetup } from '../src';
11
+ import { createManager, createAsyncManager, useRunner } from '../src/manager';
12
+ import { createWaterfall, createAsyncWaterfall } from '../src/waterfall';
13
+ import {
14
+ createWorkflow,
15
+ createAsyncWorkflow,
16
+ createParallelWorkflow,
17
+ } from '../src/workflow';
18
+ import { main, TestAsyncHooks, TestAsyncPlugin } from './fixtures/async/core';
11
19
  import foo from './fixtures/async/base/foo';
12
20
  import bar, { getBar } from './fixtures/async/base/bar';
13
21
  import dFoo from './fixtures/async/dynamic/foo';
14
22
  import dBar, { getNumber } from './fixtures/async/dynamic/bar';
15
23
  import { sleep } from './helpers';
16
- import { createManager, createAsyncManager, useRunner } from '@/manager';
17
- import { createWaterfall, createAsyncWaterfall } from '@/waterfall';
18
- import {
19
- createWorkflow,
20
- createAsyncWorkflow,
21
- createParallelWorkflow,
22
- } from '@/workflow';
23
24
 
24
25
  describe('async manager', () => {
25
26
  it('base usage', async () => {
@@ -40,7 +41,7 @@ describe('async manager', () => {
40
41
  expect(getBar()).toBe(3);
41
42
  });
42
43
 
43
- it('should support async initializer', async () => {
44
+ it('should support async setup function', async () => {
44
45
  const manager = createAsyncManager();
45
46
 
46
47
  const countContext = createContext(0);
@@ -397,6 +398,22 @@ describe('async manager', () => {
397
398
  expect(count).toBe(2);
398
399
  });
399
400
 
401
+ it('should support manager clone and override pluginAPI', done => {
402
+ const myAPI = { hello: () => 1 };
403
+ const manager = createAsyncManager({}, myAPI);
404
+ const plugin = {
405
+ setup(api: typeof myAPI) {
406
+ expect(api.hello()).toEqual(2);
407
+ done();
408
+ },
409
+ };
410
+ const clonedManager = manager.clone({
411
+ hello: () => 2,
412
+ });
413
+
414
+ clonedManager.usePlugin(() => plugin).init();
415
+ });
416
+
400
417
  it('isPlugin if exclusive plugins of manager', () => {
401
418
  const manager0 = createManager();
402
419
  const manager1 = createAsyncManager();
@@ -501,11 +518,11 @@ describe('async manager', () => {
501
518
 
502
519
  runner.pipeline({});
503
520
  await runner.asyncPipeline({});
504
- runner.waterfall({});
505
- await runner.asyncWaterfall({});
506
- runner.workflow({});
507
- await runner.asyncWorkflow({});
508
- await runner.parallelWorkflow({});
521
+ runner.waterfall();
522
+ await runner.asyncWaterfall();
523
+ runner.workflow();
524
+ await runner.asyncWorkflow();
525
+ await runner.parallelWorkflow();
509
526
 
510
527
  expect(list).toStrictEqual([
511
528
  'pipeline',
@@ -591,4 +608,105 @@ describe('async manager', () => {
591
608
  );
592
609
  });
593
610
  });
611
+
612
+ describe('setup api', () => {
613
+ it('should allow to access api.useHookRunners by default', done => {
614
+ const manager = createAsyncManager<TestAsyncHooks>();
615
+ const plugin: TestAsyncPlugin = {
616
+ name: 'plugin',
617
+ setup: api => {
618
+ expect(api.useHookRunners).toBeTruthy();
619
+ done();
620
+ },
621
+ };
622
+ manager.usePlugin(() => plugin);
623
+ manager.init();
624
+ });
625
+
626
+ it('should allow to register extra api', done => {
627
+ type API = { foo: () => void };
628
+
629
+ const manager = createAsyncManager<TestAsyncHooks, API>(
630
+ {},
631
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
632
+ { foo: () => {} },
633
+ );
634
+
635
+ const plugin: PluginOptions<
636
+ TestAsyncHooks,
637
+ AsyncSetup<TestAsyncHooks, API>
638
+ > = {
639
+ name: 'plugin',
640
+ setup: api => {
641
+ expect(api.foo).toBeTruthy();
642
+ done();
643
+ },
644
+ };
645
+
646
+ manager.usePlugin(() => plugin);
647
+ manager.init();
648
+ });
649
+ });
650
+
651
+ describe('usePlugins option', () => {
652
+ it('should allow to use single plugin', async () => {
653
+ const manager = createAsyncManager<TestAsyncHooks>();
654
+
655
+ const list: number[] = [];
656
+ const plugin0: TestAsyncPlugin = {
657
+ name: 'plugin0',
658
+ setup: () => {
659
+ list.push(0);
660
+ },
661
+ };
662
+
663
+ const plugin1: TestAsyncPlugin = {
664
+ name: 'plugin1',
665
+ usePlugins: [plugin0],
666
+ setup: () => {
667
+ list.push(1);
668
+ },
669
+ };
670
+
671
+ manager.usePlugin(() => plugin1);
672
+
673
+ await manager.init();
674
+
675
+ expect(list).toStrictEqual([0, 1]);
676
+ });
677
+
678
+ it('should allow to use multiple plugins', async () => {
679
+ const manager = createAsyncManager<TestAsyncHooks>();
680
+
681
+ const list: number[] = [];
682
+ const plugin0: TestAsyncPlugin = {
683
+ name: 'plugin0',
684
+ setup: () => {
685
+ list.push(0);
686
+ },
687
+ };
688
+
689
+ const plugin1 = {
690
+ name: 'plugin1',
691
+ usePlugins: [plugin0],
692
+ setup: () => {
693
+ list.push(1);
694
+ },
695
+ };
696
+
697
+ const plugin2 = {
698
+ name: 'plugin2',
699
+ usePlugins: [plugin1],
700
+ setup: () => {
701
+ list.push(2);
702
+ },
703
+ };
704
+
705
+ manager.usePlugin(() => plugin2);
706
+
707
+ await manager.init();
708
+
709
+ expect(list).toStrictEqual([0, 1, 2]);
710
+ });
711
+ });
594
712
  });
@@ -3,6 +3,8 @@ import {
3
3
  createWaterfall,
4
4
  createWorkflow,
5
5
  createContext,
6
+ PluginOptions,
7
+ AsyncSetup,
6
8
  } from '../../../../src';
7
9
 
8
10
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -99,9 +101,15 @@ const lifecircle = {
99
101
  preBuild,
100
102
  postBuild,
101
103
  };
102
- export const main = createAsyncManager<ExternalProgress, typeof lifecircle>(
103
- lifecircle,
104
- );
104
+
105
+ export type TestAsyncHooks = ExternalProgress & typeof lifecircle;
106
+
107
+ export const main = createAsyncManager<TestAsyncHooks>(lifecircle);
108
+
109
+ export type TestAsyncPlugin = PluginOptions<
110
+ TestAsyncHooks,
111
+ AsyncSetup<TestAsyncHooks>
112
+ >;
105
113
 
106
114
  export const { createPlugin } = main;
107
115
 
@@ -111,7 +119,7 @@ export const { usePlugin } = main;
111
119
 
112
120
  export const initPlugins = main.init;
113
121
 
114
- export const registeManager = main.registe;
122
+ export const { registerHook } = main;
115
123
 
116
124
  export const { useRunner } = main;
117
125
 
@@ -1,5 +1,5 @@
1
1
  import { createAsyncWaterfall } from '../../../../src';
2
- import { createPlugin, registeManager, useRunner } from '../core';
2
+ import { createPlugin, registerHook, useRunner } from '../core';
3
3
 
4
4
  // declare new lifecircle type
5
5
  declare module '../core' {
@@ -13,7 +13,7 @@ const fooWaterfall = createAsyncWaterfall();
13
13
 
14
14
  const foo = createPlugin(() => {
15
15
  // registe new lifecircle
16
- registeManager({ fooWaterfall });
16
+ registerHook({ fooWaterfall });
17
17
 
18
18
  return {
19
19
  preDev: () => {
@@ -1,8 +1,10 @@
1
1
  import {
2
+ Setup,
2
3
  createManager,
3
4
  createWaterfall,
4
5
  createWorkflow,
5
6
  createContext,
7
+ PluginOptions,
6
8
  } from '../../../../src';
7
9
 
8
10
  // eslint-disable-next-line @typescript-eslint/ban-types
@@ -99,9 +101,12 @@ const lifecircle = {
99
101
  preBuild,
100
102
  postBuild,
101
103
  };
102
- export const main = createManager<ExternalProgress, typeof lifecircle>(
103
- lifecircle,
104
- );
104
+
105
+ export type TestHooks = ExternalProgress & typeof lifecircle;
106
+
107
+ export const main = createManager<TestHooks>(lifecircle);
108
+
109
+ export type TestPlugin = PluginOptions<TestHooks, Setup<TestHooks>>;
105
110
 
106
111
  export const { createPlugin } = main;
107
112
 
@@ -111,7 +116,7 @@ export const { usePlugin } = main;
111
116
 
112
117
  export const initPlugins = main.init;
113
118
 
114
- export const registeManager = main.registe;
119
+ export const { registerHook } = main;
115
120
 
116
121
  export const { useRunner } = main;
117
122
 
@@ -1,5 +1,5 @@
1
1
  import { createAsyncWaterfall } from '../../../../src';
2
- import { createPlugin, registeManager, useRunner } from '../core';
2
+ import { createPlugin, registerHook, useRunner } from '../core';
3
3
 
4
4
  // declare new lifecircle type
5
5
  declare module '../core' {
@@ -13,7 +13,7 @@ const fooWaterfall = createAsyncWaterfall();
13
13
 
14
14
  const foo = createPlugin(() => {
15
15
  // registe new lifecircle
16
- registeManager({ fooWaterfall });
16
+ registerHook({ fooWaterfall });
17
17
 
18
18
  return {
19
19
  preDev: () => {
@@ -1,7 +1,6 @@
1
1
  // eslint-disable-next-line eslint-comments/disable-enable-pair
2
2
  /* eslint-disable max-lines */
3
3
  import * as asyncHooksImpl from 'farrow-pipeline/asyncHooks.node';
4
- import { sleep } from './helpers';
5
4
  import {
6
5
  createContext,
7
6
  createContainer,
@@ -10,7 +9,8 @@ import {
10
9
  usePipeline,
11
10
  useContainer,
12
11
  isPipeline,
13
- } from '@/index';
12
+ } from '../src';
13
+ import { sleep } from './helpers';
14
14
 
15
15
  describe('createPipeline', () => {
16
16
  it('basic usage', async () => {
@@ -6,18 +6,19 @@ import {
6
6
  createContext,
7
7
  createContainer,
8
8
  } from 'farrow-pipeline';
9
- import { main } from './fixtures/sync/core';
10
- import foo from './fixtures/sync/base/foo';
11
- import bar, { getBar } from './fixtures/sync/base/bar';
12
- import dFoo from './fixtures/sync/dynamic/foo';
13
- import dBar, { getNumber, setNumber } from './fixtures/sync/dynamic/bar';
14
- import { createManager, createAsyncManager, useRunner } from '@/manager';
15
- import { createWaterfall, createAsyncWaterfall } from '@/waterfall';
9
+ import type { PluginOptions, Setup } from '../src';
10
+ import { createManager, createAsyncManager, useRunner } from '../src/manager';
11
+ import { createWaterfall, createAsyncWaterfall } from '../src/waterfall';
16
12
  import {
17
13
  createWorkflow,
18
14
  createAsyncWorkflow,
19
15
  createParallelWorkflow,
20
- } from '@/workflow';
16
+ } from '../src/workflow';
17
+ import { main, TestHooks, TestPlugin } from './fixtures/sync/core';
18
+ import foo from './fixtures/sync/base/foo';
19
+ import bar, { getBar } from './fixtures/sync/base/bar';
20
+ import dFoo from './fixtures/sync/dynamic/foo';
21
+ import dBar, { getNumber, setNumber } from './fixtures/sync/dynamic/bar';
21
22
 
22
23
  describe('sync manager', () => {
23
24
  it('base useage', () => {
@@ -391,6 +392,22 @@ describe('sync manager', () => {
391
392
  expect(count).toBe(1);
392
393
  });
393
394
 
395
+ it('should support manager clone and override pluginAPI', done => {
396
+ const myAPI = { hello: () => 1 };
397
+ const manager = createManager({}, myAPI);
398
+ const plugin = {
399
+ setup(api: typeof myAPI) {
400
+ expect(api.hello()).toEqual(2);
401
+ done();
402
+ },
403
+ };
404
+ const clonedManager = manager.clone({
405
+ hello: () => 2,
406
+ });
407
+
408
+ clonedManager.usePlugin(() => plugin).init();
409
+ });
410
+
394
411
  it('isPlugin: exclusive plugins of manager', () => {
395
412
  const manager0 = createAsyncManager();
396
413
  const manager1 = createManager();
@@ -497,11 +514,11 @@ describe('sync manager', () => {
497
514
 
498
515
  runner.pipeline({});
499
516
  await runner.asyncPipeline({});
500
- runner.waterfall({});
501
- await runner.asyncWaterfall({});
502
- runner.workflow({});
503
- await runner.asyncWorkflow({});
504
- await runner.parallelWorkflow({});
517
+ runner.waterfall();
518
+ await runner.asyncWaterfall();
519
+ runner.workflow();
520
+ await runner.asyncWorkflow();
521
+ await runner.parallelWorkflow();
505
522
 
506
523
  expect(list).toStrictEqual([
507
524
  'pipeline',
@@ -561,4 +578,100 @@ describe('sync manager', () => {
561
578
  );
562
579
  });
563
580
  });
581
+
582
+ describe('setup api', () => {
583
+ it('should allow to access api.useHookRunners by default', done => {
584
+ const manager = createManager<TestHooks>();
585
+ const plugin: TestPlugin = {
586
+ name: 'plugin',
587
+ setup: api => {
588
+ expect(api.useHookRunners).toBeTruthy();
589
+ done();
590
+ },
591
+ };
592
+ manager.usePlugin(() => plugin);
593
+ manager.init();
594
+ });
595
+
596
+ it('should allow to register extra api', done => {
597
+ type API = { foo: () => void };
598
+
599
+ const manager = createAsyncManager<TestHooks, API>(
600
+ {},
601
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
602
+ { foo: () => {} },
603
+ );
604
+
605
+ const plugin: PluginOptions<TestHooks, Setup<TestHooks, API>> = {
606
+ name: 'plugin',
607
+ setup: api => {
608
+ expect(api.foo).toBeTruthy();
609
+ done();
610
+ },
611
+ };
612
+
613
+ manager.usePlugin(() => plugin);
614
+ manager.init();
615
+ });
616
+ });
617
+
618
+ describe('usePlugins option', () => {
619
+ it('should allow to use single plugin', async () => {
620
+ const manager = createManager<TestHooks>();
621
+
622
+ const list: number[] = [];
623
+ const plugin0: TestPlugin = {
624
+ name: 'plugin0',
625
+ setup: () => {
626
+ list.push(0);
627
+ },
628
+ };
629
+
630
+ const plugin1: TestPlugin = {
631
+ name: 'plugin1',
632
+ usePlugins: [plugin0],
633
+ setup: () => {
634
+ list.push(1);
635
+ },
636
+ };
637
+
638
+ manager.usePlugin(() => plugin1);
639
+ manager.init();
640
+
641
+ expect(list).toStrictEqual([0, 1]);
642
+ });
643
+
644
+ it('should allow to use multiple plugins', async () => {
645
+ const manager = createManager<TestHooks>();
646
+
647
+ const list: number[] = [];
648
+ const plugin0: TestPlugin = {
649
+ name: 'plugin0',
650
+ setup: () => {
651
+ list.push(0);
652
+ },
653
+ };
654
+
655
+ const plugin1: TestPlugin = {
656
+ name: 'plugin1',
657
+ usePlugins: [plugin0],
658
+ setup: () => {
659
+ list.push(1);
660
+ },
661
+ };
662
+
663
+ const plugin2: TestPlugin = {
664
+ name: 'plugin2',
665
+ usePlugins: [plugin1],
666
+ setup: () => {
667
+ list.push(2);
668
+ },
669
+ };
670
+
671
+ manager.usePlugin(() => plugin2);
672
+ manager.init();
673
+
674
+ expect(list).toStrictEqual([0, 1, 2]);
675
+ });
676
+ });
564
677
  });
@@ -7,9 +7,7 @@
7
7
  "isolatedModules": true,
8
8
  "target": "ES5",
9
9
  "esModuleInterop": true,
10
- "paths": {
11
- "@/*": ["../src/*"]
12
- },
10
+ "paths": {},
13
11
  "types": ["node", "jest"]
14
12
  }
15
13
  }
@@ -1,5 +1,4 @@
1
1
  import { enable, disable } from 'farrow-pipeline/asyncHooks.node';
2
- import { sleep } from './helpers';
3
2
  import {
4
3
  createWaterfall,
5
4
  createAsyncWaterfall,
@@ -7,7 +6,8 @@ import {
7
6
  createContainer,
8
7
  isWaterfall,
9
8
  isAsyncWaterfall,
10
- } from '@/index';
9
+ } from '../src';
10
+ import { sleep } from './helpers';
11
11
 
12
12
  describe('waterfall', () => {
13
13
  it('base usage', () => {
@@ -1,4 +1,3 @@
1
- import { sleep } from './helpers';
2
1
  import {
3
2
  createWorkflow,
4
3
  createAsyncWorkflow,
@@ -6,7 +5,8 @@ import {
6
5
  isWorkflow,
7
6
  isAsyncWorkflow,
8
7
  isParallelWorkflow,
9
- } from '@/workflow';
8
+ } from '../src/workflow';
9
+ import { sleep } from './helpers';
10
10
 
11
11
  describe('workflow', () => {
12
12
  describe('sync', () => {
package/tsconfig.json CHANGED
@@ -7,9 +7,7 @@
7
7
  "isolatedModules": true,
8
8
  "target": "ES5",
9
9
  "esModuleInterop": true,
10
- "paths": {
11
- "@/*": ["./src/*"]
12
- },
10
+ "paths": {},
13
11
  "types": ["node", "jest"]
14
12
  },
15
13
  "include": ["src"]
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- export * from 'farrow-pipeline';
2
-
3
- export * from './waterfall';
4
- export * from './workflow';
5
- export * from './manager';