@teambit/scope 1.0.108 → 1.0.110

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.
@@ -1,208 +0,0 @@
1
- import { Component, ComponentFS, ComponentID, Config, Snap, State, Tag, TagMap } from '@teambit/component';
2
- import pMapSeries from 'p-map-series';
3
- import { Logger } from '@teambit/logger';
4
- import { SemVer } from 'semver';
5
- import ConsumerComponent from '@teambit/legacy/dist/consumer/component';
6
- import { ModelComponent, Version } from '@teambit/legacy/dist/scope/models';
7
- import { Ref } from '@teambit/legacy/dist/scope/objects';
8
- import { VERSION_ZERO } from '@teambit/legacy/dist/scope/models/model-component';
9
- import { getMaxSizeForComponents, InMemoryCache } from '@teambit/legacy/dist/cache/in-memory-cache';
10
- import { createInMemoryCache } from '@teambit/legacy/dist/cache/cache-factory';
11
- import type { ScopeMain } from './scope.main.runtime';
12
-
13
- export class ScopeComponentLoader {
14
- private componentsCache: InMemoryCache<Component>; // cache loaded components
15
- private importedComponentsCache: InMemoryCache<boolean>;
16
- constructor(private scope: ScopeMain, private logger: Logger) {
17
- this.componentsCache = createInMemoryCache({ maxSize: getMaxSizeForComponents() });
18
- this.importedComponentsCache = createInMemoryCache({ maxAge: 1000 * 60 * 30 }); // 30 min
19
- }
20
-
21
- async get(id: ComponentID, importIfMissing = true, useCache = true): Promise<Component | undefined> {
22
- const fromCache = this.getFromCache(id);
23
- if (fromCache && useCache) {
24
- return fromCache;
25
- }
26
- const idStr = id.toString();
27
- this.logger.debug(`ScopeComponentLoader.get, loading ${idStr}`);
28
- const legacyId = id;
29
- let modelComponent = await this.scope.legacyScope.getModelComponentIfExist(id);
30
- // import if missing
31
- if (
32
- !modelComponent &&
33
- importIfMissing &&
34
- this.scope.isExported(id) &&
35
- !this.importedComponentsCache.get(id.toString())
36
- ) {
37
- await this.scope.import([id], { reason: `${id.toString()} because it's missing from the local scope` });
38
- this.importedComponentsCache.set(id.toString(), true);
39
- modelComponent = await this.scope.legacyScope.getModelComponentIfExist(id);
40
- }
41
- // Search with scope name for bare scopes
42
- if (!modelComponent && !legacyId.scope) {
43
- id = id.changeScope(this.scope.name);
44
- modelComponent = await this.scope.legacyScope.getModelComponentIfExist(id);
45
- }
46
- if (!modelComponent) return undefined;
47
-
48
- const versionStr = id.hasVersion()
49
- ? (id.version as string)
50
- : modelComponent.getHeadRegardlessOfLaneAsTagOrHash(true);
51
-
52
- if (versionStr === VERSION_ZERO) return undefined;
53
- const newId = id.changeVersion(versionStr);
54
- const version = await modelComponent.loadVersion(versionStr, this.scope.legacyScope.objects);
55
- const snap = await this.getHeadSnap(modelComponent);
56
- const state = await this.createStateFromVersion(id, version);
57
- const tagMap = this.getTagMap(modelComponent);
58
-
59
- const component = new Component(newId, snap, state, tagMap, this.scope);
60
- this.componentsCache.set(idStr, component);
61
- return component;
62
- }
63
-
64
- async getFromConsumerComponent(consumerComponent: ConsumerComponent): Promise<Component> {
65
- const legacyId = consumerComponent.id;
66
- const modelComponent = await this.scope.legacyScope.getModelComponent(legacyId);
67
- // :TODO move to head snap once we have it merged, for now using `latest`.
68
- const id = await this.scope.resolveComponentId(legacyId);
69
- const version =
70
- consumerComponent.pendingVersion ||
71
- (await modelComponent.loadVersion(legacyId.version as string, this.scope.legacyScope.objects));
72
- const snap = await this.getHeadSnap(modelComponent);
73
- const state = await this.createStateFromVersion(id, version, consumerComponent);
74
- const tagMap = this.getTagMap(modelComponent);
75
-
76
- return new Component(id, snap, state, tagMap, this.scope);
77
- }
78
-
79
- /**
80
- * get a component from a remote without importing it
81
- */
82
- async getRemoteComponent(id: ComponentID): Promise<Component> {
83
- const compImport = this.scope.legacyScope.scopeImporter;
84
- const objectList = await compImport.getRemoteComponent(id);
85
- // it's crucial to add all objects to the Repository cache. otherwise, later, when it asks
86
- // for the consumerComponent from the legacyScope, it won't work.
87
- objectList?.getAll().forEach((obj) => this.scope.legacyScope.objects.setCache(obj));
88
- const consumerComponent = await this.scope.legacyScope.getConsumerComponent(id);
89
- return this.getFromConsumerComponent(consumerComponent);
90
- }
91
-
92
- /**
93
- * get components from a remote without importing it
94
- */
95
- async getManyRemoteComponents(ids: ComponentID[]): Promise<Component[]> {
96
- const compImport = this.scope.legacyScope.scopeImporter;
97
- const legacyIds = ids.map((id) => id);
98
- const objectList = await compImport.getManyRemoteComponents(legacyIds);
99
- // it's crucial to add all objects to the Repository cache. otherwise, later, when it asks
100
- // for the consumerComponent from the legacyScope, it won't work.
101
- objectList?.getAll().forEach((obj) => this.scope.legacyScope.objects.setCache(obj));
102
- return pMapSeries(legacyIds, async (legacyId) => {
103
- const consumerComponent = await this.scope.legacyScope.getConsumerComponent(legacyId);
104
- return this.getFromConsumerComponent(consumerComponent);
105
- });
106
- }
107
-
108
- async getState(id: ComponentID, hash: string): Promise<State> {
109
- const version = (await this.scope.legacyScope.objects.load(new Ref(hash))) as Version;
110
- return this.createStateFromVersion(id, version);
111
- }
112
-
113
- async getSnap(id: ComponentID, hash: string): Promise<Snap> {
114
- const getVersionObject = async (): Promise<Version> => {
115
- try {
116
- const snap = await this.scope.legacyScope.objects.load(new Ref(hash), true);
117
- return snap as Version;
118
- } catch (err: any) {
119
- if (err.code === 'ENOENT') {
120
- const errMsg = `fatal: snap "${hash}" file for component "${id.toString()}" was not found in the filesystem`;
121
- this.logger.error(errMsg, err);
122
- throw new Error(errMsg);
123
- } else {
124
- throw err;
125
- }
126
- }
127
- };
128
- const version = await getVersionObject();
129
- return this.createSnapFromVersion(version);
130
- }
131
-
132
- clearCache() {
133
- this.componentsCache.deleteAll();
134
- }
135
-
136
- /**
137
- * make sure that not only the id-str match, but also the legacy-id.
138
- * this is needed because the ComponentID.toString() is the same whether or not the legacy-id has
139
- * scope-name, as it includes the defaultScope if the scope is empty.
140
- * as a result, when out-of-sync is happening and the id is changed to include scope-name in the
141
- * legacy-id, the component is the cache has the old id.
142
- */
143
- private getFromCache(id: ComponentID): Component | undefined {
144
- const idStr = id.toString();
145
- const fromCache = this.componentsCache.get(idStr);
146
- if (fromCache && fromCache.id.isEqual(id)) {
147
- return fromCache;
148
- }
149
- return undefined;
150
- }
151
-
152
- private getTagMap(modelComponent: ModelComponent): TagMap {
153
- const tagMap = new TagMap();
154
- const allVersions = modelComponent.versionsIncludeOrphaned;
155
- Object.keys(allVersions).forEach((versionStr: string) => {
156
- const tag = new Tag(allVersions[versionStr].toString(), new SemVer(versionStr));
157
- tagMap.set(tag.version, tag);
158
- });
159
- return tagMap;
160
- }
161
-
162
- private async getHeadSnap(modelComponent: ModelComponent): Promise<Snap | null> {
163
- const head = modelComponent.getHeadRegardlessOfLane();
164
- if (!head) {
165
- // happens for example when on main and merging a lane.
166
- return null;
167
- }
168
- const version = await modelComponent.loadVersion(head.toString(), this.scope.legacyScope.objects, false);
169
- if (!version) {
170
- // might happen when the component is just a dependency and a previous version was needed.
171
- return null;
172
- }
173
- return this.createSnapFromVersion(version);
174
- }
175
-
176
- private createSnapFromVersion(version: Version): Snap {
177
- return new Snap(
178
- version.hash().toString(),
179
- new Date(parseInt(version.log.date)),
180
- version.parents.map((p) => p.toString()),
181
- {
182
- displayName: version.log.username || 'unknown',
183
- email: version.log.email || 'unknown@anywhere',
184
- },
185
- version.log.message
186
- );
187
- }
188
-
189
- private async createStateFromVersion(
190
- id: ComponentID,
191
- version: Version,
192
- consumerComponent?: ConsumerComponent
193
- ): Promise<State> {
194
- consumerComponent = consumerComponent || (await this.scope.legacyScope.getConsumerComponent(id));
195
- const state = new State(
196
- // We use here the consumerComponent.extensions instead of version.extensions
197
- // because as part of the conversion to consumer component the artifacts are initialized as Artifact instances
198
- new Config(consumerComponent),
199
- // todo: see the comment of this "createAspectListFromLegacy" method. the aspect ids may be incorrect.
200
- // find a better way to get the ids correctly.
201
- this.scope.componentExtension.createAspectListFromLegacy(consumerComponent.extensions),
202
- ComponentFS.fromVinyls(consumerComponent.files),
203
- version.dependencies,
204
- consumerComponent
205
- );
206
- return state;
207
- }
208
- }
package/scope.aspect.ts DELETED
@@ -1,7 +0,0 @@
1
- import { Aspect } from '@teambit/harmony';
2
-
3
- export const ScopeAspect = Aspect.create({
4
- id: 'teambit.scope/scope',
5
- });
6
-
7
- export default ScopeAspect;
package/scope.graphql.ts DELETED
@@ -1,115 +0,0 @@
1
- import { ComponentID } from '@teambit/component';
2
- import gql from 'graphql-tag';
3
- import { latestVersions } from '@teambit/legacy/dist/api/scope';
4
- import { ScopeMain } from './scope.main.runtime';
5
-
6
- export function scopeSchema(scopeMain: ScopeMain) {
7
- return {
8
- typeDefs: gql`
9
- type Scope {
10
- # name of the scope.
11
- name: String
12
-
13
- # description of the scope.
14
- description: String
15
-
16
- # icon of the scope.
17
- icon: String
18
-
19
- # background color of the icon.
20
- backgroundIconColor: String
21
-
22
- # path of the scope.
23
- path: String
24
-
25
- # list of components contained in the scope.
26
- components(offset: Int, limit: Int, includeCache: Boolean, namespaces: [String!]): [Component]
27
-
28
- # get a specific component.
29
- get(id: String!): Component
30
-
31
- # get serialized legacy component. deprecated. PLEASE DO NOT USE THIS API.
32
- _getLegacy(id: String!): String
33
-
34
- # get logs.
35
- getLogs(id: String!): [Log]
36
-
37
- # get many components by ID.
38
- getMany(ids: [String]!): [Component]
39
-
40
- # get serialized legacy component ids with versions. deprecated. PLEASE DO NOT USE THIS API.
41
- _legacyLatestVersions(ids: [String]!): [String]
42
- }
43
-
44
- type Log {
45
- message: String
46
- username: String
47
- email: String
48
- date: String
49
- hash: String!
50
- tag: String
51
- }
52
-
53
- type LegacyMeta {
54
- id: String
55
- deprecated: Boolean
56
- }
57
-
58
- type Query {
59
- scope: Scope
60
- }
61
- `,
62
- resolvers: {
63
- Scope: {
64
- name: (scope: ScopeMain) => {
65
- return scope.name;
66
- },
67
- description: (scope: ScopeMain) => {
68
- return scope.description;
69
- },
70
- icon: (scope: ScopeMain) => {
71
- return scope.icon;
72
- },
73
- backgroundIconColor: (scope: ScopeMain) => {
74
- return scope.backgroundIconColor;
75
- },
76
- components: (
77
- scope: ScopeMain,
78
- props?: { offset: number; limit: number; includeCache?: boolean; namespaces?: string[] }
79
- ) => {
80
- if (!props) return scope.list();
81
- return scope.list({ ...props }, props.includeCache);
82
- },
83
-
84
- get: async (scope: ScopeMain, { id }: { id: string }) => {
85
- return scope.get(ComponentID.fromString(id));
86
- },
87
-
88
- _getLegacy: async (scope: ScopeMain, { id }: { id: string }) => {
89
- const resolvedId = await scope.resolveId(id);
90
- const component = await scope.get(resolvedId);
91
- if (!component) return null;
92
- return component.state._consumer.toString();
93
- },
94
-
95
- _legacyLatestVersions: async (scope: ScopeMain, { ids }: { ids: string[] }) => {
96
- return latestVersions(scope.path, ids);
97
- },
98
-
99
- getLogs: async (scope: ScopeMain, { id }: { id: string }) => {
100
- return scope.getLogs(ComponentID.fromString(id));
101
- },
102
-
103
- getMany: async (scope: ScopeMain, { ids }: { ids: string[] }) => {
104
- return scope.getMany(ids.map((str) => ComponentID.fromString(str)));
105
- },
106
- // delete: async (scope: ScopeMain, props: { }) => {
107
-
108
- // }
109
- },
110
- Query: {
111
- scope: () => scopeMain,
112
- },
113
- },
114
- };
115
- }