@teambit/harmony.testing.load-aspect 0.0.0-00ab0af77b86da7ad83d7a3df85678ea5368dacf

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.
@@ -0,0 +1,113 @@
1
+ [
2
+ "teambit.harmony/aspect-loader",
3
+ "teambit.harmony/cli",
4
+ "teambit.component/dev-files",
5
+ "teambit.workspace/workspace",
6
+ "teambit.workspace/workspace-config-files",
7
+ "teambit.workspace/install",
8
+ "teambit.defender/eslint",
9
+ "teambit.defender/prettier",
10
+ "teambit.compilation/compiler",
11
+ "teambit.defender/linter",
12
+ "teambit.defender/formatter",
13
+ "teambit.defender/validator",
14
+ "teambit.component/component",
15
+ "teambit.mdx/mdx",
16
+ "teambit.mdx/readme",
17
+ "teambit.preview/preview",
18
+ "teambit.component/component-sizer",
19
+ "teambit.docs/docs",
20
+ "teambit.dependencies/yarn",
21
+ "teambit.compositions/compositions",
22
+ "teambit.harmony/global-config",
23
+ "teambit.harmony/graphql",
24
+ "teambit.dependencies/pnpm",
25
+ "teambit.compilation/multi-compiler",
26
+ "teambit.ui-foundation/ui",
27
+ "teambit.generator/generator",
28
+ "teambit.envs/envs",
29
+ "teambit.envs/env",
30
+ "teambit.component/graph",
31
+ "teambit.harmony/pubsub",
32
+ "teambit.dependencies/dependency-resolver",
33
+ "teambit.explorer/insights",
34
+ "teambit.component/isolator",
35
+ "teambit.harmony/logger",
36
+ "teambit.pkg/pkg",
37
+ "teambit.react/react",
38
+ "teambit.vue/vue-aspect",
39
+ "teambit.harmony/worker",
40
+ "teambit.scope/scope",
41
+ "teambit.defender/tester",
42
+ "teambit.defender/multi-tester",
43
+ "teambit.pipelines/builder",
44
+ "teambit.workspace/variants",
45
+ "teambit.component/deprecation",
46
+ "teambit.component/internalize",
47
+ "teambit.harmony/express",
48
+ "teambit.harmony/aspect",
49
+ "teambit.webpack/webpack",
50
+ "teambit.semantics/schema",
51
+ "teambit.ui-foundation/react-router",
52
+ "teambit.typescript/typescript",
53
+ "teambit.ui-foundation/panels",
54
+ "teambit.compilation/babel",
55
+ "teambit.harmony/node",
56
+ "teambit.ui-foundation/notifications",
57
+ "teambit.compilation/bundler",
58
+ "teambit.defender/jest",
59
+ "teambit.harmony/cache",
60
+ "teambit.component/changelog",
61
+ "teambit.component/code",
62
+ "teambit.explorer/command-bar",
63
+ "teambit.ui-foundation/sidebar",
64
+ "teambit.component/component-tree",
65
+ "teambit.scope/export",
66
+ "teambit.scope/importer",
67
+ "teambit.ui-foundation/harmony-ui-app",
68
+ "teambit.ui-foundation/user-agent",
69
+ "teambit.harmony/application",
70
+ "teambit.workspace/eject",
71
+ "teambit.lanes/lanes",
72
+ "teambit.component/forking",
73
+ "teambit.component/renaming",
74
+ "teambit.component/new-component-helper",
75
+ "teambit.component/component-log",
76
+ "teambit.bit/clear-cache",
77
+ "teambit.defender/mocha",
78
+ "teambit.harmony/diagnostic",
79
+ "teambit.component/status",
80
+ "teambit.community/community",
81
+ "teambit.cloud/cloud",
82
+ "teambit.component/snapping",
83
+ "teambit.component/merging",
84
+ "teambit.component/issues",
85
+ "teambit.component/refactoring",
86
+ "teambit.component/component-compare",
87
+ "teambit.component/lister",
88
+ "teambit.dependencies/dependencies",
89
+ "teambit.component/remove",
90
+ "teambit.lanes/merge-lanes",
91
+ "teambit.component/checkout",
92
+ "teambit.component/component-writer",
93
+ "teambit.api-reference/api-reference",
94
+ "teambit.harmony/api-server",
95
+ "teambit.component/tracker",
96
+ "teambit.component/mover",
97
+ "teambit.workspace/watcher",
98
+ "teambit.component/stash",
99
+ "teambit.git/git",
100
+ "teambit.harmony/ipc-events",
101
+ "teambit.workspace/config-merger",
102
+ "teambit.scope/version-history",
103
+ "teambit.harmony/host-initializer",
104
+ "teambit.harmony/doctor",
105
+ "teambit.scope/objects",
106
+ "teambit.harmony/config-store",
107
+ "teambit.mcp/cli-mcp-server",
108
+ "teambit.git/ci",
109
+ "teambit.cloud/ripple",
110
+ "teambit.workspace/scripts",
111
+ "teambit.harmony/bit",
112
+ "teambit.harmony/config"
113
+ ]
@@ -0,0 +1,113 @@
1
+ [
2
+ "teambit.harmony/aspect-loader",
3
+ "teambit.harmony/cli",
4
+ "teambit.component/dev-files",
5
+ "teambit.workspace/workspace",
6
+ "teambit.workspace/workspace-config-files",
7
+ "teambit.workspace/install",
8
+ "teambit.defender/eslint",
9
+ "teambit.defender/prettier",
10
+ "teambit.compilation/compiler",
11
+ "teambit.defender/linter",
12
+ "teambit.defender/formatter",
13
+ "teambit.defender/validator",
14
+ "teambit.component/component",
15
+ "teambit.mdx/mdx",
16
+ "teambit.mdx/readme",
17
+ "teambit.preview/preview",
18
+ "teambit.component/component-sizer",
19
+ "teambit.docs/docs",
20
+ "teambit.dependencies/yarn",
21
+ "teambit.compositions/compositions",
22
+ "teambit.harmony/global-config",
23
+ "teambit.harmony/graphql",
24
+ "teambit.dependencies/pnpm",
25
+ "teambit.compilation/multi-compiler",
26
+ "teambit.ui-foundation/ui",
27
+ "teambit.generator/generator",
28
+ "teambit.envs/envs",
29
+ "teambit.envs/env",
30
+ "teambit.component/graph",
31
+ "teambit.harmony/pubsub",
32
+ "teambit.dependencies/dependency-resolver",
33
+ "teambit.explorer/insights",
34
+ "teambit.component/isolator",
35
+ "teambit.harmony/logger",
36
+ "teambit.pkg/pkg",
37
+ "teambit.react/react",
38
+ "teambit.vue/vue-aspect",
39
+ "teambit.harmony/worker",
40
+ "teambit.scope/scope",
41
+ "teambit.defender/tester",
42
+ "teambit.defender/multi-tester",
43
+ "teambit.pipelines/builder",
44
+ "teambit.workspace/variants",
45
+ "teambit.component/deprecation",
46
+ "teambit.component/internalize",
47
+ "teambit.harmony/express",
48
+ "teambit.harmony/aspect",
49
+ "teambit.webpack/webpack",
50
+ "teambit.semantics/schema",
51
+ "teambit.ui-foundation/react-router",
52
+ "teambit.typescript/typescript",
53
+ "teambit.ui-foundation/panels",
54
+ "teambit.compilation/babel",
55
+ "teambit.harmony/node",
56
+ "teambit.ui-foundation/notifications",
57
+ "teambit.compilation/bundler",
58
+ "teambit.defender/jest",
59
+ "teambit.harmony/cache",
60
+ "teambit.component/changelog",
61
+ "teambit.component/code",
62
+ "teambit.explorer/command-bar",
63
+ "teambit.ui-foundation/sidebar",
64
+ "teambit.component/component-tree",
65
+ "teambit.scope/export",
66
+ "teambit.scope/importer",
67
+ "teambit.ui-foundation/harmony-ui-app",
68
+ "teambit.ui-foundation/user-agent",
69
+ "teambit.harmony/application",
70
+ "teambit.workspace/eject",
71
+ "teambit.lanes/lanes",
72
+ "teambit.component/forking",
73
+ "teambit.component/renaming",
74
+ "teambit.component/new-component-helper",
75
+ "teambit.component/component-log",
76
+ "teambit.bit/clear-cache",
77
+ "teambit.defender/mocha",
78
+ "teambit.harmony/diagnostic",
79
+ "teambit.component/status",
80
+ "teambit.community/community",
81
+ "teambit.cloud/cloud",
82
+ "teambit.component/snapping",
83
+ "teambit.component/merging",
84
+ "teambit.component/issues",
85
+ "teambit.component/refactoring",
86
+ "teambit.component/component-compare",
87
+ "teambit.component/lister",
88
+ "teambit.dependencies/dependencies",
89
+ "teambit.component/remove",
90
+ "teambit.lanes/merge-lanes",
91
+ "teambit.component/checkout",
92
+ "teambit.component/component-writer",
93
+ "teambit.api-reference/api-reference",
94
+ "teambit.harmony/api-server",
95
+ "teambit.component/tracker",
96
+ "teambit.component/mover",
97
+ "teambit.workspace/watcher",
98
+ "teambit.component/stash",
99
+ "teambit.git/git",
100
+ "teambit.harmony/ipc-events",
101
+ "teambit.workspace/config-merger",
102
+ "teambit.scope/version-history",
103
+ "teambit.harmony/host-initializer",
104
+ "teambit.harmony/doctor",
105
+ "teambit.scope/objects",
106
+ "teambit.harmony/config-store",
107
+ "teambit.mcp/cli-mcp-server",
108
+ "teambit.git/ci",
109
+ "teambit.cloud/ripple",
110
+ "teambit.workspace/scripts",
111
+ "teambit.harmony/bit",
112
+ "teambit.harmony/config"
113
+ ]
@@ -0,0 +1 @@
1
+ export { loadAspect, loadManyAspects, getConfig } from './load-aspect';
package/dist/index.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "getConfig", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _loadAspect().getConfig;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "loadAspect", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _loadAspect().loadAspect;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "loadManyAspects", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _loadAspect().loadManyAspects;
22
+ }
23
+ });
24
+ function _loadAspect() {
25
+ const data = require("./load-aspect");
26
+ _loadAspect = function () {
27
+ return data;
28
+ };
29
+ return data;
30
+ }
31
+
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_loadAspect","data","require"],"sources":["index.ts"],"sourcesContent":["export { loadAspect, loadManyAspects, getConfig } from './load-aspect';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAAA,YAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,WAAA,YAAAA,CAAA;IAAA,OAAAC,IAAA;EAAA;EAAA,OAAAA,IAAA;AAAA","ignoreList":[]}
@@ -0,0 +1,19 @@
1
+ import type { Aspect } from '@teambit/harmony';
2
+ import { Harmony } from '@teambit/harmony';
3
+ import { Config } from '@teambit/harmony/harmony-config';
4
+ /**
5
+ * ! important ! prefer using `loadManyAspects` instead of this function. otherwise, you may end up with
6
+ * different instances of "Workspace" aspect for example for each one of the aspects you load.
7
+ *
8
+ * to make this work, export the main also as default (e.g. `export default LanesMain;`).
9
+ * otherwise, it'll show an error "TypeError: Cannot read property 'runtime' of undefined".
10
+ */
11
+ export declare function loadAspect<T>(targetAspect: Aspect, cwd?: string, runtime?: string): Promise<T>;
12
+ /**
13
+ * returns an instance of Harmony. with this instance, you can get any aspect you loaded (or its dependencies).
14
+ * e.g. `const workspace = harmony.get<Workspace>(WorkspaceAspect.id);`
15
+ * when used for tests, specify all aspects you need and call it once. this way, you make sure all of them are in sync
16
+ * and use the same instances of each other.
17
+ */
18
+ export declare function loadManyAspects(targetAspects: Aspect[], cwd?: string, runtime?: string): Promise<Harmony>;
19
+ export declare function getConfig(cwd?: string): Promise<Config>;
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getConfig = getConfig;
7
+ exports.loadAspect = loadAspect;
8
+ exports.loadManyAspects = loadManyAspects;
9
+ function _path() {
10
+ const data = require("path");
11
+ _path = function () {
12
+ return data;
13
+ };
14
+ return data;
15
+ }
16
+ function _legacy() {
17
+ const data = require("@teambit/legacy.consumer");
18
+ _legacy = function () {
19
+ return data;
20
+ };
21
+ return data;
22
+ }
23
+ function _workspaceModules() {
24
+ const data = require("@teambit/workspace.modules.workspace-locator");
25
+ _workspaceModules = function () {
26
+ return data;
27
+ };
28
+ return data;
29
+ }
30
+ function _scopeModules() {
31
+ const data = require("@teambit/scope.modules.find-scope-path");
32
+ _scopeModules = function () {
33
+ return data;
34
+ };
35
+ return data;
36
+ }
37
+ function _fs() {
38
+ const data = require("fs");
39
+ _fs = function () {
40
+ return data;
41
+ };
42
+ return data;
43
+ }
44
+ function _harmony() {
45
+ const data = require("@teambit/harmony");
46
+ _harmony = function () {
47
+ return data;
48
+ };
49
+ return data;
50
+ }
51
+ function _harmonyConfig() {
52
+ const data = require("@teambit/harmony/harmony-config");
53
+ _harmonyConfig = function () {
54
+ return data;
55
+ };
56
+ return data;
57
+ }
58
+ function _component() {
59
+ const data = require("@teambit/component");
60
+ _component = function () {
61
+ return data;
62
+ };
63
+ return data;
64
+ }
65
+ function _cli() {
66
+ const data = require("@teambit/cli");
67
+ _cli = function () {
68
+ return data;
69
+ };
70
+ return data;
71
+ }
72
+ function _node() {
73
+ const data = require("@teambit/node");
74
+ _node = function () {
75
+ return data;
76
+ };
77
+ return data;
78
+ }
79
+ function _legacy2() {
80
+ const data = require("@teambit/legacy.consumer-component");
81
+ _legacy2 = function () {
82
+ return data;
83
+ };
84
+ return data;
85
+ }
86
+ function _legacy3() {
87
+ const data = require("@teambit/legacy.consumer-config");
88
+ _legacy3 = function () {
89
+ return data;
90
+ };
91
+ return data;
92
+ }
93
+ function _workspaceModules2() {
94
+ const data = require("@teambit/workspace.modules.node-modules-linker");
95
+ _workspaceModules2 = function () {
96
+ return data;
97
+ };
98
+ return data;
99
+ }
100
+ function _dependencies() {
101
+ const data = require("@teambit/dependencies");
102
+ _dependencies = function () {
103
+ return data;
104
+ };
105
+ return data;
106
+ }
107
+ function _legacy4() {
108
+ const data = require("@teambit/legacy.extension-data");
109
+ _legacy4 = function () {
110
+ return data;
111
+ };
112
+ return data;
113
+ }
114
+ function getPackageName(aspect, id) {
115
+ return `@teambit/${id.name}`;
116
+ // const [owner, name] = aspect.id.split('.');
117
+ // return `@${owner}/${replaceAll(name, '/', '.')}`;
118
+ }
119
+
120
+ /**
121
+ * we keep a static list of core-aspect-ids here in this component in order to not depend on bit aspect.
122
+ * it gets updated during Circle tag process.
123
+ */
124
+ function getCoreAspectIds() {
125
+ const aspectIdsFile = (0, _path().join)(__dirname, 'core-aspects-ids.json');
126
+ const file = (0, _fs().readFileSync)(aspectIdsFile, 'utf8');
127
+ return JSON.parse(file);
128
+ }
129
+
130
+ /**
131
+ * ! important ! prefer using `loadManyAspects` instead of this function. otherwise, you may end up with
132
+ * different instances of "Workspace" aspect for example for each one of the aspects you load.
133
+ *
134
+ * to make this work, export the main also as default (e.g. `export default LanesMain;`).
135
+ * otherwise, it'll show an error "TypeError: Cannot read property 'runtime' of undefined".
136
+ */
137
+ async function loadAspect(targetAspect, cwd = process.cwd(), runtime = 'main') {
138
+ const harmony = await loadManyAspects([targetAspect], cwd, runtime);
139
+ return harmony.get(targetAspect.id);
140
+ }
141
+
142
+ /**
143
+ * returns an instance of Harmony. with this instance, you can get any aspect you loaded (or its dependencies).
144
+ * e.g. `const workspace = harmony.get<Workspace>(WorkspaceAspect.id);`
145
+ * when used for tests, specify all aspects you need and call it once. this way, you make sure all of them are in sync
146
+ * and use the same instances of each other.
147
+ */
148
+ async function loadManyAspects(targetAspects, cwd = process.cwd(), runtime = 'main') {
149
+ clearGlobalsIfNeeded();
150
+ const coreAspectIds = getCoreAspectIds();
151
+ // tried alternative to avoid this. however, in some cases, during build, this component is in a capsule, so the list
152
+ // of core-aspects registered during load-bit doesn't apply here.
153
+ _legacy4().ExtensionDataList.registerManyCoreExtensionNames(coreAspectIds);
154
+ const config = await getConfig(cwd);
155
+ const configMap = config.toObject();
156
+ configMap['teambit.harmony/bit'] = {
157
+ cwd
158
+ };
159
+
160
+ // CLIAspect is needed for register the main runtime. NodeAspect is needed to get the default env if nothing
161
+ // was configured. If it's not loaded here, it'll throw an error later that there is no node-env.
162
+ // DependenciesAspect is needed to make ComponentLoader.loadDeps hook works.
163
+ const harmony = await _harmony().Harmony.load([_cli().CLIAspect, _dependencies().DependenciesAspect, _node().NodeAspect, ...targetAspects], runtime, configMap);
164
+ await harmony.run(async (aspect, runtimeDef) => {
165
+ const id = _component().ComponentID.fromString(aspect.id);
166
+ const mainFilePath = getMainFilePath(aspect, id);
167
+ const packagePath = (0, _path().resolve)((0, _path().join)(mainFilePath, '..'));
168
+ const files = (0, _fs().readdirSync)(packagePath);
169
+ const runtimePath = files.find(path => path.includes(`.${runtimeDef.name}.runtime.js`));
170
+ if (!runtimePath) throw new Error(`could not find runtime '${runtimeDef.name}' for aspect ID '${aspect.id}'`);
171
+ // eslint-disable-next-line global-require, import/no-dynamic-require
172
+ const runtimeC = require((0, _path().join)(packagePath, runtimePath));
173
+ if (aspect.manifest._runtimes.length === 0 || targetAspects.includes(aspect.id)) {
174
+ // core-aspects running outside of bit-bin repo end up here. they don't have runtime.
175
+ // this makes sure to load them from the path were they're imported
176
+ addRuntimeIfNeeded(runtimeC, aspect);
177
+ }
178
+ });
179
+ return harmony;
180
+ }
181
+ function addRuntimeIfNeeded(runtimeC, aspect) {
182
+ if (runtimeC.default) {
183
+ aspect.manifest.addRuntime(runtimeC.default);
184
+ return;
185
+ }
186
+ const methodsOnResult = Object.keys(runtimeC);
187
+ if (methodsOnResult.length === 1) {
188
+ aspect.manifest.addRuntime(runtimeC[methodsOnResult[0]]);
189
+ return;
190
+ }
191
+ const main = methodsOnResult.filter(method => method.endsWith('Main'));
192
+ if (main.length === 1) {
193
+ aspect.manifest.addRuntime(runtimeC[main[0]]);
194
+ return;
195
+ }
196
+ throw new Error(`error: ${aspect.id} does not export its main-runtime as default.
197
+ go to the aspect-main file and add a new line with "export default YourAspectMain"`);
198
+ }
199
+ function getMainFilePath(aspect, id) {
200
+ let packageName = getPackageName(aspect, id);
201
+ try {
202
+ // try core aspects
203
+ return require.resolve(packageName);
204
+ } catch {
205
+ // fallback to a naive way of converting componentId to pkg-name. (it won't work when the component has special pkg name settings)
206
+ packageName = `@${id.scope.replace('.', '/')}.${id.fullName.replaceAll('/', '.')}`;
207
+ return require.resolve(packageName);
208
+ }
209
+ }
210
+ async function getConfig(cwd = process.cwd()) {
211
+ const consumerInfo = await (0, _workspaceModules().getWorkspaceInfo)(cwd);
212
+ const scopePath = (0, _scopeModules().findScopePath)(cwd);
213
+ const globalConfigOpts = {
214
+ name: '.bitrc.jsonc'
215
+ };
216
+ const configOpts = {
217
+ global: globalConfigOpts,
218
+ shouldThrow: false,
219
+ cwd: consumerInfo?.path || scopePath
220
+ };
221
+ if (consumerInfo) {
222
+ const config = _harmonyConfig().Config.load('workspace.jsonc', configOpts);
223
+ return config;
224
+ }
225
+ if (scopePath && !consumerInfo) {
226
+ return _harmonyConfig().Config.load('scope.jsonc', configOpts);
227
+ }
228
+ return _harmonyConfig().Config.loadGlobal(globalConfigOpts);
229
+ }
230
+ function clearGlobalsIfNeeded() {
231
+ if (!_legacy().loadConsumer.cache && !_workspaceModules2().PackageJsonTransformer.packageJsonTransformersRegistry.length) {
232
+ return;
233
+ }
234
+ delete _legacy().loadConsumer.cache;
235
+ _legacy2().ComponentLoader.onComponentLoadSubscribers = [];
236
+ _legacy3().ComponentOverrides.componentOverridesLoadingRegistry = {};
237
+ _legacy3().ComponentConfig.componentConfigLoadingRegistry = {};
238
+ _workspaceModules2().PackageJsonTransformer.packageJsonTransformersRegistry = [];
239
+ // @ts-ignore
240
+ _legacy2().ComponentLoader.loadDeps = undefined;
241
+ _legacy4().ExtensionDataList.coreExtensionsNames = new Map();
242
+ // @ts-ignore
243
+ _legacy3().LegacyWorkspaceConfig.workspaceConfigLoadingRegistry = undefined;
244
+ }
245
+
246
+ //# sourceMappingURL=load-aspect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_path","data","require","_legacy","_workspaceModules","_scopeModules","_fs","_harmony","_harmonyConfig","_component","_cli","_node","_legacy2","_legacy3","_workspaceModules2","_dependencies","_legacy4","getPackageName","aspect","id","name","getCoreAspectIds","aspectIdsFile","join","__dirname","file","readFileSync","JSON","parse","loadAspect","targetAspect","cwd","process","runtime","harmony","loadManyAspects","get","targetAspects","clearGlobalsIfNeeded","coreAspectIds","ExtensionDataList","registerManyCoreExtensionNames","config","getConfig","configMap","toObject","Harmony","load","CLIAspect","DependenciesAspect","NodeAspect","run","runtimeDef","ComponentID","fromString","mainFilePath","getMainFilePath","packagePath","resolve","files","readdirSync","runtimePath","find","path","includes","Error","runtimeC","manifest","_runtimes","length","addRuntimeIfNeeded","default","addRuntime","methodsOnResult","Object","keys","main","filter","method","endsWith","packageName","scope","replace","fullName","replaceAll","consumerInfo","getWorkspaceInfo","scopePath","findScopePath","globalConfigOpts","configOpts","global","shouldThrow","Config","loadGlobal","loadConsumer","cache","PackageJsonTransformer","packageJsonTransformersRegistry","ComponentLoader","onComponentLoadSubscribers","ComponentOverrides","componentOverridesLoadingRegistry","ComponentConfig","componentConfigLoadingRegistry","loadDeps","undefined","coreExtensionsNames","Map","LegacyWorkspaceConfig","workspaceConfigLoadingRegistry"],"sources":["load-aspect.ts"],"sourcesContent":["import { resolve, join } from 'path';\nimport { loadConsumer } from '@teambit/legacy.consumer';\nimport { getWorkspaceInfo } from '@teambit/workspace.modules.workspace-locator';\nimport { findScopePath } from '@teambit/scope.modules.find-scope-path';\nimport { readdirSync, readFileSync } from 'fs';\nimport type { Aspect, Extension } from '@teambit/harmony';\nimport { Harmony } from '@teambit/harmony';\nimport type { ConfigOptions } from '@teambit/harmony/harmony-config';\nimport { Config } from '@teambit/harmony/harmony-config';\nimport { ComponentID } from '@teambit/component';\nimport { CLIAspect } from '@teambit/cli';\nimport { NodeAspect } from '@teambit/node';\nimport { ComponentLoader } from '@teambit/legacy.consumer-component';\nimport { LegacyWorkspaceConfig, ComponentOverrides, ComponentConfig } from '@teambit/legacy.consumer-config';\nimport { PackageJsonTransformer } from '@teambit/workspace.modules.node-modules-linker';\nimport { DependenciesAspect } from '@teambit/dependencies';\nimport { ExtensionDataList } from '@teambit/legacy.extension-data';\n\nfunction getPackageName(aspect: any, id: ComponentID) {\n return `@teambit/${id.name}`;\n // const [owner, name] = aspect.id.split('.');\n // return `@${owner}/${replaceAll(name, '/', '.')}`;\n}\n\n/**\n * we keep a static list of core-aspect-ids here in this component in order to not depend on bit aspect.\n * it gets updated during Circle tag process.\n */\nfunction getCoreAspectIds() {\n const aspectIdsFile = join(__dirname, 'core-aspects-ids.json');\n const file = readFileSync(aspectIdsFile, 'utf8');\n return JSON.parse(file);\n}\n\n/**\n * ! important ! prefer using `loadManyAspects` instead of this function. otherwise, you may end up with\n * different instances of \"Workspace\" aspect for example for each one of the aspects you load.\n *\n * to make this work, export the main also as default (e.g. `export default LanesMain;`).\n * otherwise, it'll show an error \"TypeError: Cannot read property 'runtime' of undefined\".\n */\nexport async function loadAspect<T>(targetAspect: Aspect, cwd = process.cwd(), runtime = 'main'): Promise<T> {\n const harmony = await loadManyAspects([targetAspect], cwd, runtime);\n return harmony.get(targetAspect.id);\n}\n\n/**\n * returns an instance of Harmony. with this instance, you can get any aspect you loaded (or its dependencies).\n * e.g. `const workspace = harmony.get<Workspace>(WorkspaceAspect.id);`\n * when used for tests, specify all aspects you need and call it once. this way, you make sure all of them are in sync\n * and use the same instances of each other.\n */\nexport async function loadManyAspects(\n targetAspects: Aspect[],\n cwd = process.cwd(),\n runtime = 'main'\n): Promise<Harmony> {\n clearGlobalsIfNeeded();\n const coreAspectIds = getCoreAspectIds();\n // tried alternative to avoid this. however, in some cases, during build, this component is in a capsule, so the list\n // of core-aspects registered during load-bit doesn't apply here.\n ExtensionDataList.registerManyCoreExtensionNames(coreAspectIds);\n\n const config = await getConfig(cwd);\n const configMap = config.toObject();\n configMap['teambit.harmony/bit'] = {\n cwd,\n };\n\n // CLIAspect is needed for register the main runtime. NodeAspect is needed to get the default env if nothing\n // was configured. If it's not loaded here, it'll throw an error later that there is no node-env.\n // DependenciesAspect is needed to make ComponentLoader.loadDeps hook works.\n const harmony = await Harmony.load([CLIAspect, DependenciesAspect, NodeAspect, ...targetAspects], runtime, configMap);\n\n await harmony.run(async (aspect, runtimeDef) => {\n const id = ComponentID.fromString(aspect.id);\n const mainFilePath = getMainFilePath(aspect, id);\n const packagePath = resolve(join(mainFilePath, '..'));\n const files = readdirSync(packagePath);\n const runtimePath = files.find((path) => path.includes(`.${runtimeDef.name}.runtime.js`));\n if (!runtimePath) throw new Error(`could not find runtime '${runtimeDef.name}' for aspect ID '${aspect.id}'`);\n // eslint-disable-next-line global-require, import/no-dynamic-require\n const runtimeC = require(join(packagePath, runtimePath));\n if (aspect.manifest._runtimes.length === 0 || targetAspects.includes(aspect.id)) {\n // core-aspects running outside of bit-bin repo end up here. they don't have runtime.\n // this makes sure to load them from the path were they're imported\n addRuntimeIfNeeded(runtimeC, aspect);\n }\n });\n\n return harmony;\n}\n\nfunction addRuntimeIfNeeded(runtimeC: any, aspect: Extension) {\n if (runtimeC.default) {\n aspect.manifest.addRuntime(runtimeC.default);\n return;\n }\n const methodsOnResult = Object.keys(runtimeC);\n if (methodsOnResult.length === 1) {\n aspect.manifest.addRuntime(runtimeC[methodsOnResult[0]]);\n return;\n }\n const main = methodsOnResult.filter((method) => method.endsWith('Main'));\n if (main.length === 1) {\n aspect.manifest.addRuntime(runtimeC[main[0]]);\n return;\n }\n throw new Error(`error: ${aspect.id} does not export its main-runtime as default.\ngo to the aspect-main file and add a new line with \"export default YourAspectMain\"`);\n}\n\nfunction getMainFilePath(aspect: any, id: ComponentID) {\n let packageName = getPackageName(aspect, id);\n try {\n // try core aspects\n return require.resolve(packageName);\n } catch {\n // fallback to a naive way of converting componentId to pkg-name. (it won't work when the component has special pkg name settings)\n packageName = `@${id.scope.replace('.', '/')}.${id.fullName.replaceAll('/', '.')}`;\n return require.resolve(packageName);\n }\n}\n\nexport async function getConfig(cwd = process.cwd()) {\n const consumerInfo = await getWorkspaceInfo(cwd);\n const scopePath = findScopePath(cwd);\n const globalConfigOpts = {\n name: '.bitrc.jsonc',\n };\n const configOpts: ConfigOptions = {\n global: globalConfigOpts,\n shouldThrow: false,\n cwd: consumerInfo?.path || scopePath,\n };\n\n if (consumerInfo) {\n const config = Config.load('workspace.jsonc', configOpts);\n return config;\n }\n\n if (scopePath && !consumerInfo) {\n return Config.load('scope.jsonc', configOpts);\n }\n\n return Config.loadGlobal(globalConfigOpts);\n}\n\nfunction clearGlobalsIfNeeded() {\n if (!loadConsumer.cache && !PackageJsonTransformer.packageJsonTransformersRegistry.length) {\n return;\n }\n delete loadConsumer.cache;\n ComponentLoader.onComponentLoadSubscribers = [];\n ComponentOverrides.componentOverridesLoadingRegistry = {};\n ComponentConfig.componentConfigLoadingRegistry = {};\n PackageJsonTransformer.packageJsonTransformersRegistry = [];\n // @ts-ignore\n ComponentLoader.loadDeps = undefined;\n ExtensionDataList.coreExtensionsNames = new Map();\n // @ts-ignore\n LegacyWorkspaceConfig.workspaceConfigLoadingRegistry = undefined;\n}\n"],"mappings":";;;;;;;;AAAA,SAAAA,MAAA;EAAA,MAAAC,IAAA,GAAAC,OAAA;EAAAF,KAAA,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K,IAAI,CAACC,2CAAsB,CAACC,+BAA+B,CAAC7B,MAAM,EAAE;IACzF;EACF;EACA,OAAO0B,sBAAY,CAACC,KAAK;EACzBG,0BAAe,CAACC,0BAA0B,GAAG,EAAE;EAC/CC,6BAAkB,CAACC,iCAAiC,GAAG,CAAC,CAAC;EACzDC,0BAAe,CAACC,8BAA8B,GAAG,CAAC,CAAC;EACnDP,2CAAsB,CAACC,+BAA+B,GAAG,EAAE;EAC3D;EACAC,0BAAe,CAACM,QAAQ,GAAGC,SAAS;EACpClE,4BAAiB,CAACmE,mBAAmB,GAAG,IAAIC,GAAG,CAAC,CAAC;EACjD;EACAC,gCAAqB,CAACC,8BAA8B,GAAGJ,SAAS;AAClE","ignoreList":[]}
package/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { loadAspect, loadManyAspects, getConfig } from './load-aspect';
package/load-aspect.ts ADDED
@@ -0,0 +1,163 @@
1
+ import { resolve, join } from 'path';
2
+ import { loadConsumer } from '@teambit/legacy.consumer';
3
+ import { getWorkspaceInfo } from '@teambit/workspace.modules.workspace-locator';
4
+ import { findScopePath } from '@teambit/scope.modules.find-scope-path';
5
+ import { readdirSync, readFileSync } from 'fs';
6
+ import type { Aspect, Extension } from '@teambit/harmony';
7
+ import { Harmony } from '@teambit/harmony';
8
+ import type { ConfigOptions } from '@teambit/harmony/harmony-config';
9
+ import { Config } from '@teambit/harmony/harmony-config';
10
+ import { ComponentID } from '@teambit/component';
11
+ import { CLIAspect } from '@teambit/cli';
12
+ import { NodeAspect } from '@teambit/node';
13
+ import { ComponentLoader } from '@teambit/legacy.consumer-component';
14
+ import { LegacyWorkspaceConfig, ComponentOverrides, ComponentConfig } from '@teambit/legacy.consumer-config';
15
+ import { PackageJsonTransformer } from '@teambit/workspace.modules.node-modules-linker';
16
+ import { DependenciesAspect } from '@teambit/dependencies';
17
+ import { ExtensionDataList } from '@teambit/legacy.extension-data';
18
+
19
+ function getPackageName(aspect: any, id: ComponentID) {
20
+ return `@teambit/${id.name}`;
21
+ // const [owner, name] = aspect.id.split('.');
22
+ // return `@${owner}/${replaceAll(name, '/', '.')}`;
23
+ }
24
+
25
+ /**
26
+ * we keep a static list of core-aspect-ids here in this component in order to not depend on bit aspect.
27
+ * it gets updated during Circle tag process.
28
+ */
29
+ function getCoreAspectIds() {
30
+ const aspectIdsFile = join(__dirname, 'core-aspects-ids.json');
31
+ const file = readFileSync(aspectIdsFile, 'utf8');
32
+ return JSON.parse(file);
33
+ }
34
+
35
+ /**
36
+ * ! important ! prefer using `loadManyAspects` instead of this function. otherwise, you may end up with
37
+ * different instances of "Workspace" aspect for example for each one of the aspects you load.
38
+ *
39
+ * to make this work, export the main also as default (e.g. `export default LanesMain;`).
40
+ * otherwise, it'll show an error "TypeError: Cannot read property 'runtime' of undefined".
41
+ */
42
+ export async function loadAspect<T>(targetAspect: Aspect, cwd = process.cwd(), runtime = 'main'): Promise<T> {
43
+ const harmony = await loadManyAspects([targetAspect], cwd, runtime);
44
+ return harmony.get(targetAspect.id);
45
+ }
46
+
47
+ /**
48
+ * returns an instance of Harmony. with this instance, you can get any aspect you loaded (or its dependencies).
49
+ * e.g. `const workspace = harmony.get<Workspace>(WorkspaceAspect.id);`
50
+ * when used for tests, specify all aspects you need and call it once. this way, you make sure all of them are in sync
51
+ * and use the same instances of each other.
52
+ */
53
+ export async function loadManyAspects(
54
+ targetAspects: Aspect[],
55
+ cwd = process.cwd(),
56
+ runtime = 'main'
57
+ ): Promise<Harmony> {
58
+ clearGlobalsIfNeeded();
59
+ const coreAspectIds = getCoreAspectIds();
60
+ // tried alternative to avoid this. however, in some cases, during build, this component is in a capsule, so the list
61
+ // of core-aspects registered during load-bit doesn't apply here.
62
+ ExtensionDataList.registerManyCoreExtensionNames(coreAspectIds);
63
+
64
+ const config = await getConfig(cwd);
65
+ const configMap = config.toObject();
66
+ configMap['teambit.harmony/bit'] = {
67
+ cwd,
68
+ };
69
+
70
+ // CLIAspect is needed for register the main runtime. NodeAspect is needed to get the default env if nothing
71
+ // was configured. If it's not loaded here, it'll throw an error later that there is no node-env.
72
+ // DependenciesAspect is needed to make ComponentLoader.loadDeps hook works.
73
+ const harmony = await Harmony.load([CLIAspect, DependenciesAspect, NodeAspect, ...targetAspects], runtime, configMap);
74
+
75
+ await harmony.run(async (aspect, runtimeDef) => {
76
+ const id = ComponentID.fromString(aspect.id);
77
+ const mainFilePath = getMainFilePath(aspect, id);
78
+ const packagePath = resolve(join(mainFilePath, '..'));
79
+ const files = readdirSync(packagePath);
80
+ const runtimePath = files.find((path) => path.includes(`.${runtimeDef.name}.runtime.js`));
81
+ if (!runtimePath) throw new Error(`could not find runtime '${runtimeDef.name}' for aspect ID '${aspect.id}'`);
82
+ // eslint-disable-next-line global-require, import/no-dynamic-require
83
+ const runtimeC = require(join(packagePath, runtimePath));
84
+ if (aspect.manifest._runtimes.length === 0 || targetAspects.includes(aspect.id)) {
85
+ // core-aspects running outside of bit-bin repo end up here. they don't have runtime.
86
+ // this makes sure to load them from the path were they're imported
87
+ addRuntimeIfNeeded(runtimeC, aspect);
88
+ }
89
+ });
90
+
91
+ return harmony;
92
+ }
93
+
94
+ function addRuntimeIfNeeded(runtimeC: any, aspect: Extension) {
95
+ if (runtimeC.default) {
96
+ aspect.manifest.addRuntime(runtimeC.default);
97
+ return;
98
+ }
99
+ const methodsOnResult = Object.keys(runtimeC);
100
+ if (methodsOnResult.length === 1) {
101
+ aspect.manifest.addRuntime(runtimeC[methodsOnResult[0]]);
102
+ return;
103
+ }
104
+ const main = methodsOnResult.filter((method) => method.endsWith('Main'));
105
+ if (main.length === 1) {
106
+ aspect.manifest.addRuntime(runtimeC[main[0]]);
107
+ return;
108
+ }
109
+ throw new Error(`error: ${aspect.id} does not export its main-runtime as default.
110
+ go to the aspect-main file and add a new line with "export default YourAspectMain"`);
111
+ }
112
+
113
+ function getMainFilePath(aspect: any, id: ComponentID) {
114
+ let packageName = getPackageName(aspect, id);
115
+ try {
116
+ // try core aspects
117
+ return require.resolve(packageName);
118
+ } catch {
119
+ // fallback to a naive way of converting componentId to pkg-name. (it won't work when the component has special pkg name settings)
120
+ packageName = `@${id.scope.replace('.', '/')}.${id.fullName.replaceAll('/', '.')}`;
121
+ return require.resolve(packageName);
122
+ }
123
+ }
124
+
125
+ export async function getConfig(cwd = process.cwd()) {
126
+ const consumerInfo = await getWorkspaceInfo(cwd);
127
+ const scopePath = findScopePath(cwd);
128
+ const globalConfigOpts = {
129
+ name: '.bitrc.jsonc',
130
+ };
131
+ const configOpts: ConfigOptions = {
132
+ global: globalConfigOpts,
133
+ shouldThrow: false,
134
+ cwd: consumerInfo?.path || scopePath,
135
+ };
136
+
137
+ if (consumerInfo) {
138
+ const config = Config.load('workspace.jsonc', configOpts);
139
+ return config;
140
+ }
141
+
142
+ if (scopePath && !consumerInfo) {
143
+ return Config.load('scope.jsonc', configOpts);
144
+ }
145
+
146
+ return Config.loadGlobal(globalConfigOpts);
147
+ }
148
+
149
+ function clearGlobalsIfNeeded() {
150
+ if (!loadConsumer.cache && !PackageJsonTransformer.packageJsonTransformersRegistry.length) {
151
+ return;
152
+ }
153
+ delete loadConsumer.cache;
154
+ ComponentLoader.onComponentLoadSubscribers = [];
155
+ ComponentOverrides.componentOverridesLoadingRegistry = {};
156
+ ComponentConfig.componentConfigLoadingRegistry = {};
157
+ PackageJsonTransformer.packageJsonTransformersRegistry = [];
158
+ // @ts-ignore
159
+ ComponentLoader.loadDeps = undefined;
160
+ ExtensionDataList.coreExtensionsNames = new Map();
161
+ // @ts-ignore
162
+ LegacyWorkspaceConfig.workspaceConfigLoadingRegistry = undefined;
163
+ }
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@teambit/harmony.testing.load-aspect",
3
+ "version": "0.0.0-00ab0af77b86da7ad83d7a3df85678ea5368dacf",
4
+ "homepage": "https://bit.cloud/teambit/harmony/testing/load-aspect",
5
+ "main": "dist/index.js",
6
+ "componentId": {
7
+ "scope": "teambit.harmony",
8
+ "name": "testing/load-aspect",
9
+ "version": "00ab0af77b86da7ad83d7a3df85678ea5368dacf"
10
+ },
11
+ "dependencies": {
12
+ "@teambit/harmony": "0.4.12",
13
+ "@teambit/legacy.consumer-component": "0.0.0-69843b73504c167276bfeb42a67f8345e268590f",
14
+ "@teambit/legacy.consumer-config": "0.0.0-d0e1e9b81dc531509d5592061836d3b325d9b0ce",
15
+ "@teambit/legacy.consumer": "0.0.0-c4da2610d10bb478394eb36398f9bd253460ad75",
16
+ "@teambit/legacy.extension-data": "0.0.0-a5e7888bb10533bd25f1cdf0b4373d80bb1d357b",
17
+ "@teambit/scope.modules.find-scope-path": "0.0.0-462d88ff6234ab66a0f82b64e2cd1d38d313aa9d",
18
+ "@teambit/workspace.modules.node-modules-linker": "0.0.0-482ae5a8e8ca5408e5582ba17e2909d8b12f19a5",
19
+ "@teambit/workspace.modules.workspace-locator": "0.0.0-3f6e7056ee338650cf9fac8c3ad95311288841a9"
20
+ },
21
+ "devDependencies": {
22
+ "@types/mocha": "9.1.0",
23
+ "chai": "5.2.1",
24
+ "@teambit/node.envs.node-babel-mocha": "1.0.2"
25
+ },
26
+ "peerDependencies": {},
27
+ "license": "Apache-2.0",
28
+ "optionalDependencies": {},
29
+ "peerDependenciesMeta": {},
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "node": {
34
+ "require": "./dist/index.js",
35
+ "import": "./dist/esm.mjs"
36
+ },
37
+ "default": "./dist/index.js"
38
+ },
39
+ "./dist/*": "./dist/*"
40
+ },
41
+ "private": false,
42
+ "engines": {
43
+ "node": ">=12.22.0"
44
+ },
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "https://github.com/teambit/bit"
48
+ },
49
+ "keywords": [
50
+ "bit",
51
+ "components",
52
+ "collaboration",
53
+ "web",
54
+ "react",
55
+ "react-components",
56
+ "angular",
57
+ "angular-components"
58
+ ]
59
+ }
@@ -0,0 +1,41 @@
1
+ declare module '*.png' {
2
+ const value: any;
3
+ export = value;
4
+ }
5
+ declare module '*.svg' {
6
+ import type { FunctionComponent, SVGProps } from 'react';
7
+
8
+ export const ReactComponent: FunctionComponent<
9
+ SVGProps<SVGSVGElement> & { title?: string }
10
+ >;
11
+ const src: string;
12
+ export default src;
13
+ }
14
+ declare module '*.jpg' {
15
+ const value: any;
16
+ export = value;
17
+ }
18
+ declare module '*.jpeg' {
19
+ const value: any;
20
+ export = value;
21
+ }
22
+ declare module '*.gif' {
23
+ const value: any;
24
+ export = value;
25
+ }
26
+ declare module '*.bmp' {
27
+ const value: any;
28
+ export = value;
29
+ }
30
+ declare module '*.otf' {
31
+ const value: any;
32
+ export = value;
33
+ }
34
+ declare module '*.woff' {
35
+ const value: any;
36
+ export = value;
37
+ }
38
+ declare module '*.woff2' {
39
+ const value: any;
40
+ export = value;
41
+ }
@@ -0,0 +1,46 @@
1
+ declare module '*.module.css' {
2
+ const classes: { readonly [key: string]: string };
3
+ export default classes;
4
+ }
5
+ declare module '*.module.scss' {
6
+ const classes: { readonly [key: string]: string };
7
+ export default classes;
8
+ }
9
+ declare module '*.module.sass' {
10
+ const classes: { readonly [key: string]: string };
11
+ export default classes;
12
+ }
13
+
14
+ declare module '*.module.less' {
15
+ const classes: { readonly [key: string]: string };
16
+ export default classes;
17
+ }
18
+
19
+ declare module '*.less' {
20
+ const classes: { readonly [key: string]: string };
21
+ export default classes;
22
+ }
23
+
24
+ declare module '*.css' {
25
+ const classes: { readonly [key: string]: string };
26
+ export default classes;
27
+ }
28
+
29
+ declare module '*.sass' {
30
+ const classes: { readonly [key: string]: string };
31
+ export default classes;
32
+ }
33
+
34
+ declare module '*.scss' {
35
+ const classes: { readonly [key: string]: string };
36
+ export default classes;
37
+ }
38
+
39
+ declare module '*.mdx' {
40
+ const component: any;
41
+ export default component;
42
+ }
43
+
44
+ // `reset-css` ships only `reset.css` (no `.d.ts`). TS6 refuses bare
45
+ // side-effect imports without declarations, so we declare the module here.
46
+ declare module 'reset-css';