Skip to content

Commit

Permalink
[TA-2736] Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
LaberionAjvazi committed Jan 23, 2024
1 parent 768d669 commit 090bd52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BatchImportExportService {

exportedPackagesZip.getEntries().forEach(entry => {
if (entry.name.endsWith(".zip")) {
const updatedPackage = studioService.getPackageWithoutActionFlowsAndFixConnectionVariables(entry, exportedVariables);
const updatedPackage = studioService.getPackageWithoutActionFlowsAndFixedConnectionVariables(entry, exportedVariables);

exportedPackagesZip.updateFile(entry, updatedPackage.toBuffer());
}
Expand Down
43 changes: 21 additions & 22 deletions src/services/studio/studio.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
NodeExportTransport, NodeSerializedContent,
NodeExportTransport,
NodeSerializedContent,
PackageExportTransport,
PackageManifestTransport, StudioPackageManifest, VariableExportTransport,
PackageManifestTransport,
StudioPackageManifest,
VariableExportTransport,
VariableManifestTransport
} from "../../interfaces/package-export-transport";
import {packageApi} from "../../api/package-api";
Expand All @@ -11,9 +14,9 @@ import {
StudioComputeNodeDescriptor
} from "../../interfaces/package-manager.interfaces";
import {dataModelService} from "../package-manager/datamodel-service";
import AdmZip = require("adm-zip");
import {IZipEntry} from "adm-zip";
import {parse, stringify} from "../../util/yaml";
import AdmZip = require("adm-zip");

class StudioService {

Expand Down Expand Up @@ -63,7 +66,7 @@ class StudioService {
return studioManifests;
}

public getPackageWithoutActionFlowsAndFixConnectionVariables(exportedPackage: IZipEntry, exportedVariables: VariableManifestTransport[]): AdmZip {
public getPackageWithoutActionFlowsAndFixedConnectionVariables(exportedPackage: IZipEntry, exportedVariables: VariableManifestTransport[]): AdmZip {
const packageZip = new AdmZip(exportedPackage.getData());
packageZip.getEntries().forEach(entry => {
if (entry.entryName.startsWith("nodes/") && entry.entryName.endsWith(".yml")) {
Expand All @@ -75,25 +78,19 @@ class StudioService {

if (entry.name === "package.yml") {
const packageKeyAndVersion = exportedPackage.name.replace(".zip", "").split("_");
const connectionVariables = this.getConnectionVariablesForPackage(packageKeyAndVersion[0], packageKeyAndVersion[1], exportedVariables);

if (connectionVariables.length) {
const variableValuesByKey = new Map<string, VariableExportTransport>();
connectionVariables.forEach(variable => variableValuesByKey.set(variable.key, variable));
const connectionVariablesByKey = this.getConnectionVariablesByKeyForPackage(packageKeyAndVersion[0], packageKeyAndVersion[1], exportedVariables);

if (connectionVariablesByKey.size) {
const exportedNode: NodeExportTransport = parse(entry.getData().toString());
const nodeContent: NodeSerializedContent = parse(exportedNode.serializedContent);

nodeContent.variables = nodeContent.variables.map(variable => {
if (variable.type === PackageManagerVariableType.CONNECTION) {
variable = {
...variable,
metadata: variableValuesByKey.get(variable.key).metadata
}
}

return variable;
})
nodeContent.variables = nodeContent.variables.map(variable => ({
...variable,
metadata: variable.type === PackageManagerVariableType.CONNECTION ? {
...variable.metadata,
...connectionVariablesByKey.get(variable.key).metadata
} : { ...variable.metadata }
}));

exportedNode.serializedContent = stringify(nodeContent);
packageZip.updateFile(entry, Buffer.from(stringify(exportedNode)));
Expand Down Expand Up @@ -135,14 +132,16 @@ class StudioService {
});
}

private getConnectionVariablesForPackage(packageKey: string, packageVersion: string, variables: VariableManifestTransport[]): VariableExportTransport[] {
private getConnectionVariablesByKeyForPackage(packageKey: string, packageVersion: string, variables: VariableManifestTransport[]): Map<string, VariableExportTransport> {
const variablesByKey = new Map<string, VariableExportTransport>();
const packageVariables = variables.find(exportedVariable => exportedVariable.packageKey === packageKey && exportedVariable.version === packageVersion);

if (packageVariables && packageVariables.variables.length) {
return packageVariables.variables.filter(variable => variable.type === PackageManagerVariableType.CONNECTION);
packageVariables.variables.filter(variable => variable.type === PackageManagerVariableType.CONNECTION)
.forEach(variable => variablesByKey.set(variable.key, variable));
}

return [];
return variablesByKey;
}
}

Expand Down

0 comments on commit 090bd52

Please sign in to comment.