-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/main' into N21-2022-close-wizard-with-migration
- Loading branch information
Showing
92 changed files
with
1,872 additions
and
998 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 70 additions & 84 deletions
154
...server/src/modules/common-cartridge/export/builders/common-cartridge-file-builder.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 40 additions & 39 deletions
79
apps/server/src/modules/common-cartridge/export/builders/common-cartridge-file-builder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,81 @@ | ||
import AdmZip from 'adm-zip'; | ||
import { CommonCartridgeResourceType, CommonCartridgeVersion } from '../common-cartridge.enums'; | ||
import { | ||
CommonCartridgeElementType, | ||
CommonCartridgeResourceType, | ||
CommonCartridgeVersion, | ||
} from '../common-cartridge.enums'; | ||
import { | ||
CommonCartridgeElementFactory, | ||
CommonCartridgeElementProps, | ||
} from '../elements/common-cartridge-element-factory'; | ||
import { CommonCartridgeElement, CommonCartridgeResource } from '../interfaces'; | ||
import { MissingMetadataLoggableException } from '../errors'; | ||
import { CommonCartridgeElement } from '../interfaces'; | ||
import { CommonCartridgeResourceFactory } from '../resources/common-cartridge-resource-factory'; | ||
import { OmitVersion } from '../utils'; | ||
import { | ||
CommonCartridgeOrganizationBuilder, | ||
CommonCartridgeOrganizationBuilderOptions, | ||
} from './common-cartridge-organization-builder'; | ||
CommonCartridgeOrganizationNode, | ||
CommonCartridgeOrganizationNodeProps, | ||
} from './common-cartridge-organization-node'; | ||
import { CommonCartridgeResourceCollectionBuilder } from './common-cartridge-resource-collection-builder'; | ||
|
||
export type CommonCartridgeFileBuilderProps = { | ||
version: CommonCartridgeVersion; | ||
identifier: string; | ||
}; | ||
|
||
export class CommonCartridgeFileBuilder { | ||
private readonly archive: AdmZip = new AdmZip(); | ||
export type CommonCartridgeOrganizationProps = Omit<CommonCartridgeOrganizationNodeProps, 'version' | 'type'>; | ||
|
||
private readonly organizationBuilders = new Array<CommonCartridgeOrganizationBuilder>(); | ||
export class CommonCartridgeFileBuilder { | ||
private readonly resourcesBuilder: CommonCartridgeResourceCollectionBuilder = | ||
new CommonCartridgeResourceCollectionBuilder(); | ||
|
||
private readonly resources = new Array<CommonCartridgeResource>(); | ||
private readonly organizationsRoot: CommonCartridgeOrganizationNode[] = []; | ||
|
||
private metadata?: CommonCartridgeElement; | ||
private metadataElement: CommonCartridgeElement | null = null; | ||
|
||
constructor(private readonly props: CommonCartridgeFileBuilderProps) {} | ||
|
||
public addMetadata(props: CommonCartridgeElementProps): CommonCartridgeFileBuilder { | ||
this.metadata = CommonCartridgeElementFactory.createElement({ | ||
public addMetadata(metadataProps: CommonCartridgeElementProps): void { | ||
this.metadataElement = CommonCartridgeElementFactory.createElement({ | ||
version: this.props.version, | ||
...props, | ||
...metadataProps, | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
public addOrganization( | ||
props: OmitVersion<CommonCartridgeOrganizationBuilderOptions> | ||
): CommonCartridgeOrganizationBuilder { | ||
const builder = new CommonCartridgeOrganizationBuilder( | ||
{ ...props, version: this.props.version }, | ||
(resource: CommonCartridgeResource) => this.resources.push(resource) | ||
public createOrganization(organizationProps: CommonCartridgeOrganizationProps): CommonCartridgeOrganizationNode { | ||
const organization = new CommonCartridgeOrganizationNode( | ||
{ ...organizationProps, version: this.props.version, type: CommonCartridgeElementType.ORGANIZATION }, | ||
this.resourcesBuilder, | ||
null | ||
); | ||
|
||
this.organizationBuilders.push(builder); | ||
this.organizationsRoot.push(organization); | ||
|
||
return builder; | ||
return organization; | ||
} | ||
|
||
public async build(): Promise<Buffer> { | ||
if (!this.metadata) { | ||
throw new Error('Metadata is not defined'); | ||
public build(): Buffer { | ||
if (!this.metadataElement) { | ||
throw new MissingMetadataLoggableException(); | ||
} | ||
|
||
const organizations = this.organizationBuilders.map((builder) => builder.build()); | ||
const archive = new AdmZip(); | ||
const organizations = this.organizationsRoot.map((organization) => organization.build()); | ||
const resources = this.resourcesBuilder.build(); | ||
const manifest = CommonCartridgeResourceFactory.createResource({ | ||
type: CommonCartridgeResourceType.MANIFEST, | ||
version: this.props.version, | ||
identifier: this.props.identifier, | ||
metadata: this.metadata, | ||
metadata: this.metadataElement, | ||
organizations, | ||
resources: this.resources, | ||
resources, | ||
}); | ||
|
||
for (const resources of this.resources) { | ||
if (!resources.canInline()) { | ||
this.archive.addFile(resources.getFilePath(), Buffer.from(resources.getFileContent())); | ||
} | ||
} | ||
|
||
this.archive.addFile(manifest.getFilePath(), Buffer.from(manifest.getFileContent())); | ||
archive.addFile(manifest.getFilePath(), Buffer.from(manifest.getFileContent())); | ||
|
||
const buffer = await this.archive.toBufferPromise(); | ||
resources.forEach((resource) => { | ||
archive.addFile(resource.getFilePath(), Buffer.from(resource.getFileContent())); | ||
}); | ||
|
||
return buffer; | ||
return archive.toBuffer(); | ||
} | ||
} |
Oops, something went wrong.