@teambit/objects 0.0.19
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/artifacts/__bit_junit.xml +68 -0
- package/artifacts/preview/teambit_scope_objects-preview.js +1 -0
- package/dist/fixtures/version-model-extended.json +48 -0
- package/dist/fixtures/version-model-object.json +87 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +371 -0
- package/dist/index.js.map +1 -0
- package/dist/models/dependencies-graph.d.ts +45 -0
- package/dist/models/dependencies-graph.js +106 -0
- package/dist/models/dependencies-graph.js.map +1 -0
- package/dist/models/detach-heads.d.ts +25 -0
- package/dist/models/detach-heads.js +84 -0
- package/dist/models/detach-heads.js.map +1 -0
- package/dist/models/export-metadata.d.ts +24 -0
- package/dist/models/export-metadata.js +76 -0
- package/dist/models/export-metadata.js.map +1 -0
- package/dist/models/index.d.ts +10 -0
- package/dist/models/index.js +125 -0
- package/dist/models/index.js.map +1 -0
- package/dist/models/lane-history.d.ts +40 -0
- package/dist/models/lane-history.js +117 -0
- package/dist/models/lane-history.js.map +1 -0
- package/dist/models/lane.d.ts +124 -0
- package/dist/models/lane.js +463 -0
- package/dist/models/lane.js.map +1 -0
- package/dist/models/model-component.d.ts +317 -0
- package/dist/models/model-component.js +1365 -0
- package/dist/models/model-component.js.map +1 -0
- package/dist/models/model-component.spec.d.ts +1 -0
- package/dist/models/model-component.spec.js +71 -0
- package/dist/models/model-component.spec.js.map +1 -0
- package/dist/models/scopeMeta.d.ts +20 -0
- package/dist/models/scopeMeta.js +71 -0
- package/dist/models/scopeMeta.js.map +1 -0
- package/dist/models/source.d.ts +10 -0
- package/dist/models/source.js +43 -0
- package/dist/models/source.js.map +1 -0
- package/dist/models/symlink.d.ts +30 -0
- package/dist/models/symlink.js +91 -0
- package/dist/models/symlink.js.map +1 -0
- package/dist/models/version-history.d.ts +59 -0
- package/dist/models/version-history.js +285 -0
- package/dist/models/version-history.js.map +1 -0
- package/dist/models/version.d.ts +279 -0
- package/dist/models/version.js +777 -0
- package/dist/models/version.js.map +1 -0
- package/dist/models/version.spec.d.ts +1 -0
- package/dist/models/version.spec.js +340 -0
- package/dist/models/version.spec.js.map +1 -0
- package/dist/objects/bit-object-list.d.ts +24 -0
- package/dist/objects/bit-object-list.js +65 -0
- package/dist/objects/bit-object-list.js.map +1 -0
- package/dist/objects/index.d.ts +5 -0
- package/dist/objects/index.js +60 -0
- package/dist/objects/index.js.map +1 -0
- package/dist/objects/object-list-to-graph.d.ts +13 -0
- package/dist/objects/object-list-to-graph.js +93 -0
- package/dist/objects/object-list-to-graph.js.map +1 -0
- package/dist/objects/object-list.d.ts +52 -0
- package/dist/objects/object-list.js +369 -0
- package/dist/objects/object-list.js.map +1 -0
- package/dist/objects/object.d.ts +35 -0
- package/dist/objects/object.js +190 -0
- package/dist/objects/object.js.map +1 -0
- package/dist/objects/objects-readable-generator.d.ts +31 -0
- package/dist/objects/objects-readable-generator.js +192 -0
- package/dist/objects/objects-readable-generator.js.map +1 -0
- package/dist/objects/raw-object.d.ts +23 -0
- package/dist/objects/raw-object.js +155 -0
- package/dist/objects/raw-object.js.map +1 -0
- package/dist/objects/ref.d.ts +14 -0
- package/dist/objects/ref.js +45 -0
- package/dist/objects/ref.js.map +1 -0
- package/dist/objects/repository-hooks.d.ts +4 -0
- package/dist/objects/repository-hooks.js +56 -0
- package/dist/objects/repository-hooks.js.map +1 -0
- package/dist/objects/repository.d.ts +148 -0
- package/dist/objects/repository.js +842 -0
- package/dist/objects/repository.js.map +1 -0
- package/dist/objects/scope-index.d.ts +73 -0
- package/dist/objects/scope-index.js +251 -0
- package/dist/objects/scope-index.js.map +1 -0
- package/dist/objects/scope-index.spec.d.ts +1 -0
- package/dist/objects/scope-index.spec.js +152 -0
- package/dist/objects/scope-index.spec.js.map +1 -0
- package/dist/objects.aspect.d.ts +2 -0
- package/dist/objects.aspect.js +18 -0
- package/dist/objects.aspect.js.map +1 -0
- package/dist/objects.main.runtime.d.ts +7 -0
- package/dist/objects.main.runtime.js +36 -0
- package/dist/objects.main.runtime.js.map +1 -0
- package/dist/preview-1736824735631.js +7 -0
- package/fixtures/version-model-extended.json +48 -0
- package/fixtures/version-model-object.json +87 -0
- package/models/dependencies-graph.ts +119 -0
- package/models/detach-heads.ts +79 -0
- package/models/export-metadata.ts +57 -0
- package/models/index.ts +11 -0
- package/models/lane-history.ts +106 -0
- package/models/lane.ts +367 -0
- package/models/model-component.spec.ts +55 -0
- package/models/model-component.ts +1367 -0
- package/models/scopeMeta.ts +60 -0
- package/models/source.ts +32 -0
- package/models/symlink.ts +66 -0
- package/models/version-history.ts +266 -0
- package/models/version.spec.ts +288 -0
- package/models/version.ts +818 -0
- package/objects/bit-object-list.ts +59 -0
- package/objects/index.ts +6 -0
- package/objects/object-list-to-graph.ts +69 -0
- package/objects/object-list.ts +313 -0
- package/objects/object.ts +153 -0
- package/objects/objects-readable-generator.ts +167 -0
- package/objects/raw-object.ts +142 -0
- package/objects/ref.ts +45 -0
- package/objects/repository-hooks.ts +42 -0
- package/objects/repository.ts +753 -0
- package/objects/scope-index.spec.ts +95 -0
- package/objects/scope-index.ts +192 -0
- package/package.json +98 -0
- package/types/asset.d.ts +41 -0
- package/types/style.d.ts +42 -0
@@ -0,0 +1,167 @@
|
|
1
|
+
import pMapSeries from 'p-map-series';
|
2
|
+
import { BitError } from '@teambit/bit-error';
|
3
|
+
import { Readable } from 'stream';
|
4
|
+
import BitObject from './object';
|
5
|
+
import Repository from './repository';
|
6
|
+
import Ref from './ref';
|
7
|
+
import { Scope } from '@teambit/legacy.scope';
|
8
|
+
import { logger } from '@teambit/legacy.logger';
|
9
|
+
import { getAllVersionHashesMemoized } from '@teambit/component.snap-distance';
|
10
|
+
import { Lane, LaneHistory, ModelComponent, Version } from '../models';
|
11
|
+
import { ObjectItem } from './object-list';
|
12
|
+
|
13
|
+
export type ComponentWithCollectOptions = {
|
14
|
+
component: ModelComponent;
|
15
|
+
version: string;
|
16
|
+
collectParents: boolean;
|
17
|
+
collectParentsUntil?: Ref | null; // stop traversing when this hash found. helps to import only the delta.
|
18
|
+
collectArtifacts: boolean;
|
19
|
+
includeVersionHistory?: boolean; // send VersionHistory object if exists rather than collecting parents
|
20
|
+
};
|
21
|
+
|
22
|
+
export class ObjectsReadableGenerator {
|
23
|
+
public readable: Readable;
|
24
|
+
private pushed: string[] = [];
|
25
|
+
constructor(
|
26
|
+
private repo: Repository,
|
27
|
+
private callbackOnceDone: Function
|
28
|
+
) {
|
29
|
+
this.readable = new Readable({ objectMode: true, read() {} });
|
30
|
+
}
|
31
|
+
async pushObjectsToReadable(componentsWithOptions: ComponentWithCollectOptions[]) {
|
32
|
+
try {
|
33
|
+
await this.pushScopeMeta();
|
34
|
+
await pMapSeries(componentsWithOptions, async (componentWithOptions) =>
|
35
|
+
this.pushComponentObjects(componentWithOptions)
|
36
|
+
);
|
37
|
+
this.closeReadableSuccessfully();
|
38
|
+
} catch (err: any) {
|
39
|
+
this.closeReadableFailure(err);
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
async pushLanes(lanesToFetch: Lane[], lanesHistory: LaneHistory[] = []) {
|
44
|
+
try {
|
45
|
+
const lanesItems = await Promise.all(lanesToFetch.map((laneToFetch) => this.bitObjectToItem(laneToFetch)));
|
46
|
+
const laneHistoryItems = await Promise.all(lanesHistory.map((laneHistory) => this.bitObjectToItem(laneHistory)));
|
47
|
+
this.pushManyObjects([...lanesItems, ...laneHistoryItems]);
|
48
|
+
this.closeReadableSuccessfully();
|
49
|
+
} catch (err: any) {
|
50
|
+
this.closeReadableFailure(err);
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
async pushObjects(refs: Ref[], scope: Scope) {
|
55
|
+
try {
|
56
|
+
await pMapSeries(refs, async (ref) => {
|
57
|
+
const objectItem = await this.getObjectGracefully(ref, scope);
|
58
|
+
if (objectItem) this.push(objectItem);
|
59
|
+
});
|
60
|
+
this.closeReadableSuccessfully();
|
61
|
+
} catch (err: any) {
|
62
|
+
this.closeReadableFailure(err);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
private closeReadableSuccessfully() {
|
67
|
+
logger.debug(`ObjectsReadableGenerator, pushed ${this.pushed.length} objects`);
|
68
|
+
this.callbackOnceDone();
|
69
|
+
this.readable.push(null);
|
70
|
+
}
|
71
|
+
|
72
|
+
private closeReadableFailure(err: Error) {
|
73
|
+
logger.debug(`ObjectsReadableGenerator, pushed ${this.pushed.length} objects`);
|
74
|
+
logger.error(`ObjectsReadableGenerator, got an error`, err);
|
75
|
+
this.callbackOnceDone(err);
|
76
|
+
this.readable.destroy(err);
|
77
|
+
}
|
78
|
+
|
79
|
+
private async bitObjectToItem(obj: BitObject): Promise<ObjectItem> {
|
80
|
+
return {
|
81
|
+
ref: obj.hash(),
|
82
|
+
buffer: await obj.compress(),
|
83
|
+
type: obj.getType(),
|
84
|
+
};
|
85
|
+
}
|
86
|
+
|
87
|
+
private async getObjectGracefully(ref: Ref, scope: Scope) {
|
88
|
+
try {
|
89
|
+
return await scope.getObjectItem(ref);
|
90
|
+
} catch (err: any) {
|
91
|
+
logger.warn(
|
92
|
+
`getObjectGracefully: failed retrieving an object ${ref.toString()} from the filesystem.\n${err.message}`
|
93
|
+
);
|
94
|
+
return null;
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
private async pushScopeMeta() {
|
99
|
+
const scopeMeta = await this.repo.getScopeMetaObject();
|
100
|
+
this.push(scopeMeta);
|
101
|
+
}
|
102
|
+
|
103
|
+
private push(obj: ObjectItem) {
|
104
|
+
const hashStr = obj.ref.toString();
|
105
|
+
if (this.pushed.includes(hashStr)) {
|
106
|
+
return;
|
107
|
+
}
|
108
|
+
logger.trace('ObjectsReadableGenerator.push', hashStr);
|
109
|
+
this.readable.push(obj);
|
110
|
+
this.pushed.push(hashStr);
|
111
|
+
}
|
112
|
+
private pushManyObjects(objects: ObjectItem[]) {
|
113
|
+
objects.map((obj) => this.push(obj));
|
114
|
+
}
|
115
|
+
|
116
|
+
private async pushComponentObjects(componentWithOptions: ComponentWithCollectOptions): Promise<void> {
|
117
|
+
const { component, collectParents, collectArtifacts, collectParentsUntil, includeVersionHistory } =
|
118
|
+
componentWithOptions;
|
119
|
+
const version = await component.loadVersion(componentWithOptions.version, this.repo, false);
|
120
|
+
if (!version) throw new BitError(`failed loading version ${componentWithOptions.version} of ${component.id()}`);
|
121
|
+
if (collectParentsUntil && version.hash().isEqual(collectParentsUntil)) {
|
122
|
+
return;
|
123
|
+
}
|
124
|
+
const collectVersionObjects = async (ver: Version): Promise<ObjectItem[]> => {
|
125
|
+
const versionRefs = ver.refsWithOptions(false, collectArtifacts);
|
126
|
+
const missingVersionRefs = versionRefs.filter((ref) => !this.pushed.includes(ref.toString()));
|
127
|
+
const versionObjects = await ver.collectManyObjects(this.repo, missingVersionRefs);
|
128
|
+
const versionData = { ref: ver.hash(), buffer: await ver.asRaw(this.repo), type: ver.getType() };
|
129
|
+
return [...versionObjects, versionData];
|
130
|
+
};
|
131
|
+
if (!this.pushed.includes(component.hash().toString())) {
|
132
|
+
const componentData = {
|
133
|
+
ref: component.hash(),
|
134
|
+
buffer: await component.asRaw(this.repo),
|
135
|
+
type: component.getType(),
|
136
|
+
};
|
137
|
+
this.push(componentData);
|
138
|
+
}
|
139
|
+
const allVersions: Version[] = [];
|
140
|
+
if (includeVersionHistory) {
|
141
|
+
const versionHistory = await component.getAndPopulateVersionHistory(this.repo, version.hash());
|
142
|
+
const versionHistoryData = {
|
143
|
+
ref: versionHistory.hash(),
|
144
|
+
buffer: await versionHistory.asRaw(this.repo),
|
145
|
+
type: versionHistory.getType(),
|
146
|
+
};
|
147
|
+
this.push(versionHistoryData);
|
148
|
+
}
|
149
|
+
if (collectParents) {
|
150
|
+
const allParentsHashes = await getAllVersionHashesMemoized({
|
151
|
+
modelComponent: component,
|
152
|
+
repo: this.repo,
|
153
|
+
startFrom: version.hash(),
|
154
|
+
stopAt: collectParentsUntil ? [collectParentsUntil] : undefined,
|
155
|
+
});
|
156
|
+
const missingParentsHashes = allParentsHashes.filter((h) => !h.isEqual(version.hash()));
|
157
|
+
const parentVersions = await pMapSeries(missingParentsHashes, (parentHash) => parentHash.load(this.repo));
|
158
|
+
allVersions.push(...(parentVersions as unknown as Version[]));
|
159
|
+
// note: don't bring the head. otherwise, component-delta of the head won't bring all history of this comp.
|
160
|
+
}
|
161
|
+
allVersions.push(version);
|
162
|
+
await pMapSeries(allVersions, async (ver) => {
|
163
|
+
const versionObjects = await collectVersionObjects(ver);
|
164
|
+
this.pushManyObjects(versionObjects);
|
165
|
+
});
|
166
|
+
}
|
167
|
+
}
|
@@ -0,0 +1,142 @@
|
|
1
|
+
import { NULL_BYTE, SPACE_DELIMITER } from '@teambit/legacy.constants';
|
2
|
+
import { getStringifyArgs, inflate } from '@teambit/legacy.utils';
|
3
|
+
import { typesObj as types } from '@teambit/legacy.scope';
|
4
|
+
import BitObject from './object';
|
5
|
+
import { clone } from 'lodash';
|
6
|
+
|
7
|
+
export default class BitRawObject {
|
8
|
+
headers: string[];
|
9
|
+
type: string;
|
10
|
+
content: Buffer;
|
11
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
12
|
+
parsedContent: any;
|
13
|
+
_ref: string;
|
14
|
+
|
15
|
+
constructor(
|
16
|
+
buffer: Buffer,
|
17
|
+
ref: string | null | undefined,
|
18
|
+
type: string | null | undefined,
|
19
|
+
content: Buffer | null | undefined,
|
20
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
21
|
+
parsedContent: any | null | undefined
|
22
|
+
) {
|
23
|
+
let headers;
|
24
|
+
let contentFromBuffer;
|
25
|
+
if (buffer) {
|
26
|
+
const firstNullByteLocation = buffer.indexOf(NULL_BYTE);
|
27
|
+
headers = buffer.slice(0, firstNullByteLocation).toString();
|
28
|
+
contentFromBuffer = buffer.slice(firstNullByteLocation + 1, buffer.length);
|
29
|
+
}
|
30
|
+
this.content = content || contentFromBuffer;
|
31
|
+
this.headers = headers ? headers.split(SPACE_DELIMITER) : undefined;
|
32
|
+
const typeFromHeader = this.headers ? this.headers[0] : undefined;
|
33
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
34
|
+
this.type = type || typeFromHeader;
|
35
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
36
|
+
this._ref = ref;
|
37
|
+
this.parsedContent = parsedContent || this.getParsedContent();
|
38
|
+
}
|
39
|
+
|
40
|
+
getParsedContent() {
|
41
|
+
if (this.parsedContent) return this.parsedContent;
|
42
|
+
let parsedContent;
|
43
|
+
switch (this.type) {
|
44
|
+
case 'Version':
|
45
|
+
case 'Component':
|
46
|
+
case 'Symlink':
|
47
|
+
case 'ScopeMeta':
|
48
|
+
case 'VersionHistory':
|
49
|
+
parsedContent = JSON.parse(this.content.toString());
|
50
|
+
break;
|
51
|
+
|
52
|
+
default:
|
53
|
+
parsedContent = this.content;
|
54
|
+
}
|
55
|
+
return parsedContent;
|
56
|
+
}
|
57
|
+
|
58
|
+
getString(pretty: boolean) {
|
59
|
+
const args = getStringifyArgs(pretty);
|
60
|
+
switch (this.type) {
|
61
|
+
case 'Version':
|
62
|
+
case 'Component':
|
63
|
+
case 'Symlink':
|
64
|
+
case 'ScopeMeta':
|
65
|
+
case 'VersionHistory':
|
66
|
+
return JSON.stringify(this.parsedContent, ...args);
|
67
|
+
case 'Source':
|
68
|
+
return pretty ? JSON.stringify(JSON.parse(this.content.toString()), ...args) : this.content;
|
69
|
+
|
70
|
+
default:
|
71
|
+
return this.content;
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
76
|
+
set ref(ref: string) {
|
77
|
+
this._ref = ref;
|
78
|
+
}
|
79
|
+
|
80
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
81
|
+
get ref(): string {
|
82
|
+
return this._ref;
|
83
|
+
}
|
84
|
+
|
85
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
86
|
+
get id(): string {
|
87
|
+
switch (this.type) {
|
88
|
+
case 'Version':
|
89
|
+
return 'component version';
|
90
|
+
case 'Component':
|
91
|
+
return this.parsedContent.scope
|
92
|
+
? [this.parsedContent.scope, this.parsedContent.name].join('/')
|
93
|
+
: this.parsedContent.name;
|
94
|
+
case 'Symlink':
|
95
|
+
return this.parsedContent.name;
|
96
|
+
case 'ScopeMeta':
|
97
|
+
return this.parsedContent.name;
|
98
|
+
|
99
|
+
default:
|
100
|
+
return 'component source file';
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
refs(): string[] {
|
105
|
+
if (this.type === 'Component') {
|
106
|
+
return Object.values(this.parsedContent.versions);
|
107
|
+
}
|
108
|
+
if (this.type === 'Version') {
|
109
|
+
const files = this.parsedContent.files ? this.parsedContent.files.map((file) => file.file) : [];
|
110
|
+
const dists = this.parsedContent.dists ? this.parsedContent.dists.map((dist) => dist.file) : [];
|
111
|
+
return [...dists, ...files].filter((ref) => ref);
|
112
|
+
}
|
113
|
+
|
114
|
+
return [];
|
115
|
+
}
|
116
|
+
|
117
|
+
static async fromDeflatedBuffer(
|
118
|
+
fileContents: Buffer,
|
119
|
+
ref: string | null | undefined
|
120
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
121
|
+
): Promise<BitObject> {
|
122
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
123
|
+
return inflate(fileContents).then((buffer) => new BitRawObject(buffer, ref));
|
124
|
+
}
|
125
|
+
|
126
|
+
/**
|
127
|
+
* Build a real object (model) from a parsed content (can be the original parsed conents or a provided one)
|
128
|
+
* We use the provided version during the migration process when we change the parsed content outside
|
129
|
+
* @param {Any} parsedContent
|
130
|
+
*/
|
131
|
+
toRealObject() {
|
132
|
+
// @ts-ignore
|
133
|
+
return types[this.type].from(this.parsedContent || this.getParsedContent(), this.headers[1]);
|
134
|
+
}
|
135
|
+
|
136
|
+
clone() {
|
137
|
+
const parsedContent = this.parsedContent ? clone(this.parsedContent) : undefined;
|
138
|
+
// TODO: Should also clone the buffers (content)
|
139
|
+
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
|
140
|
+
return new BitRawObject(undefined, this._ref, this.type, this.content, parsedContent);
|
141
|
+
}
|
142
|
+
}
|
package/objects/ref.ts
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
import BitObject from './object';
|
2
|
+
import Repository from './repository';
|
3
|
+
|
4
|
+
export default class Ref {
|
5
|
+
hash: string;
|
6
|
+
|
7
|
+
constructor(hash: string) {
|
8
|
+
if (!hash) throw new Error('failed creating a Ref object, the hash argument is empty');
|
9
|
+
this.hash = hash;
|
10
|
+
}
|
11
|
+
|
12
|
+
toString() {
|
13
|
+
// reason for hash.toString() is when working with short hash, it's possible that all characters are numbers
|
14
|
+
// so it's needed to convert it to string
|
15
|
+
return this.hash.toString();
|
16
|
+
}
|
17
|
+
|
18
|
+
toShortString(numOfChars = 9) {
|
19
|
+
return this.hash.substring(0, numOfChars).toString();
|
20
|
+
}
|
21
|
+
|
22
|
+
load(repository: Repository, throws = false): Promise<BitObject> {
|
23
|
+
return repository.load(this, throws);
|
24
|
+
}
|
25
|
+
|
26
|
+
loadSync(repo: Repository, throws = true): BitObject {
|
27
|
+
return repo.loadSync(this, throws);
|
28
|
+
}
|
29
|
+
|
30
|
+
loadRaw(repo: Repository): Promise<Buffer> {
|
31
|
+
return repo.loadRaw(this);
|
32
|
+
}
|
33
|
+
|
34
|
+
isEqual(ref: Ref): boolean {
|
35
|
+
return this.toString() === ref.toString();
|
36
|
+
}
|
37
|
+
|
38
|
+
clone() {
|
39
|
+
return new Ref(this.hash);
|
40
|
+
}
|
41
|
+
|
42
|
+
static from(hash: string): Ref {
|
43
|
+
return new Ref(hash);
|
44
|
+
}
|
45
|
+
}
|
@@ -0,0 +1,42 @@
|
|
1
|
+
import isRelative from 'is-relative-path';
|
2
|
+
import path from 'path';
|
3
|
+
|
4
|
+
import { ScopeJson } from '@teambit/legacy.scope';
|
5
|
+
|
6
|
+
export type ContentTransformer = (content: Buffer) => Buffer;
|
7
|
+
|
8
|
+
function loadHooks(scopePath: string, scopeJson: ScopeJson): any | undefined {
|
9
|
+
const hooksPath = scopeJson.hooksPath;
|
10
|
+
if (hooksPath) {
|
11
|
+
const hooksFinalPath = isRelative(hooksPath) ? path.join(scopePath, hooksPath) : hooksPath;
|
12
|
+
// eslint-disable-next-line global-require, import/no-dynamic-require
|
13
|
+
const hooks = require(hooksFinalPath);
|
14
|
+
return hooks;
|
15
|
+
}
|
16
|
+
return undefined;
|
17
|
+
}
|
18
|
+
|
19
|
+
export function onPersist(scopePath: string, scopeJson: ScopeJson): ContentTransformer {
|
20
|
+
const defaultFunc = (content) => content;
|
21
|
+
const hooks = loadHooks(scopePath, scopeJson);
|
22
|
+
if (hooks) {
|
23
|
+
const onReadFunction = hooks.onPersist;
|
24
|
+
if (onReadFunction) {
|
25
|
+
return onReadFunction;
|
26
|
+
}
|
27
|
+
}
|
28
|
+
return defaultFunc;
|
29
|
+
}
|
30
|
+
|
31
|
+
export function onRead(scopePath: string, scopeJson: ScopeJson): ContentTransformer {
|
32
|
+
const defaultFunc = (content) => content;
|
33
|
+
|
34
|
+
const hooks = loadHooks(scopePath, scopeJson);
|
35
|
+
if (hooks) {
|
36
|
+
const onReadFunction = hooks.onRead;
|
37
|
+
if (onReadFunction) {
|
38
|
+
return onReadFunction;
|
39
|
+
}
|
40
|
+
}
|
41
|
+
return defaultFunc;
|
42
|
+
}
|