@teambit/harmony.testing.load-aspect 0.0.316 → 0.0.317
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.
- package/dist/load-aspect.d.ts +2 -1
- package/dist/load-aspect.js.map +1 -1
- package/load-aspect.ts +4 -2
- package/package.json +8 -8
package/dist/load-aspect.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Aspect } from '@teambit/harmony';
|
|
2
|
+
import { Harmony } from '@teambit/harmony';
|
|
2
3
|
import { Config } from '@teambit/harmony/dist/harmony-config';
|
|
3
4
|
/**
|
|
4
5
|
* ! important ! prefer using `loadManyAspects` instead of this function. otherwise, you may end up with
|
package/dist/load-aspect.js.map
CHANGED
|
@@ -1 +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 { Harmony, Aspect, Extension } from '@teambit/harmony';\n// TODO: expose this types from harmony (once we have a way to expose it only for node)\nimport { Config, ConfigOptions } from '@teambit/harmony/dist/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":";;;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5C;AAEA,SAASnD,oBAAoBA,CAAA,EAAG;EAC9B,IAAI,CAACyD,sBAAY,CAACC,KAAK,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":[]}
|
|
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';\n// TODO: expose this types from harmony (once we have a way to expose it only for node)\nimport type { ConfigOptions } from '@teambit/harmony/dist/harmony-config';\nimport { Config } from '@teambit/harmony/dist/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 =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E2C,UAAU,CAAC;EAC/C;EAEA,OAAOG,uBAAM,CAACC,UAAU,CAACL,gBAAgB,CAAC;AAC5C;AAEA,SAASnD,oBAAoBA,CAAA,EAAG;EAC9B,IAAI,CAACyD,sBAAY,CAACC,KAAK,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/load-aspect.ts
CHANGED
|
@@ -3,9 +3,11 @@ import { loadConsumer } from '@teambit/legacy.consumer';
|
|
|
3
3
|
import { getWorkspaceInfo } from '@teambit/workspace.modules.workspace-locator';
|
|
4
4
|
import { findScopePath } from '@teambit/scope.modules.find-scope-path';
|
|
5
5
|
import { readdirSync, readFileSync } from 'fs';
|
|
6
|
-
import {
|
|
6
|
+
import type { Aspect, Extension } from '@teambit/harmony';
|
|
7
|
+
import { Harmony } from '@teambit/harmony';
|
|
7
8
|
// TODO: expose this types from harmony (once we have a way to expose it only for node)
|
|
8
|
-
import {
|
|
9
|
+
import type { ConfigOptions } from '@teambit/harmony/dist/harmony-config';
|
|
10
|
+
import { Config } from '@teambit/harmony/dist/harmony-config';
|
|
9
11
|
import { ComponentID } from '@teambit/component';
|
|
10
12
|
import { CLIAspect } from '@teambit/cli';
|
|
11
13
|
import { NodeAspect } from '@teambit/node';
|
package/package.json
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teambit/harmony.testing.load-aspect",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.317",
|
|
4
4
|
"homepage": "https://bit.cloud/teambit/harmony/testing/load-aspect",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"componentId": {
|
|
7
7
|
"scope": "teambit.harmony",
|
|
8
8
|
"name": "testing/load-aspect",
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.317"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@teambit/harmony": "0.4.7",
|
|
13
|
-
"@teambit/legacy.consumer-component": "0.0.58",
|
|
14
|
-
"@teambit/legacy.consumer-config": "0.0.57",
|
|
15
|
-
"@teambit/legacy.consumer": "0.0.57",
|
|
16
|
-
"@teambit/legacy.extension-data": "0.0.59",
|
|
17
13
|
"@teambit/scope.modules.find-scope-path": "0.0.14",
|
|
18
|
-
"@teambit/workspace.modules.
|
|
19
|
-
"@teambit/
|
|
14
|
+
"@teambit/workspace.modules.workspace-locator": "0.0.14",
|
|
15
|
+
"@teambit/legacy.consumer-component": "0.0.59",
|
|
16
|
+
"@teambit/legacy.consumer-config": "0.0.58",
|
|
17
|
+
"@teambit/legacy.consumer": "0.0.58",
|
|
18
|
+
"@teambit/legacy.extension-data": "0.0.60",
|
|
19
|
+
"@teambit/workspace.modules.node-modules-linker": "0.0.286"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/mocha": "9.1.0",
|