@teambit/harmony 0.2.10 → 0.3.1

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 (206) hide show
  1. package/aspect.ts +52 -0
  2. package/config/config.ts +36 -0
  3. package/config/index.ts +1 -0
  4. package/container.ts +22 -0
  5. package/dist/aspect.d.ts +3 -2
  6. package/dist/aspect.js +4 -0
  7. package/dist/aspect.js.map +1 -0
  8. package/dist/config/config.d.ts +2 -1
  9. package/dist/config/config.js +7 -0
  10. package/dist/config/config.js.map +1 -0
  11. package/dist/config/index.js +2 -0
  12. package/dist/config/index.js.map +1 -0
  13. package/dist/container.js +1 -0
  14. package/dist/container.js.map +1 -0
  15. package/dist/exceptions/extension-init-error.js +1 -0
  16. package/dist/exceptions/extension-init-error.js.map +1 -0
  17. package/dist/exceptions/extension-load-error.d.ts +1 -1
  18. package/dist/exceptions/extension-load-error.js +1 -0
  19. package/dist/exceptions/extension-load-error.js.map +1 -0
  20. package/dist/exceptions/extension-potential-circular.js +1 -0
  21. package/dist/exceptions/extension-potential-circular.js.map +1 -0
  22. package/dist/exceptions/harmony-already-running.js +1 -0
  23. package/dist/exceptions/harmony-already-running.js.map +1 -0
  24. package/dist/exceptions/harmony-error.js +1 -0
  25. package/dist/exceptions/harmony-error.js.map +1 -0
  26. package/dist/exceptions/hook-not-found.js +1 -0
  27. package/dist/exceptions/hook-not-found.js.map +1 -0
  28. package/dist/exceptions/index.js +7 -2
  29. package/dist/exceptions/index.js.map +1 -0
  30. package/dist/exceptions/runtime-not-defined.js +1 -1
  31. package/dist/exceptions/runtime-not-defined.js.map +1 -0
  32. package/dist/extension/any-extension.js +1 -0
  33. package/dist/extension/any-extension.js.map +1 -0
  34. package/dist/extension/decorator.d.ts +2 -2
  35. package/dist/extension/decorator.js +1 -0
  36. package/dist/extension/decorator.js.map +1 -0
  37. package/dist/extension/extension-manifest.js +1 -0
  38. package/dist/extension/extension-manifest.js.map +1 -0
  39. package/dist/extension/extension.d.ts +3 -2
  40. package/dist/extension/extension.js +4 -0
  41. package/dist/extension/extension.js.map +1 -0
  42. package/dist/extension/index.js +2 -0
  43. package/dist/extension/index.js.map +1 -0
  44. package/dist/extension-graph/extension-graph.d.ts +2 -2
  45. package/dist/extension-graph/extension-graph.js +5 -4
  46. package/dist/extension-graph/extension-graph.js.map +1 -0
  47. package/dist/extension-graph/from-extension.js +3 -2
  48. package/dist/extension-graph/from-extension.js.map +1 -0
  49. package/dist/extension-graph/index.js +6 -1
  50. package/dist/extension-graph/index.js.map +1 -0
  51. package/dist/factory/extension-factory.js +1 -0
  52. package/dist/factory/extension-factory.js.map +1 -0
  53. package/dist/factory/index.js +2 -0
  54. package/dist/factory/index.js.map +1 -0
  55. package/dist/fixtures/aspects/babel/babel.aspect.js +1 -0
  56. package/dist/fixtures/aspects/babel/babel.aspect.js.map +1 -0
  57. package/dist/fixtures/aspects/babel/babel.cli.js +1 -0
  58. package/dist/fixtures/aspects/babel/babel.cli.js.map +1 -0
  59. package/dist/fixtures/aspects/react/react.aspect.js +1 -0
  60. package/dist/fixtures/aspects/react/react.aspect.js.map +1 -0
  61. package/dist/fixtures/aspects/react/react.cli.js +2 -1
  62. package/dist/fixtures/aspects/react/react.cli.js.map +1 -0
  63. package/dist/fixtures/aspects/react/react.ui.js +1 -0
  64. package/dist/fixtures/aspects/react/react.ui.js.map +1 -0
  65. package/dist/fixtures/aspects/ui/ui.aspect.js +1 -0
  66. package/dist/fixtures/aspects/ui/ui.aspect.js.map +1 -0
  67. package/dist/fixtures/aspects/ui/ui.ui.js +1 -0
  68. package/dist/fixtures/aspects/ui/ui.ui.js.map +1 -0
  69. package/dist/fixtures/babel/babel.extension.d.ts +1 -1
  70. package/dist/fixtures/babel/babel.extension.js +8 -26
  71. package/dist/fixtures/babel/babel.extension.js.map +1 -0
  72. package/dist/fixtures/babel/index.js +1 -1
  73. package/dist/fixtures/babel/index.js.map +1 -0
  74. package/dist/fixtures/base-compiler/base-compiler.extension.js +5 -23
  75. package/dist/fixtures/base-compiler/base-compiler.extension.js.map +1 -0
  76. package/dist/fixtures/base-compiler/index.js +2 -0
  77. package/dist/fixtures/base-compiler/index.js.map +1 -0
  78. package/dist/fixtures/cli/cli.extension.js +5 -12
  79. package/dist/fixtures/cli/cli.extension.js.map +1 -0
  80. package/dist/fixtures/cli/command.js +1 -1
  81. package/dist/fixtures/cli/command.js.map +1 -0
  82. package/dist/fixtures/cli/index.js +2 -1
  83. package/dist/fixtures/cli/index.js.map +1 -0
  84. package/dist/fixtures/typescript/index.js +1 -1
  85. package/dist/fixtures/typescript/index.js.map +1 -0
  86. package/dist/fixtures/typescript/typescript.js +8 -22
  87. package/dist/fixtures/typescript/typescript.js.map +1 -0
  88. package/dist/harmony-config/config-reader.js +3 -2
  89. package/dist/harmony-config/config-reader.js.map +1 -0
  90. package/dist/harmony-config/exceptions/index.js +2 -0
  91. package/dist/harmony-config/exceptions/index.js.map +1 -0
  92. package/dist/harmony-config/exceptions/read-config-error.d.ts +1 -1
  93. package/dist/harmony-config/exceptions/read-config-error.js +1 -0
  94. package/dist/harmony-config/exceptions/read-config-error.js.map +1 -0
  95. package/dist/harmony-config/harmony-config.js +5 -4
  96. package/dist/harmony-config/harmony-config.js.map +1 -0
  97. package/dist/harmony-config/index.js +2 -0
  98. package/dist/harmony-config/index.js.map +1 -0
  99. package/dist/harmony-config/locator.js +1 -1
  100. package/dist/harmony-config/locator.js.map +1 -0
  101. package/dist/harmony.docs.mdx +250 -0
  102. package/dist/harmony.js +3 -2
  103. package/dist/harmony.js.map +1 -0
  104. package/dist/{factory/extension-factory.spec.d.ts → harmony.spec.old.d.ts} +0 -0
  105. package/dist/{harmony.spec.js → harmony.spec.old.js} +12 -23
  106. package/dist/harmony.spec.old.js.map +1 -0
  107. package/dist/index.js +6 -1
  108. package/dist/index.js.map +1 -0
  109. package/dist/readme.md +80 -0
  110. package/dist/runtimes/exceptions/index.js +2 -0
  111. package/dist/runtimes/exceptions/index.js.map +1 -0
  112. package/dist/runtimes/exceptions/runtime-module-error.d.ts +1 -1
  113. package/dist/runtimes/exceptions/runtime-module-error.js +1 -0
  114. package/dist/runtimes/exceptions/runtime-module-error.js.map +1 -0
  115. package/dist/runtimes/exceptions/runtime-not-defined.js +1 -0
  116. package/dist/runtimes/exceptions/runtime-not-defined.js.map +1 -0
  117. package/dist/runtimes/index.js +2 -0
  118. package/dist/runtimes/index.js.map +1 -0
  119. package/dist/runtimes/runtime-definition.js +1 -0
  120. package/dist/runtimes/runtime-definition.js.map +1 -0
  121. package/dist/runtimes/runtime-manifest.js +1 -0
  122. package/dist/runtimes/runtime-manifest.js.map +1 -0
  123. package/dist/runtimes/runtimes.js +1 -0
  124. package/dist/runtimes/runtimes.js.map +1 -0
  125. package/dist/slots/index.js +2 -0
  126. package/dist/slots/index.js.map +1 -0
  127. package/dist/slots/registry.js +1 -0
  128. package/dist/slots/registry.js.map +1 -0
  129. package/dist/slots/slot.js +1 -0
  130. package/dist/slots/slot.js.map +1 -0
  131. package/dist/types.js +1 -0
  132. package/dist/types.js.map +1 -0
  133. package/dist/utils/async-for-each.js +1 -0
  134. package/dist/utils/async-for-each.js.map +1 -0
  135. package/dist/utils/index.js +2 -0
  136. package/dist/utils/index.js.map +1 -0
  137. package/exceptions/extension-init-error.ts +1 -0
  138. package/exceptions/extension-load-error.ts +28 -0
  139. package/exceptions/extension-potential-circular.ts +24 -0
  140. package/exceptions/harmony-already-running.ts +1 -0
  141. package/exceptions/harmony-error.ts +1 -0
  142. package/exceptions/hook-not-found.ts +1 -0
  143. package/exceptions/index.ts +5 -0
  144. package/{dist/extension-graph/extension-graph.spec.d.ts → exceptions/runtime-not-defined.ts} +0 -0
  145. package/extension/any-extension.ts +8 -0
  146. package/extension/decorator.ts +126 -0
  147. package/extension/extension-manifest.ts +51 -0
  148. package/extension/extension.ts +142 -0
  149. package/extension/index.ts +3 -0
  150. package/extension-graph/extension-graph.ts +151 -0
  151. package/extension-graph/from-extension.ts +72 -0
  152. package/extension-graph/index.ts +1 -0
  153. package/factory/extension-factory.ts +8 -0
  154. package/factory/index.ts +1 -0
  155. package/fixtures/aspects/babel/babel.aspect.ts +14 -0
  156. package/fixtures/aspects/babel/babel.cli.ts +16 -0
  157. package/fixtures/aspects/react/react.aspect.ts +22 -0
  158. package/fixtures/aspects/react/react.cli.ts +26 -0
  159. package/fixtures/aspects/react/react.ui.ts +16 -0
  160. package/fixtures/aspects/ui/ui.aspect.ts +13 -0
  161. package/fixtures/aspects/ui/ui.ui.ts +11 -0
  162. package/fixtures/babel/babel.extension.ts +20 -0
  163. package/{dist/extension/extension.spec.d.ts → fixtures/babel/index.ts} +0 -0
  164. package/fixtures/base-compiler/base-compiler.extension.ts +31 -0
  165. package/fixtures/base-compiler/index.ts +1 -0
  166. package/fixtures/cli/cli.extension.ts +25 -0
  167. package/fixtures/cli/command.ts +6 -0
  168. package/fixtures/cli/index.ts +1 -0
  169. package/fixtures/typescript/index.ts +0 -0
  170. package/fixtures/typescript/typescript.ts +20 -0
  171. package/harmony-config/config-reader.ts +17 -0
  172. package/harmony-config/exceptions/index.ts +1 -0
  173. package/harmony-config/exceptions/read-config-error.ts +9 -0
  174. package/harmony-config/harmony-config.ts +49 -0
  175. package/harmony-config/index.ts +1 -0
  176. package/harmony-config/locator.ts +0 -0
  177. package/harmony.docs.mdx +250 -0
  178. package/harmony.spec.old.ts +177 -0
  179. package/harmony.ts +146 -0
  180. package/index.ts +10 -0
  181. package/package-tar/teambit-harmony-0.3.1.tgz +0 -0
  182. package/package.json +42 -28
  183. package/preview-1649151063005.js +1 -0
  184. package/readme.md +58 -137
  185. package/runtimes/exceptions/index.ts +2 -0
  186. package/runtimes/exceptions/runtime-module-error.ts +9 -0
  187. package/runtimes/exceptions/runtime-not-defined.ts +5 -0
  188. package/runtimes/index.ts +3 -0
  189. package/runtimes/runtime-definition.ts +32 -0
  190. package/runtimes/runtime-manifest.ts +12 -0
  191. package/runtimes/runtimes.ts +34 -0
  192. package/slots/index.ts +2 -0
  193. package/slots/registry.ts +36 -0
  194. package/slots/slot.ts +11 -0
  195. package/tsconfig.json +26 -0
  196. package/types/asset.d.ts +29 -0
  197. package/types/style.d.ts +42 -0
  198. package/types.ts +7 -0
  199. package/utils/async-for-each.ts +10 -0
  200. package/utils/index.ts +1 -0
  201. package/dist/extension/extension.spec.js +0 -1
  202. package/dist/extension-graph/extension-graph.spec.js +0 -1
  203. package/dist/factory/extension-factory.spec.js +0 -6
  204. package/dist/harmony-config/harmony-config.spec.d.ts +0 -1
  205. package/dist/harmony-config/harmony-config.spec.js +0 -8
  206. package/dist/harmony.spec.d.ts +0 -1
@@ -0,0 +1,49 @@
1
+ import { stringify, assign } from 'comment-json';
2
+ import { join } from 'path';
3
+ import { readConfigFile } from './config-reader';
4
+
5
+ const userHome = require('user-home');
6
+
7
+ export type ConfigOptions = {
8
+ cwd?: string;
9
+ global?: GlobalConfigOpts;
10
+ shouldThrow?: boolean;
11
+ };
12
+
13
+ export type GlobalConfigOpts = {
14
+ dir?: string;
15
+ name: string;
16
+ };
17
+
18
+ const defaultConfig = {
19
+ cwd: process.cwd(),
20
+ shouldThrow: true,
21
+ };
22
+
23
+ export class HarmonyConfig {
24
+ constructor(private raw: Record<string, any>) {}
25
+
26
+ toObject() {
27
+ return this.raw;
28
+ }
29
+
30
+ toString() {
31
+ return stringify(this.raw);
32
+ }
33
+
34
+ static load(fileName: string, opts?: ConfigOptions) {
35
+ const mergedOpts = Object.assign(defaultConfig, opts);
36
+ const config = readConfigFile(join(mergedOpts.cwd, fileName), mergedOpts.shouldThrow);
37
+
38
+ if (mergedOpts.global) {
39
+ return HarmonyConfig.loadGlobal(mergedOpts.global, config);
40
+ }
41
+
42
+ return new HarmonyConfig(config);
43
+ }
44
+
45
+ static loadGlobal(globalOpts: GlobalConfigOpts, config: any = {}) {
46
+ const globalConfig = readConfigFile(join(globalOpts.dir || userHome, globalOpts.name), false);
47
+ return new HarmonyConfig(assign(config, globalConfig));
48
+ }
49
+ }
@@ -0,0 +1 @@
1
+ export { HarmonyConfig as Config } from './harmony-config';
File without changes
@@ -0,0 +1,250 @@
1
+ ---
2
+ description: Harmony is an abstract extension system you can use to make any software extendable and composable. It is the engine that drives Bit extensibility and composability.
3
+ labels: ['aspect', 'composition', 'extendability']
4
+ ---
5
+
6
+ ## Basic features
7
+
8
+ Harmony allows you to implement Aspects as the main building blocks that compose your software. Each Aspect has:
9
+
10
+ * Dependency declaration - Where you declare other Aspects as dependencies.
11
+ * Dependency injection - An Aspect gets providers for all its dependencies.
12
+ * Slots - An easy way for an Aspect to provide API for another aspect to hook into any of its processes.
13
+ * Multiple runtimes - Aspect can annotate in which runtimes it can run (for example providing functionality to run on the server and on the browser).
14
+
15
+ ## Harmony config file
16
+
17
+ Use a config file to describe your desired composition of Aspects and their configuration. Harmony uses a basic JSON syntax, where each key is an aspect.
18
+
19
+ ```json
20
+ {
21
+ "teambit.dependencies/dependency-resolver": {}, // load an Aspect without providing configuration
22
+ "teambit.workspace/workspace": { // Load an Aspect with configuration
23
+ "name": "bit",
24
+ "icon": "https://static.bit.dev/bit-logo.svg",
25
+ }
26
+ ```
27
+
28
+ ## Define an aspect
29
+
30
+ Harmony Aspect is an object that implements the `ExtensionManifest` interface
31
+
32
+ ```js
33
+ export type ExtensionManifest = {
34
+ /**
35
+ * Aspect's name.
36
+ */
37
+ name: string;
38
+
39
+ /**
40
+ * Aspect unique ID.
41
+ */
42
+ id?: string;
43
+
44
+ /**
45
+ * Array of dependencies.
46
+ * These other Aspects to be installed and resolved prior to this Aspect activation.
47
+ */
48
+ dependencies?: ExtensionManifest[];
49
+
50
+ /**
51
+ * Reference to the Aspect factory function.
52
+ */
53
+ provider?: ProviderFn;
54
+
55
+ /**
56
+ * Default Aspect configuration.
57
+ */
58
+ defaultConfig?: object;
59
+
60
+ /**
61
+ * Alias to the provider.
62
+ */
63
+ provide?: ProviderFn;
64
+
65
+ /**
66
+ * Array of slots the Aspect exposes.
67
+ */
68
+ slots?: SlotProvider<unknown>[],
69
+
70
+ /**
71
+ * Additional keys which might be expected by other Aspects.
72
+ */
73
+ [key: string]: any;
74
+ }
75
+ ```
76
+
77
+ ## Programmatic API
78
+
79
+ ### Static API
80
+
81
+ ```js
82
+ // Create instance of harmony with the provided aspects, on specific runtime, with provided config
83
+ static async load(aspects: Aspect[], runtime: string, globalConfig: GlobalConfig) {
84
+ ```
85
+
86
+ ### Instance API
87
+
88
+ ```js
89
+ // load an Aspect into the dependency graph
90
+ async load(extensions: ExtensionManifest[])
91
+
92
+ // set extensions during Harmony runtime.
93
+ async set(extensions: ExtensionManifest[])
94
+
95
+ export type RequireFn = (aspect: Extension, runtime: RuntimeDefinition) => Promise<void>;
96
+
97
+ // Start harmony.
98
+ // The require function is a function that get's the aspect and its runtime, and knows to require/load it
99
+ async run(requireFn?: RequireFn)
100
+ ```
101
+
102
+ ## Assumptions
103
+
104
+ * Harmony does not allow circular dependencies between Aspects.
105
+
106
+ <!--
107
+
108
+ -------------------------for reference only - to remove ------------------
109
+
110
+ ## composition model
111
+ Harmony proposes a graph composition model.
112
+ composition model should allow:
113
+
114
+ - full control over composition including dependency composition.
115
+ - easy overrides mechanism for configs.
116
+ - full encapsulation of an extension.
117
+
118
+ bit.config.js
119
+ ```js
120
+ import FlowSchemaPlugin from '@bit/plugins.flow-schema';
121
+
122
+ export default () => {
123
+ return [ // returns an array of PluginInstance
124
+ [FlowSchemaPlugin, {
125
+
126
+ }],
127
+ [ReactSchemaPlugin, {
128
+ eslintrc: './.eslintrc'
129
+ }]
130
+ ];
131
+ }
132
+ ```
133
+
134
+ configured extensions in bit.json
135
+ ```json
136
+ {
137
+ "extensions": {
138
+ "bit.envs/eslint@0.0.4": {
139
+ "eslintrc": "./.eslintrc"
140
+ },
141
+ "bit.envs/babel@0.0.1": {
142
+ "babelrc": "./.babelrc",
143
+ "strict": true,
144
+ "__alias": "compile"
145
+ },
146
+ "bit.envs/webpack@0.0.1": {
147
+ "config": "./webpack.config.js",
148
+ "mode": "prod",
149
+ "__alias": "bundle"
150
+ },
151
+ "bit.envs/mocha@0.0.1": {
152
+ "reporter": "json",
153
+ "mochaOptions": "./mocha.opts",
154
+ "__alias": "test-mocha"
155
+ }
156
+ },
157
+ "pipes": {
158
+ "tag": [""]
159
+ },
160
+ "dist": {
161
+ "target": "",
162
+ "entry": ""
163
+ }
164
+ }
165
+ ```
166
+
167
+ ### Open questions
168
+ - How do I install a Bit extensions? Where the component is configured?
169
+ - Is the extension installed with Bit/NPM?
170
+ - What's the impl. behind `bit use <plugin/extension name>`
171
+ - maybe an composition model can co-exist as json and js? what are the tradeoffs?
172
+
173
+ ## extension registration
174
+ ```js
175
+ import { register } from 'harmony';
176
+ import { DocGen } from '@bit/bit.exts.doc-gen';
177
+
178
+ const extensions = register([DocGen]);
179
+ ```
180
+
181
+ ## lifecycle event invocation
182
+
183
+ ```js
184
+ import { invoke } from 'harmony';
185
+
186
+ invoke('tag', ...data);
187
+ ```
188
+
189
+ ## lifecycle event registration
190
+ ```js
191
+ import { Lifecycle } from 'harmony';
192
+
193
+ @Lifecycle(Tag);
194
+ function tag() {
195
+
196
+ }
197
+ ```
198
+
199
+ ## state / schema management
200
+ ```ts
201
+ @Lifecycle(Tag)
202
+ function tag(component: Component) {
203
+ const docs = component.get('docs'); // returns `Maybe` type?
204
+ const docs: Docs = docs.get();
205
+ }
206
+ ```
207
+
208
+ ## function extension
209
+
210
+ ```js
211
+ export default function foo() {
212
+ context.a
213
+ }
214
+ ```
215
+
216
+ ## configuration
217
+ an extension can declaratively ask for configuration type. this can be reflected
218
+ - extensions can be configured during instantiation.
219
+ - configuration types can be built from multiple sources
220
+
221
+ ## context
222
+ - standard context can be shared between all extensions in the same instance.
223
+
224
+ ## extension composition
225
+ - hook invocation from an extension
226
+ - extension dependencies? how can an extension declare a dependency as part of its execution?
227
+ - contextual/namespaced hooks
228
+ - config api
229
+
230
+ ## extension resolution
231
+ ```js
232
+ import { resolve } from 'harmony';
233
+
234
+ const extension = resolve('doc-gen');
235
+ ```
236
+
237
+ ## Questions (?)
238
+ - can extensions be added/configured during runtime? DI
239
+ - is there a difference between extension composition to registration?
240
+ - how to make autocomplete work for hooks?
241
+ - how to avoid unintentional hook invocation (decorators?)
242
+ - how to declare a new hook..
243
+ - schema validator
244
+ - dev experience of an extension.
245
+ - how do manage different runtime environments? should I? Why is that relevant?
246
+ - runtime capsules?
247
+ - how can one extension run from both server and client? how capsule is related?
248
+ - in which process extensions will run from? also, what will happen from the backend?
249
+ -
250
+ -->
@@ -0,0 +1,177 @@
1
+ import { Harmony } from './harmony';
2
+ // import { BitExt } from '../../bit/dist/extensions/bit';
3
+ // import { BaseCompiler } from './fixtures/base-compiler';
4
+ // import { TypeScript } from './fixtures/typescript/typescript';
5
+ // import { Babel } from './fixtures/babel/babel.extension';
6
+ // import { Slot, SlotRegistry } from './slots';
7
+ // import { start } from 'repl';
8
+ // import { ExtensionManifest } from './extension';
9
+ import { ReactAspect } from './fixtures/aspects/react/react.aspect';
10
+ import { ReactCLI } from './fixtures/aspects/react/react.cli';
11
+ import { ReactUI } from './fixtures/aspects/react/react.ui';
12
+
13
+ describe('Harmony', () => {
14
+ describe('run()', () => {
15
+ // it('should return a string', async () => {
16
+ // const manifest = {
17
+ // name: 'HelloWorld',
18
+ // provider: async () => 'hello world'
19
+ // };
20
+ // await harmony.run(manifest);
21
+ // expect(harmony.get('HelloWorld')).eql('hello world');
22
+ // });
23
+
24
+ // it('should load an array of different extensions', async () => {
25
+ // await harmony.run([BaseCompiler, TypeScript]);
26
+ // const compiler = harmony.get<BaseCompiler>('BaseCompiler')
27
+ // const typescript = harmony.get<TypeScript>('typescript')
28
+
29
+ // expect(typescript.compile()).to.eq('hello world');
30
+ // expect(compiler.compile()).to.eq('hello world');
31
+ // });
32
+
33
+ // it('should load extensions with slots', async () => {
34
+ // class Env {
35
+ // build () {
36
+ // return 'react built';
37
+ // }
38
+ // }
39
+
40
+ // it('extension instance should include an ID', async () => {
41
+ // const React = {
42
+ // name: '@teambit/react',
43
+ // dependencies: [],
44
+ // provide: async () => {}
45
+ // };
46
+
47
+ // await harmony.run([React]);
48
+ // const react = harmony.get<any>(React.name);
49
+ // expect(react.id).to.eq(React.name);
50
+ // });
51
+
52
+ // const Envs: ExtensionManifest = {
53
+ // name: '@teambit/envs',
54
+ // slots: [Slot.withType<Env>()],
55
+ // provider: async ([], config: {env: string}, [envSlot]: [SlotRegistry<Env>]) => {
56
+ // return {
57
+ // register: (env: Env) => {
58
+ // envSlot.register(env);
59
+ // },
60
+
61
+ // start() {
62
+ // const instance = envSlot.get(config.env);
63
+ // if (!instance) throw new Error('could not find envs');
64
+ // return instance.build();
65
+ // }
66
+ // };
67
+ // }
68
+ // };
69
+
70
+ // const React = {
71
+ // name: '@teambit/react',
72
+ // dependencies: [Envs],
73
+ // provide: async ([envs]: [{ register: (env: Env) => void }], config: {}) => {
74
+ // envs.register(new Env());
75
+ // }
76
+ // };
77
+
78
+ // const harmony = Harmony.load([Envs, React], {
79
+ // '@teambit/envs': {
80
+ // env: '@teambit/react'
81
+ // }
82
+ // });
83
+
84
+ // await harmony.run([Envs, React]);
85
+
86
+ // const envs = harmony.get<any>(Envs.name);
87
+ // expect(envs.start()).to.eq('react built');
88
+ // });
89
+
90
+ // it('should load extensions with config', async () => {
91
+ // const e1 = {
92
+ // name: '@teambit/typescript',
93
+
94
+ // provider: async ([], config: { declarations: boolean }) => {
95
+ // return {
96
+ // get: () => {
97
+ // return config.declarations || false
98
+ // }
99
+ // };
100
+ // }
101
+ // };
102
+
103
+ // const e2 = { name: '@teambit/react', defaultConfig: {ts: false}, provide: async ([], config: any) => {
104
+ // return {
105
+ // config: config.ts
106
+ // };
107
+ // }
108
+ // };
109
+
110
+ // const harmony = Harmony.load([e1], {
111
+ // '@teambit/typescript': {
112
+ // declarations: true
113
+ // },
114
+ // });
115
+ // await harmony.run([e1]);
116
+
117
+ // const ts = harmony.get<{get: () => {}}>('@teambit/typescript');
118
+ // expect(ts.get()).to.eq(true);
119
+
120
+ // harmony.config.set('@teambit/react', {ts: true});
121
+ // await harmony.set([e2]);
122
+ // const react = harmony.get<{[key: string]: object}>('@teambit/react');
123
+ // expect(react.config).to.eq(true);
124
+ // });
125
+
126
+ // it('should invoke a class extension method', async () => {
127
+ // await harmony.run(BaseCompiler);
128
+ // const compiler = harmony.get<BaseCompiler>('BaseCompiler')
129
+ // expect(compiler.compile()).to.eq('hello world');
130
+ // });
131
+
132
+ // it('should invoke a class extension with configured dependencies', async () => {
133
+ // await harmony.run(TypeScript);
134
+ // const typescript = harmony.get<TypeScript>('typescript')
135
+ // expect(typescript.compile()).to.eq('hello world');
136
+ // });
137
+
138
+ // it('should use prefer id over name', async () => {
139
+ // const dependency = {
140
+ // name: 'dependencyName',
141
+ // id: 'dependencyId',
142
+ // provider: async () => 'hello world'
143
+ // };
144
+ // const dependent = {
145
+ // name: 'dependentName',
146
+ // id: 'dependentId',
147
+ // dependencies: [dependency],
148
+ // provider: async () => 'hello world'
149
+ // };
150
+ // await harmony.run(dependent);
151
+ // const ids = harmony.extensionsIds;
152
+ // expect(ids).to.contain('dependencyId');
153
+ // expect(ids).to.contain('dependentId');
154
+ // });
155
+
156
+ // it('should execute bit core extension graph', async () => {
157
+ // // const bit = await Harmony.run(BitExt);
158
+ // });
159
+
160
+ // it('should register during run', async () => {
161
+ // const compiler = await harmony.run(Babel);
162
+ // });
163
+
164
+ // it('should execute an aspect in the cli runtime', async() => {
165
+ // const harmony = await Harmony.load([ReactAspect], 'cli', {});
166
+ // await harmony.run();
167
+ // const react = harmony.get<ReactCLI>('@teambit/react');
168
+ // })
169
+
170
+ // it('should execute an aspect in the ui runtime', async() => {
171
+ // const harmony = await Harmony.load([ReactAspect], 'ui', {});
172
+ // await harmony.run();
173
+ // const react = harmony.get<ReactUI>('@teambit/react');
174
+ // expect(react.render()).toBeTruthy;
175
+ // })
176
+ });
177
+ });
package/harmony.ts ADDED
@@ -0,0 +1,146 @@
1
+ import 'reflect-metadata';
2
+ import ExtensionGraph from './extension-graph/extension-graph';
3
+ import { ExtensionLoadError } from './exceptions';
4
+ import { Extension, ExtensionManifest } from './extension';
5
+ import { asyncForEach } from './utils';
6
+ import { Config } from './config';
7
+ import { Aspect } from './aspect';
8
+ import { Runtimes } from './runtimes/runtimes';
9
+ import { RuntimeDefinition } from './runtimes/runtime-definition';
10
+ import { RuntimeNotDefined } from './runtimes/exceptions';
11
+
12
+ export type GlobalConfig = {
13
+ [key: string]: object
14
+ };
15
+
16
+ export type RequireFn = (aspect: Extension, runtime: RuntimeDefinition) => Promise<void>;
17
+
18
+ export class Harmony {
19
+ constructor(
20
+ /**
21
+ * extension graph
22
+ */
23
+ readonly graph: ExtensionGraph,
24
+
25
+ /**
26
+ * harmony top level config
27
+ */
28
+ readonly config: Config,
29
+
30
+ readonly runtimes: Runtimes,
31
+
32
+ readonly activeRuntime: string
33
+ ) {}
34
+
35
+ public current: string|null = null;
36
+
37
+ private runtime: RuntimeDefinition | undefined;
38
+
39
+ /**
40
+ * list all registered extensions
41
+ */
42
+ get extensions() {
43
+ return this.graph.nodes;
44
+ }
45
+
46
+ /**
47
+ * list all registered extensions ids
48
+ */
49
+ get extensionsIds() {
50
+ return [...this.graph.nodes.keys()];
51
+ }
52
+
53
+ /**
54
+ * load an Aspect into the dependency graph.
55
+ */
56
+ async load(extensions: ExtensionManifest[]) {
57
+ return this.set(extensions);
58
+ }
59
+
60
+ /**
61
+ * set extensions during Harmony runtime.
62
+ * hack!
63
+ */
64
+ async set(extensions: ExtensionManifest[]) {
65
+ this.graph.load(extensions);
66
+ // Only load new extensions and their dependencies
67
+ const extensionsToLoad = extensions.map((ext) => {
68
+ // @ts-ignore
69
+ return Reflect.getMetadata('harmony:name', ext) || ext.id || ext.name;
70
+ });
71
+
72
+ // @ts-ignore
73
+ await this.graph.enrichRuntime(this.runtime, this.runtimes, () => {});
74
+ // @ts-ignore
75
+ const subgraphs = this.graph.successorsSubgraph(extensionsToLoad);
76
+ if (subgraphs) {
77
+ const executionOrder = subgraphs.toposort(true);
78
+ await asyncForEach(executionOrder, async (ext: Extension) => {
79
+ if (!this.runtime) throw new RuntimeNotDefined(this.activeRuntime);
80
+ await this.runOne(ext, this.runtime);
81
+ });
82
+ }
83
+ }
84
+
85
+ private async runOne(extension: Extension, runtime: RuntimeDefinition) {
86
+ if (extension.loaded) return;
87
+ // create an index of all vertices in dependency graph
88
+ const deps = this.graph.getRuntimeDependencies(extension, runtime);
89
+ const instances = deps.map(extension => extension.instance);
90
+
91
+ try {
92
+ return extension.__run(instances, this, runtime);
93
+ } catch (err) {
94
+ throw new ExtensionLoadError(extension, err);
95
+ }
96
+ }
97
+
98
+ getDependencies(aspect: Extension) {
99
+ if (!this.runtime) throw new RuntimeNotDefined(this.activeRuntime);
100
+ return this.graph.getRuntimeDependencies(aspect, this.runtime);
101
+ }
102
+
103
+ initExtension(id: string) {
104
+ this.current = id;
105
+ }
106
+
107
+ endExtension() {
108
+ this.current = null;
109
+ }
110
+
111
+ /**
112
+ * get an extension from harmony.
113
+ */
114
+ get<T>(id: string): T {
115
+ const extension = this.graph.get(id);
116
+ if (!extension || !extension.instance) throw new Error(`failed loading extension ${id}`);
117
+ return extension.instance;
118
+ }
119
+
120
+ resolveRuntime(name: string): RuntimeDefinition {
121
+ return this.runtimes.get(name);
122
+ }
123
+
124
+ async run(requireFn?: RequireFn) {
125
+ const runtime = this.resolveRuntime(this.activeRuntime);
126
+ this.runtime = runtime;
127
+ const defaultRequireFn: RequireFn = async (aspect: Extension, runtime: RuntimeDefinition) => {
128
+ const runtimeFile = runtime.getRuntimeFile(aspect.files);
129
+ if (!runtimeFile) return;
130
+ // runtime.require(runtimeFile);
131
+ };
132
+ // requireFn ? await requireFn(aspect, runtime) : defaultRequireFn(this.graph);
133
+ await this.graph.enrichRuntime(runtime, this.runtimes, requireFn || defaultRequireFn);
134
+
135
+ const executionOrder = this.graph.byExecutionOrder();
136
+ await asyncForEach(executionOrder, async (ext: Extension) => {
137
+ await this.runOne(ext, runtime);
138
+ });
139
+ }
140
+
141
+ static async load(aspects: Aspect[], runtime: string, globalConfig: GlobalConfig) {
142
+ const aspectGraph = ExtensionGraph.from(aspects as any);
143
+ const runtimes = await Runtimes.load(aspectGraph);
144
+ return new Harmony(aspectGraph, Config.from(globalConfig), runtimes, runtime);
145
+ }
146
+ }
package/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export { ExtensionDecorator as Extension, register, HookRegistry, hook, provider, createHook } from './extension';
2
+ export { Harmony } from './harmony';
3
+ export { Slot, SlotRegistry } from './slots';
4
+ export { ProviderFn } from './types';
5
+ export { ExtensionManifest } from './extension/extension-manifest';
6
+ export { HarmonyError } from './exceptions/harmony-error';
7
+ export { Aspect } from './aspect';
8
+ export { RuntimeDefinition, RuntimeManifest } from './runtimes';
9
+ export { default as AspectGraph } from './extension-graph/extension-graph';
10
+ export { GlobalConfig } from './harmony';