@celonis/content-cli 0.7.0 → 0.8.0

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.
@@ -29,6 +29,22 @@
29
29
  throw new logger_1.FatalError(`Problem getting active packages by keys: ${e}`);
30
30
  });
31
31
  }
32
+ exportPackages(packageKeys, withDependencies = false) {
33
+ const queryParams = new URLSearchParams();
34
+ packageKeys.forEach(packageKey => queryParams.append("packageKeys", packageKey));
35
+ queryParams.set("withDependencies", withDependencies.toString());
36
+ return http_client_service_v2_1.httpClientV2.getFile(`/package-manager/api/core/packages/export/batch?${queryParams.toString()}`).catch(e => {
37
+ throw new logger_1.FatalError(`Problem exporting packages: ${e}`);
38
+ });
39
+ }
40
+ importPackages(data, overwrite) {
41
+ return http_client_service_v2_1.httpClientV2.postFile("/package-manager/api/core/packages/import/batch", data, { overwrite });
42
+ }
43
+ findVariablesWithValuesByPackageKeysAndVersion(packagesByKeyAndVersion) {
44
+ return http_client_service_v2_1.httpClientV2.post("/package-manager/api/core/packages/export/batch/variables-with-assignments", packagesByKeyAndVersion).catch(e => {
45
+ throw new logger_1.FatalError(`Problem exporting package variables: ${e}`);
46
+ });
47
+ }
32
48
  }
33
49
  BatchImportExportApi.INSTANCE = new BatchImportExportApi();
34
50
  exports.batchImportExportApi = BatchImportExportApi.INSTANCE;
@@ -13,7 +13,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  if (v !== undefined) module.exports = v;
14
14
  }
15
15
  else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "../services/http-client-service.v2", "../util/logger", "./variable-assignment-apis"], factory);
16
+ define(["require", "exports", "../services/http-client-service.v2", "../util/logger", "./variable-assignment-apis", "url"], factory);
17
17
  }
18
18
  })(function (require, exports) {
19
19
  "use strict";
@@ -22,6 +22,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
22
22
  const http_client_service_v2_1 = require("../services/http-client-service.v2");
23
23
  const logger_1 = require("../util/logger");
24
24
  const variable_assignment_apis_1 = require("./variable-assignment-apis");
25
+ const url_1 = require("url");
25
26
  class VariablesApi {
26
27
  assignVariableValues(packageKey, variablesAssignments) {
27
28
  return __awaiter(this, void 0, void 0, function* () {
@@ -41,6 +42,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
41
42
  });
42
43
  });
43
44
  }
45
+ getRuntimeVariableValues(packageKey, appMode) {
46
+ const queryParams = new url_1.URLSearchParams();
47
+ queryParams.set("appMode", appMode);
48
+ return http_client_service_v2_1.httpClientV2.get(`/package-manager/api/nodes/by-package-key/${packageKey}/variables/runtime-values?${queryParams.toString()}`).catch(e => {
49
+ throw new logger_1.FatalError(`Problem getting runtime variables of package ${packageKey}: ${e}`);
50
+ });
51
+ }
44
52
  }
45
53
  VariablesApi.INSTANCE = new VariablesApi();
46
54
  VariablesApi.ASSIGNMENT_APIS = variable_assignment_apis_1.variableAssignmentApis;
@@ -13,13 +13,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  if (v !== undefined) module.exports = v;
14
14
  }
15
15
  else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "../services/package-manager/batch-import-export-service"], factory);
16
+ define(["require", "exports", "../services/package-manager/batch-import-export-service", "../services/package-manager/variable-service"], factory);
17
17
  }
18
18
  })(function (require, exports) {
19
19
  "use strict";
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
21
  exports.ConfigCommand = void 0;
22
22
  const batch_import_export_service_1 = require("../services/package-manager/batch-import-export-service");
23
+ const variable_service_1 = require("../services/package-manager/variable-service");
23
24
  class ConfigCommand {
24
25
  listActivePackages(jsonResponse, flavors, withDependencies, packageKeys) {
25
26
  return __awaiter(this, void 0, void 0, function* () {
@@ -31,6 +32,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
32
  }
32
33
  });
33
34
  }
35
+ listVariables(jsonResponse, keysByVersion, keysByVersionFile) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ if (jsonResponse) {
38
+ yield variable_service_1.variableService.exportVariables(keysByVersion, keysByVersionFile);
39
+ }
40
+ else {
41
+ yield variable_service_1.variableService.listVariables(keysByVersion, keysByVersionFile);
42
+ }
43
+ });
44
+ }
45
+ batchExportPackages(packageKeys, withDependencies = false) {
46
+ return batch_import_export_service_1.batchImportExportService.batchExportPackages(packageKeys, withDependencies);
47
+ }
48
+ batchImportPackages(file, overwrite) {
49
+ return batch_import_export_service_1.batchImportExportService.batchImportPackages(file, overwrite);
50
+ }
34
51
  }
35
52
  exports.ConfigCommand = ConfigCommand;
36
53
  });
@@ -39,17 +39,59 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
39
39
  }));
40
40
  return program;
41
41
  }
42
+ static listVariables(program) {
43
+ program
44
+ .command("variables")
45
+ .description("Commands related to variable configs")
46
+ .command("list")
47
+ .description("Command to list versioned variables of packages")
48
+ .option("-p, --profile <profile>", "Profile which you want to use to list packages")
49
+ .option("--json", "Return response as json type", "")
50
+ .option("--keysByVersion <keysByVersion...>", "Mapping of package keys and versions", "")
51
+ .option("--keysByVersionFile <keysByVersionFile>", "Package keys by version mappings file path.", "")
52
+ .action((cmd) => __awaiter(this, void 0, void 0, function* () {
53
+ yield new config_command_1.ConfigCommand().listVariables(cmd.json, cmd.keysByVersion, cmd.keysByVersionFile);
54
+ process.exit();
55
+ }));
56
+ return program;
57
+ }
58
+ static export(program) {
59
+ program
60
+ .command("export")
61
+ .description("Command to export package configs")
62
+ .option("-p, --profile <profile>", "Profile which you want to use to export packages")
63
+ .requiredOption("--packageKeys <packageKeys...>", "Keys of packages to export")
64
+ .option("--withDependencies", "Include variables and dependencies", "")
65
+ .action((cmd) => __awaiter(this, void 0, void 0, function* () {
66
+ yield new config_command_1.ConfigCommand().batchExportPackages(cmd.packageKeys, cmd.withDependencies);
67
+ process.exit();
68
+ }));
69
+ return program;
70
+ }
71
+ static import(program) {
72
+ program
73
+ .command("import")
74
+ .description("Command to import package configs")
75
+ .option("-p, --profile <profile>", "Profile which you want to use to import packages")
76
+ .option("--overwrite", "Flag to allow overwriting of packages")
77
+ .requiredOption("-f, --file <file>", "Exported packages file (relative path)")
78
+ .action((cmd) => __awaiter(this, void 0, void 0, function* () {
79
+ yield new config_command_1.ConfigCommand().batchImportPackages(cmd.file, cmd.overwrite);
80
+ process.exit();
81
+ }));
82
+ return program;
83
+ }
42
84
  }
43
85
  exports.Config = Config;
44
- process.on("unhandledRejection", (e, promise) => {
45
- logger_1.logger.error(e.toString());
46
- });
47
86
  const loadAllCommands = () => {
48
87
  Config.list(commander);
88
+ Config.listVariables(commander);
89
+ Config.export(commander);
90
+ Config.import(commander);
49
91
  commander.parse(process.argv);
50
92
  };
51
93
  context_initializer_1.ContextInitializer.initContext()
52
- .then(loadAllCommands)
94
+ .then(loadAllCommands, loadAllCommands)
53
95
  .catch(e => {
54
96
  logger_1.logger.error(e);
55
97
  });
@@ -108,9 +108,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
108
108
  }
109
109
  }
110
110
  exports.List = List;
111
- process.on("unhandledRejection", (e, promise) => {
112
- logger_1.logger.error(e.toString());
113
- });
114
111
  const loadAllCommands = () => {
115
112
  List.packages(commander);
116
113
  List.spaces(commander);
@@ -121,7 +118,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
121
118
  commander.parse(process.argv);
122
119
  };
123
120
  context_initializer_1.ContextInitializer.initContext()
124
- .then(loadAllCommands)
121
+ .then(loadAllCommands, loadAllCommands)
125
122
  .catch(e => {
126
123
  logger_1.logger.error(e);
127
124
  });
@@ -39,11 +39,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
39
39
  return program;
40
40
  }
41
41
  }
42
- context_initializer_1.ContextInitializer.initContext()
43
- .then(() => {
42
+ const loadAllCommands = () => {
44
43
  Set.connection(commander);
45
44
  commander.parse(process.argv);
46
- })
45
+ };
46
+ context_initializer_1.ContextInitializer.initContext()
47
+ .then(loadAllCommands, loadAllCommands)
47
48
  .catch(e => {
48
49
  logger_1.logger.error(e);
49
50
  });
package/content-cli.js CHANGED
@@ -28,7 +28,7 @@
28
28
  program.command("list", "Commands to list content.").alias("ls");
29
29
  program.command("get", "Commands to get configuration properties.");
30
30
  program.command("set", "Commands to set configuration properties.");
31
- program.command("config", "Commands related to config management");
31
+ program.command("config", "Commands related to config management.");
32
32
  program.version(version_1.VersionUtils.getCurrentCliVersion());
33
33
  program.parse(process.argv);
34
34
  if (!process.argv.slice(2).length) {
@@ -0,0 +1,25 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.BatchExportImportConstants = void 0;
13
+ var BatchExportImportConstants;
14
+ (function (BatchExportImportConstants) {
15
+ BatchExportImportConstants["STUDIO_FILE_NAME"] = "studio.yml";
16
+ BatchExportImportConstants["VARIABLES_FILE_NAME"] = "variables.yml";
17
+ BatchExportImportConstants["MANIFEST_FILE_NAME"] = "manifest.yml";
18
+ BatchExportImportConstants["STUDIO"] = "STUDIO";
19
+ BatchExportImportConstants["APP_MODE_VIEWER"] = "VIEWER";
20
+ BatchExportImportConstants["ZIP_EXTENSION"] = ".zip";
21
+ BatchExportImportConstants["YAML_EXTENSION"] = ".yml";
22
+ BatchExportImportConstants["NODES_FOLDER_NAME"] = "nodes/";
23
+ BatchExportImportConstants["SCENARIO_NODE"] = "SCENARIO";
24
+ })(BatchExportImportConstants || (exports.BatchExportImportConstants = BatchExportImportConstants = {}));
25
+ });
@@ -1,8 +1,13 @@
1
- import { PackageExportTransport } from "../interfaces/package-export-transport";
1
+ /// <reference types="node" />
2
+ import { PackageExportTransport, PackageKeyAndVersionPair, PostPackageImportData, VariableManifestTransport } from "../interfaces/package-export-transport";
3
+ import * as FormData from "form-data";
2
4
  declare class BatchImportExportApi {
3
5
  static readonly INSTANCE: BatchImportExportApi;
4
6
  findAllActivePackages(flavors: string[], withDependencies?: boolean): Promise<PackageExportTransport[]>;
5
7
  findActivePackagesByKeys(packageKeys: string[], withDependencies?: boolean): Promise<PackageExportTransport[]>;
8
+ exportPackages(packageKeys: string[], withDependencies?: boolean): Promise<Buffer>;
9
+ importPackages(data: FormData, overwrite: boolean): Promise<PostPackageImportData[]>;
10
+ findVariablesWithValuesByPackageKeysAndVersion(packagesByKeyAndVersion: PackageKeyAndVersionPair[]): Promise<VariableManifestTransport[]>;
6
11
  }
7
12
  export declare const batchImportExportApi: BatchImportExportApi;
8
13
  export {};
@@ -8,6 +8,7 @@ declare class VariablesApi {
8
8
  };
9
9
  assignVariableValues(packageKey: string, variablesAssignments: VariablesAssignments[]): Promise<ContentNodeTransport[]>;
10
10
  getCandidateAssignments(type: string, params: URLSearchParams): Promise<object[]>;
11
+ getRuntimeVariableValues(packageKey: string, appMode: string): Promise<VariablesAssignments[]>;
11
12
  }
12
13
  export declare const variablesApi: VariablesApi;
13
14
  export {};
@@ -1,3 +1,6 @@
1
1
  export declare class ConfigCommand {
2
2
  listActivePackages(jsonResponse: boolean, flavors: string[], withDependencies: boolean, packageKeys: string[]): Promise<void>;
3
+ listVariables(jsonResponse: boolean, keysByVersion: string[], keysByVersionFile: string): Promise<void>;
4
+ batchExportPackages(packageKeys: string[], withDependencies?: boolean): Promise<void>;
5
+ batchImportPackages(file: string, overwrite: boolean): Promise<void>;
3
6
  }
@@ -2,5 +2,8 @@ import commander = require("commander");
2
2
  type CommanderStatic = commander.CommanderStatic;
3
3
  export declare class Config {
4
4
  static list(program: CommanderStatic): CommanderStatic;
5
+ static listVariables(program: CommanderStatic): CommanderStatic;
6
+ static export(program: CommanderStatic): CommanderStatic;
7
+ static import(program: CommanderStatic): CommanderStatic;
5
8
  }
6
9
  export {};
@@ -0,0 +1,11 @@
1
+ export declare enum BatchExportImportConstants {
2
+ STUDIO_FILE_NAME = "studio.yml",
3
+ VARIABLES_FILE_NAME = "variables.yml",
4
+ MANIFEST_FILE_NAME = "manifest.yml",
5
+ STUDIO = "STUDIO",
6
+ APP_MODE_VIEWER = "VIEWER",
7
+ ZIP_EXTENSION = ".zip",
8
+ YAML_EXTENSION = ".yml",
9
+ NODES_FOLDER_NAME = "nodes/",
10
+ SCENARIO_NODE = "SCENARIO"
11
+ }
@@ -1,4 +1,6 @@
1
- import { StudioComputeNodeDescriptor } from "./package-manager.interfaces";
1
+ /// <reference types="node" />
2
+ import { StudioComputeNodeDescriptor, VariableDefinition, VariablesAssignments } from "./package-manager.interfaces";
3
+ import { SpaceTransport } from "./save-space.interface";
2
4
  export interface DependencyTransport {
3
5
  key: string;
4
6
  version: string;
@@ -16,3 +18,54 @@ export interface PackageExportTransport {
16
18
  spaceId?: string;
17
19
  datamodels?: StudioComputeNodeDescriptor[];
18
20
  }
21
+ export interface PackageManifestTransport {
22
+ packageKey: string;
23
+ flavor: string;
24
+ activeVersion: string;
25
+ dependenciesByVersion: Map<string, DependencyTransport[]>;
26
+ }
27
+ export interface VariableExportTransport {
28
+ key: string;
29
+ value: any;
30
+ type: string;
31
+ metadata: object;
32
+ }
33
+ export interface VariableManifestTransport {
34
+ packageKey: string;
35
+ version: string;
36
+ variables?: VariableExportTransport[];
37
+ }
38
+ export interface PackageKeyAndVersionPair {
39
+ packageKey: string;
40
+ version: string;
41
+ }
42
+ export interface NodeExportTransport {
43
+ key: string;
44
+ parentNodeKey: string;
45
+ packageNodeKey: string;
46
+ name: string;
47
+ type: string;
48
+ exportSerializationType: string;
49
+ serializedContent: string;
50
+ schemaVersion: number;
51
+ unversionedMetadata: any;
52
+ versionedMetdata: object;
53
+ invalidContent?: boolean;
54
+ serializedDocument: Buffer;
55
+ }
56
+ export interface NodeSerializedContent {
57
+ variables: VariableDefinition[];
58
+ }
59
+ export interface StudioPackageManifest {
60
+ packageKey: string;
61
+ space: Partial<SpaceTransport>;
62
+ runtimeVariableAssignments: VariablesAssignments[];
63
+ }
64
+ export interface PackageVersionImport {
65
+ oldVersion: string;
66
+ newVersion: string;
67
+ }
68
+ export interface PostPackageImportData {
69
+ packageKey: string;
70
+ importedVersions: PackageVersionImport[];
71
+ }
@@ -50,7 +50,7 @@ export interface VariablesAssignments {
50
50
  value: object;
51
51
  type: string;
52
52
  }
53
- export interface VariableDefinitionWithValue {
53
+ export interface VariableDefinition {
54
54
  key: string;
55
55
  type: PackageManagerVariableType;
56
56
  description?: string;
@@ -1,6 +1,8 @@
1
+ import * as FormData from "form-data";
1
2
  declare class HttpClientServiceV2 {
2
3
  get(url: string): Promise<any>;
3
- postFile(url: string, body: any, parameters?: {}): Promise<any>;
4
+ getFile(url: string): Promise<any>;
5
+ postFile(url: string, formData: FormData, parameters?: {}): Promise<any>;
4
6
  post(url: string, body: any): Promise<any>;
5
7
  put(url: string, body: object): Promise<any>;
6
8
  delete(url: string): Promise<any>;
@@ -1,7 +1,14 @@
1
1
  declare class BatchImportExportService {
2
2
  listActivePackages(flavors: string[]): Promise<void>;
3
3
  findAndExportListOfActivePackages(flavors: string[], packageKeys: string[], withDependencies: boolean): Promise<void>;
4
+ batchExportPackages(packageKeys: string[], withDependencies?: boolean): Promise<void>;
5
+ batchImportPackages(file: string, overwrite: boolean): Promise<void>;
4
6
  private exportListOfPackages;
7
+ private getVersionsByPackageKey;
8
+ private getVersionedVariablesForPackagesWithKeys;
9
+ private buildBodyForImport;
10
+ private getReadableStream;
11
+ private parseEntryData;
5
12
  }
6
13
  export declare const batchImportExportService: BatchImportExportService;
7
14
  export {};
@@ -4,7 +4,13 @@ declare class VariableService {
4
4
  assignVariableValues(packageKey: string, variablesAssignments: VariablesAssignments[]): Promise<void>;
5
5
  listCandidateAssignments(type: string, params: string): Promise<void>;
6
6
  findAndExportCandidateAssignments(type: string, params: string): Promise<void>;
7
+ listVariables(keysByVersion: string[], keysByVersionFile: string): Promise<void>;
8
+ exportVariables(keysByVersion: string[], keysByVersionFile: string): Promise<void>;
7
9
  private parseParams;
10
+ private getVersionedVariablesByKeyVersionPairs;
11
+ private buildKeyVersionPairs;
12
+ private buildKeyAndVersionPairsFromArrayInput;
13
+ private exportToJson;
8
14
  }
9
15
  export declare const variableService: VariableService;
10
16
  export {};
@@ -1,8 +1,25 @@
1
- import { PackageExportTransport } from "../../interfaces/package-export-transport";
1
+ import { PackageExportTransport, StudioPackageManifest, VariableManifestTransport } from "../../interfaces/package-export-transport";
2
+ import { ContentNodeTransport } from "../../interfaces/package-manager.interfaces";
3
+ import { IZipEntry } from "adm-zip";
4
+ import * as AdmZip from "adm-zip";
2
5
  declare class StudioService {
3
6
  getExportPackagesWithStudioData(packagesToExport: PackageExportTransport[], withDependencies: boolean): Promise<PackageExportTransport[]>;
7
+ fixConnectionVariables(variables: VariableManifestTransport[]): VariableManifestTransport[];
8
+ getStudioPackageManifests(studioPackageKeys: string[]): Promise<StudioPackageManifest[]>;
9
+ processPackageForExport(exportedPackage: IZipEntry, exportedVariables: VariableManifestTransport[]): AdmZip;
10
+ processImportedPackages(configs: AdmZip, existingStudioPackages: ContentNodeTransport[], studioManifests: StudioPackageManifest[]): Promise<void>;
4
11
  private setSpaceIdForStudioPackages;
5
12
  private setDataModelsForStudioPackages;
13
+ private fixConnectionVariable;
14
+ private deleteScenarioAssets;
15
+ private fixConnectionVariablesForRootNodeFiles;
16
+ private getPackageKeyAndVersion;
17
+ private getConnectionVariablesByKeyForPackage;
18
+ mapSpaces(exportedFiles: AdmZip, studioManifests: StudioPackageManifest[]): Promise<AdmZip>;
19
+ private isStudioPackage;
20
+ private findDesiredSpaceIdForPackage;
21
+ private assignRuntimeVariables;
22
+ private updateSpaceIdForNode;
6
23
  }
7
24
  export declare const studioService: StudioService;
8
25
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celonis/content-cli",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "CLI Tool to help manage content in Celonis EMS",
5
5
  "main": "content-cli.js",
6
6
  "bin": {
@@ -13,7 +13,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  if (v !== undefined) module.exports = v;
14
14
  }
15
15
  else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "../interfaces/profile.interface", "./context.service", "../util/logger", "../util/tracing", "../util/version", "axios", "form-data"], factory);
16
+ define(["require", "exports", "../interfaces/profile.interface", "./context.service", "../util/logger", "../util/tracing", "../util/version", "axios"], factory);
17
17
  }
18
18
  })(function (require, exports) {
19
19
  "use strict";
@@ -25,7 +25,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
25
25
  const tracing_1 = require("../util/tracing");
26
26
  const version_1 = require("../util/version");
27
27
  const axios_1 = require("axios");
28
- const FormData = require("form-data");
29
28
  class HttpClientServiceV2 {
30
29
  get(url) {
31
30
  return __awaiter(this, void 0, void 0, function* () {
@@ -42,11 +41,34 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
42
41
  });
43
42
  });
44
43
  }
45
- postFile(url, body, parameters) {
44
+ getFile(url) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ return new Promise((resolve, reject) => {
47
+ axios_1.default.get(this.resolveUrl(url), {
48
+ headers: this.buildHeaders(context_service_1.contextService.getContext().profile),
49
+ responseType: "stream",
50
+ validateStatus: status => status >= 200
51
+ }).then(response => {
52
+ const data = [];
53
+ response.data.on("data", (chunk) => {
54
+ data.push(chunk);
55
+ });
56
+ response.data.on("end", () => {
57
+ if (response.status !== 200) {
58
+ reject(Buffer.concat(data).toString());
59
+ return;
60
+ }
61
+ this.handleResponseStreamData(Buffer.concat(data), resolve, reject);
62
+ });
63
+ }).catch(err => {
64
+ this.handleError(err, resolve, reject);
65
+ });
66
+ });
67
+ });
68
+ }
69
+ postFile(url, formData, parameters) {
46
70
  return __awaiter(this, void 0, void 0, function* () {
47
71
  return new Promise((resolve, reject) => {
48
- const formData = new FormData();
49
- formData.append("package", body.formData.package);
50
72
  axios_1.default.post(this.resolveUrl(url), formData, {
51
73
  headers: Object.assign(Object.assign({}, this.buildHeaders(context_service_1.contextService.getContext().profile, "multipart/form-data")), formData.getHeaders()),
52
74
  params: parameters
@@ -13,7 +13,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  if (v !== undefined) module.exports = v;
14
14
  }
15
15
  else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "../../api/batch-import-export-api", "../../util/logger", "uuid", "../file-service", "../studio/studio.service"], factory);
16
+ define(["require", "exports", "../../api/batch-import-export-api", "../../util/logger", "uuid", "../file-service", "../studio/studio.service", "../../util/yaml", "form-data", "../../interfaces/batch-export-import-constants", "../../api/package-api", "stream", "adm-zip"], factory);
17
17
  }
18
18
  })(function (require, exports) {
19
19
  "use strict";
@@ -24,6 +24,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
24
24
  const uuid_1 = require("uuid");
25
25
  const file_service_1 = require("../file-service");
26
26
  const studio_service_1 = require("../studio/studio.service");
27
+ const yaml_1 = require("../../util/yaml");
28
+ const FormData = require("form-data");
29
+ const batch_export_import_constants_1 = require("../../interfaces/batch-export-import-constants");
30
+ const package_api_1 = require("../../api/package-api");
31
+ const stream_1 = require("stream");
32
+ const AdmZip = require("adm-zip");
27
33
  class BatchImportExportService {
28
34
  listActivePackages(flavors) {
29
35
  return __awaiter(this, void 0, void 0, function* () {
@@ -46,11 +52,100 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
46
52
  this.exportListOfPackages(packagesToExport);
47
53
  });
48
54
  }
55
+ batchExportPackages(packageKeys, withDependencies = false) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const exportedPackagesData = yield batch_import_export_api_1.batchImportExportApi.exportPackages(packageKeys, withDependencies);
58
+ const exportedPackagesZip = new AdmZip(exportedPackagesData);
59
+ const manifest = (0, yaml_1.parse)(exportedPackagesZip.getEntry(batch_export_import_constants_1.BatchExportImportConstants.MANIFEST_FILE_NAME).getData().toString());
60
+ const versionsByPackageKey = this.getVersionsByPackageKey(manifest);
61
+ let exportedVariables = yield this.getVersionedVariablesForPackagesWithKeys(versionsByPackageKey);
62
+ exportedVariables = studio_service_1.studioService.fixConnectionVariables(exportedVariables);
63
+ exportedPackagesZip.addFile(batch_export_import_constants_1.BatchExportImportConstants.VARIABLES_FILE_NAME, Buffer.from((0, yaml_1.stringify)(exportedVariables), "utf8"));
64
+ const studioPackageKeys = manifest.filter(packageManifest => packageManifest.flavor === batch_export_import_constants_1.BatchExportImportConstants.STUDIO)
65
+ .map(packageManifest => packageManifest.packageKey);
66
+ const studioData = yield studio_service_1.studioService.getStudioPackageManifests(studioPackageKeys);
67
+ exportedPackagesZip.addFile(batch_export_import_constants_1.BatchExportImportConstants.STUDIO_FILE_NAME, Buffer.from((0, yaml_1.stringify)(studioData), "utf8"));
68
+ exportedPackagesZip.getEntries().forEach(entry => {
69
+ if (entry.name.endsWith(batch_export_import_constants_1.BatchExportImportConstants.ZIP_EXTENSION)) {
70
+ const lastUnderscoreIndex = entry.name.lastIndexOf("_");
71
+ const packageKey = entry.name.substring(0, lastUnderscoreIndex);
72
+ if (studioPackageKeys.includes(packageKey)) {
73
+ const updatedPackage = studio_service_1.studioService.processPackageForExport(entry, exportedVariables);
74
+ exportedPackagesZip.updateFile(entry, updatedPackage.toBuffer());
75
+ }
76
+ }
77
+ });
78
+ const fileDownloadedMessage = "File downloaded successfully. New filename: ";
79
+ const filename = `export_${(0, uuid_1.v4)()}.zip`;
80
+ exportedPackagesZip.writeZip(filename);
81
+ logger_1.logger.info(fileDownloadedMessage + filename);
82
+ });
83
+ }
84
+ batchImportPackages(file, overwrite) {
85
+ return __awaiter(this, void 0, void 0, function* () {
86
+ let configs = new AdmZip(file);
87
+ const studioManifests = this.parseEntryData(configs, batch_export_import_constants_1.BatchExportImportConstants.STUDIO_FILE_NAME);
88
+ const variablesManifests = this.parseEntryData(configs, batch_export_import_constants_1.BatchExportImportConstants.VARIABLES_FILE_NAME);
89
+ configs = yield studio_service_1.studioService.mapSpaces(configs, studioManifests);
90
+ const existingStudioPackages = yield package_api_1.packageApi.findAllPackages();
91
+ const formData = this.buildBodyForImport(configs, variablesManifests);
92
+ const postPackageImportData = yield batch_import_export_api_1.batchImportExportApi.importPackages(formData, overwrite);
93
+ yield studio_service_1.studioService.processImportedPackages(configs, existingStudioPackages, studioManifests);
94
+ const reportFileName = "config_import_report_" + (0, uuid_1.v4)() + ".json";
95
+ file_service_1.fileService.writeToFileWithGivenName(JSON.stringify(postPackageImportData), reportFileName);
96
+ logger_1.logger.info("Config import report file: " + reportFileName);
97
+ });
98
+ }
49
99
  exportListOfPackages(packages) {
50
100
  const filename = (0, uuid_1.v4)() + ".json";
51
101
  file_service_1.fileService.writeToFileWithGivenName(JSON.stringify(packages), filename);
52
102
  logger_1.logger.info(file_service_1.FileService.fileDownloadedMessage + filename);
53
103
  }
104
+ getVersionsByPackageKey(manifests) {
105
+ const versionsByPackageKey = new Map();
106
+ manifests.forEach(packageManifest => {
107
+ versionsByPackageKey.set(packageManifest.packageKey, Object.keys(packageManifest.dependenciesByVersion));
108
+ });
109
+ return versionsByPackageKey;
110
+ }
111
+ getVersionedVariablesForPackagesWithKeys(versionsByPackageKey) {
112
+ const variableExportRequest = [];
113
+ versionsByPackageKey === null || versionsByPackageKey === void 0 ? void 0 : versionsByPackageKey.forEach((versions, key) => {
114
+ versions === null || versions === void 0 ? void 0 : versions.forEach(version => {
115
+ variableExportRequest.push({
116
+ packageKey: key,
117
+ version: version,
118
+ });
119
+ });
120
+ });
121
+ return batch_import_export_api_1.batchImportExportApi.findVariablesWithValuesByPackageKeysAndVersion(variableExportRequest);
122
+ }
123
+ buildBodyForImport(configs, variablesManifests) {
124
+ const formData = new FormData();
125
+ const readableStream = this.getReadableStream(configs);
126
+ formData.append("file", readableStream, { filename: "configs.zip" });
127
+ if (variablesManifests) {
128
+ formData.append("mappedVariables", JSON.stringify(variablesManifests), {
129
+ contentType: "application/json"
130
+ });
131
+ }
132
+ return formData;
133
+ }
134
+ getReadableStream(configs) {
135
+ return new stream_1.Readable({
136
+ read() {
137
+ this.push(configs.toBuffer());
138
+ this.push(null);
139
+ },
140
+ });
141
+ }
142
+ parseEntryData(configs, fileName) {
143
+ const entry = configs.getEntry(fileName);
144
+ if (entry) {
145
+ return ((0, yaml_1.parse)(entry.getData().toString()));
146
+ }
147
+ return null;
148
+ }
54
149
  }
55
150
  exports.batchImportExportService = new BatchImportExportService();
56
151
  });
@@ -13,7 +13,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  if (v !== undefined) module.exports = v;
14
14
  }
15
15
  else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "../../util/logger", "../../api/package-api", "uuid", "../file-service", "./datamodel-service", "../../interfaces/package-manager.interfaces", "../../api/node-api", "../../api/package-dependencies-api", "./variable-service", "./space-service", "fs", "adm-zip", "path", "os", "../../util/semantic-versioning", "../../util/yaml"], factory);
16
+ define(["require", "exports", "../../util/logger", "../../api/package-api", "uuid", "../file-service", "./datamodel-service", "../../interfaces/package-manager.interfaces", "../../api/node-api", "../../api/package-dependencies-api", "./variable-service", "./space-service", "fs", "adm-zip", "path", "os", "../../util/semantic-versioning", "../../util/yaml", "form-data"], factory);
17
17
  }
18
18
  })(function (require, exports) {
19
19
  "use strict";
@@ -35,6 +35,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
35
35
  const os_1 = require("os");
36
36
  const semantic_versioning_1 = require("../../util/semantic-versioning");
37
37
  const yaml_1 = require("../../util/yaml");
38
+ const FormData = require("form-data");
38
39
  class PackageService {
39
40
  constructor() {
40
41
  this.fileDownloadedMessage = "File downloaded successfully. New filename: ";
@@ -165,7 +166,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
165
166
  const targetSpace = yield this.getTargetSpaceForExportedPackage(packageToImport, spaceMappings);
166
167
  let nodeInTargetTeam = yield node_api_1.nodeApi.findOneByKeyAndRootNodeKey(packageToImport.packageKey, packageToImport.packageKey);
167
168
  const pathToZipFile = path.resolve(importedFilePath, packageToImport.packageKey + "_" + versionOfPackageBeingImported + ".zip");
168
- const packageZip = yield this.createBodyForImport(pathToZipFile);
169
+ const packageZip = this.createBodyForImport(pathToZipFile);
169
170
  yield package_api_1.packageApi.importPackage(packageZip, targetSpace.id, !!nodeInTargetTeam, excludeActionFlows);
170
171
  if (nodeInTargetTeam) {
171
172
  yield package_api_1.packageApi.movePackageToSpace(nodeInTargetTeam.id, targetSpace.id);
@@ -228,13 +229,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
228
229
  return importedPackages.includes(version);
229
230
  }
230
231
  createBodyForImport(filename) {
231
- return __awaiter(this, void 0, void 0, function* () {
232
- return {
233
- formData: {
234
- package: yield fs.createReadStream(filename, { encoding: null })
235
- },
236
- };
237
- });
232
+ const formData = new FormData();
233
+ formData.append("package", fs.createReadStream(filename, { encoding: null }));
234
+ return formData;
238
235
  }
239
236
  getTargetSpaceForExportedPackage(packageToImport, spaceMappings) {
240
237
  return __awaiter(this, void 0, void 0, function* () {
@@ -13,7 +13,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  if (v !== undefined) module.exports = v;
14
14
  }
15
15
  else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "../../api/package-api", "../../api/variables-api", "uuid", "../../util/logger", "../file-service", "url"], factory);
16
+ define(["require", "exports", "../../api/package-api", "../../api/variables-api", "uuid", "../../util/logger", "../file-service", "url", "../studio/studio.service", "../../api/batch-import-export-api"], factory);
17
17
  }
18
18
  })(function (require, exports) {
19
19
  "use strict";
@@ -25,6 +25,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
25
25
  const logger_1 = require("../../util/logger");
26
26
  const file_service_1 = require("../file-service");
27
27
  const url_1 = require("url");
28
+ const studio_service_1 = require("../studio/studio.service");
29
+ const batch_import_export_api_1 = require("../../api/batch-import-export-api");
28
30
  class VariableService {
29
31
  getVariableAssignmentsForNodes(type) {
30
32
  return __awaiter(this, void 0, void 0, function* () {
@@ -49,9 +51,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
49
51
  return __awaiter(this, void 0, void 0, function* () {
50
52
  const parsedParams = this.parseParams(params);
51
53
  const assignments = yield variables_api_1.variablesApi.getCandidateAssignments(type, parsedParams);
52
- const filename = (0, uuid_1.v4)() + ".json";
53
- file_service_1.fileService.writeToFileWithGivenName(JSON.stringify(assignments), filename);
54
- logger_1.logger.info(file_service_1.FileService.fileDownloadedMessage + filename);
54
+ this.exportToJson(assignments);
55
+ });
56
+ }
57
+ listVariables(keysByVersion, keysByVersionFile) {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ const variableManifests = yield this.getVersionedVariablesByKeyVersionPairs(keysByVersion, keysByVersionFile);
60
+ variableManifests.forEach(variableManifest => {
61
+ logger_1.logger.info(JSON.stringify(variableManifest));
62
+ });
63
+ });
64
+ }
65
+ exportVariables(keysByVersion, keysByVersionFile) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const variableManifests = yield this.getVersionedVariablesByKeyVersionPairs(keysByVersion, keysByVersionFile);
68
+ this.exportToJson(variableManifests);
55
69
  });
56
70
  }
57
71
  parseParams(params) {
@@ -69,6 +83,42 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
69
83
  }
70
84
  return queryParams;
71
85
  }
86
+ getVersionedVariablesByKeyVersionPairs(keysByVersion, keysByVersionFile) {
87
+ return __awaiter(this, void 0, void 0, function* () {
88
+ const variablesExportRequest = yield this.buildKeyVersionPairs(keysByVersion, keysByVersionFile);
89
+ const variableManifests = yield batch_import_export_api_1.batchImportExportApi.findVariablesWithValuesByPackageKeysAndVersion(variablesExportRequest);
90
+ return studio_service_1.studioService.fixConnectionVariables(variableManifests);
91
+ });
92
+ }
93
+ buildKeyVersionPairs(keysByVersion, keysByVersionFile) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ let variablesExportRequest = [];
96
+ if (keysByVersion.length !== 0) {
97
+ variablesExportRequest = this.buildKeyAndVersionPairsFromArrayInput(keysByVersion);
98
+ }
99
+ else if (keysByVersion.length === 0 && keysByVersionFile !== "") {
100
+ variablesExportRequest = yield file_service_1.fileService.readFileToJson(keysByVersionFile);
101
+ }
102
+ else {
103
+ throw new logger_1.FatalError("Please provide keysByVersion mappings or file path!");
104
+ }
105
+ return variablesExportRequest;
106
+ });
107
+ }
108
+ buildKeyAndVersionPairsFromArrayInput(keysByVersion) {
109
+ return keysByVersion.map(keyAndVersion => {
110
+ const keyAndVersionSplit = keyAndVersion.split(":");
111
+ return {
112
+ packageKey: keyAndVersionSplit[0],
113
+ version: keyAndVersionSplit[1]
114
+ };
115
+ });
116
+ }
117
+ exportToJson(data) {
118
+ const filename = (0, uuid_1.v4)() + ".json";
119
+ file_service_1.fileService.writeToFileWithGivenName(JSON.stringify(data), filename);
120
+ logger_1.logger.info(file_service_1.FileService.fileDownloadedMessage + filename);
121
+ }
72
122
  }
73
123
  exports.variableService = new VariableService();
74
124
  });
@@ -13,7 +13,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
13
13
  if (v !== undefined) module.exports = v;
14
14
  }
15
15
  else if (typeof define === "function" && define.amd) {
16
- define(["require", "exports", "../../api/package-api", "../../interfaces/package-manager.interfaces", "../package-manager/datamodel-service"], factory);
16
+ define(["require", "exports", "../../api/package-api", "../../interfaces/package-manager.interfaces", "../package-manager/datamodel-service", "../../util/yaml", "../../api/node-api", "../../api/variables-api", "../../api/space-api", "../package-manager/space-service", "../package-manager/variable-service", "../../interfaces/batch-export-import-constants", "adm-zip"], factory);
17
17
  }
18
18
  })(function (require, exports) {
19
19
  "use strict";
@@ -22,6 +22,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
22
22
  const package_api_1 = require("../../api/package-api");
23
23
  const package_manager_interfaces_1 = require("../../interfaces/package-manager.interfaces");
24
24
  const datamodel_service_1 = require("../package-manager/datamodel-service");
25
+ const yaml_1 = require("../../util/yaml");
26
+ const node_api_1 = require("../../api/node-api");
27
+ const variables_api_1 = require("../../api/variables-api");
28
+ const space_api_1 = require("../../api/space-api");
29
+ const space_service_1 = require("../package-manager/space-service");
30
+ const variable_service_1 = require("../package-manager/variable-service");
31
+ const batch_export_import_constants_1 = require("../../interfaces/batch-export-import-constants");
32
+ const AdmZip = require("adm-zip");
25
33
  class StudioService {
26
34
  getExportPackagesWithStudioData(packagesToExport, withDependencies) {
27
35
  return __awaiter(this, void 0, void 0, function* () {
@@ -34,6 +42,51 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
34
42
  return packagesToExport;
35
43
  });
36
44
  }
45
+ fixConnectionVariables(variables) {
46
+ return variables.map(variableManifest => (Object.assign(Object.assign({}, variableManifest), { variables: variableManifest.variables.map(variable => {
47
+ if (variable.type !== package_manager_interfaces_1.PackageManagerVariableType.CONNECTION) {
48
+ return variable;
49
+ }
50
+ return this.fixConnectionVariable(variable);
51
+ }) })));
52
+ }
53
+ getStudioPackageManifests(studioPackageKeys) {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ return Promise.all(studioPackageKeys.map((packageKey) => __awaiter(this, void 0, void 0, function* () {
56
+ const node = yield node_api_1.nodeApi.findOneByKeyAndRootNodeKey(packageKey, packageKey);
57
+ const nodeSpace = yield space_api_1.spaceApi.findOne(node.spaceId);
58
+ const variableAssignments = yield variables_api_1.variablesApi.getRuntimeVariableValues(packageKey, batch_export_import_constants_1.BatchExportImportConstants.APP_MODE_VIEWER);
59
+ return {
60
+ packageKey: packageKey,
61
+ space: {
62
+ name: nodeSpace.name,
63
+ iconReference: nodeSpace.iconReference
64
+ },
65
+ runtimeVariableAssignments: variableAssignments
66
+ };
67
+ })));
68
+ });
69
+ }
70
+ processPackageForExport(exportedPackage, exportedVariables) {
71
+ const packageZip = new AdmZip(exportedPackage.getData());
72
+ this.deleteScenarioAssets(packageZip);
73
+ this.fixConnectionVariablesForRootNodeFiles(packageZip, exportedPackage.name, exportedVariables);
74
+ return packageZip;
75
+ }
76
+ processImportedPackages(configs, existingStudioPackages, studioManifests) {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ if (studioManifests == null) {
79
+ return;
80
+ }
81
+ for (const manifest of studioManifests) {
82
+ const existingPackage = existingStudioPackages.find(existingPackage => existingPackage.key === manifest.packageKey);
83
+ if (existingPackage) {
84
+ yield package_api_1.packageApi.movePackageToSpace(existingPackage.id, manifest.space.id);
85
+ }
86
+ yield this.assignRuntimeVariables(manifest);
87
+ }
88
+ });
89
+ }
37
90
  setSpaceIdForStudioPackages(packages, studioPackages) {
38
91
  const studioPackageByKey = new Map();
39
92
  studioPackages.forEach(pkg => studioPackageByKey.set(pkg.key, pkg));
@@ -53,6 +106,115 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
53
106
  })) }) : pkg;
54
107
  });
55
108
  }
109
+ fixConnectionVariable(variable) {
110
+ if (!variable.value.appName) {
111
+ return variable;
112
+ }
113
+ return Object.assign(Object.assign({}, variable), { metadata: Object.assign(Object.assign({}, variable.metadata), { appName: variable.value.appName }) });
114
+ }
115
+ deleteScenarioAssets(packageZip) {
116
+ packageZip.getEntries().filter(entry => entry.entryName.startsWith(batch_export_import_constants_1.BatchExportImportConstants.NODES_FOLDER_NAME) && entry.entryName.endsWith(batch_export_import_constants_1.BatchExportImportConstants.YAML_EXTENSION))
117
+ .forEach(entry => {
118
+ const node = (0, yaml_1.parse)(entry.getData().toString());
119
+ if (node.type === batch_export_import_constants_1.BatchExportImportConstants.SCENARIO_NODE) {
120
+ packageZip.deleteFile(entry);
121
+ }
122
+ });
123
+ }
124
+ fixConnectionVariablesForRootNodeFiles(packageZip, zipName, exportedVariables) {
125
+ const packageKeyAndVersion = this.getPackageKeyAndVersion(zipName);
126
+ const connectionVariablesByKey = this.getConnectionVariablesByKeyForPackage(packageKeyAndVersion.packageKey, packageKeyAndVersion.version, exportedVariables);
127
+ if (connectionVariablesByKey.size === 0) {
128
+ return;
129
+ }
130
+ const packageEntry = packageZip.getEntry("package.yml");
131
+ const exportedNode = (0, yaml_1.parse)(packageEntry.getData().toString());
132
+ const nodeContent = (0, yaml_1.parse)(exportedNode.serializedContent);
133
+ nodeContent.variables = nodeContent.variables.map(variable => (Object.assign(Object.assign({}, variable), { metadata: variable.type === package_manager_interfaces_1.PackageManagerVariableType.CONNECTION ?
134
+ connectionVariablesByKey.get(variable.key).metadata : variable.metadata })));
135
+ exportedNode.serializedContent = (0, yaml_1.stringify)(nodeContent);
136
+ packageZip.updateFile(packageEntry, Buffer.from((0, yaml_1.stringify)(exportedNode)));
137
+ }
138
+ getPackageKeyAndVersion(zipName) {
139
+ const lastUnderscoreIndex = zipName.lastIndexOf("_");
140
+ const packageKey = zipName.replace(batch_export_import_constants_1.BatchExportImportConstants.ZIP_EXTENSION, "").substring(0, lastUnderscoreIndex);
141
+ const packageVersion = zipName.replace(batch_export_import_constants_1.BatchExportImportConstants.ZIP_EXTENSION, "").substring(lastUnderscoreIndex + 1);
142
+ return {
143
+ packageKey: packageKey,
144
+ version: packageVersion
145
+ };
146
+ }
147
+ getConnectionVariablesByKeyForPackage(packageKey, packageVersion, variables) {
148
+ const variablesByKey = new Map();
149
+ const packageVariables = variables.find(exportedVariable => exportedVariable.packageKey === packageKey && exportedVariable.version === packageVersion);
150
+ if (packageVariables && packageVariables.variables.length) {
151
+ packageVariables.variables.filter(variable => variable.type === package_manager_interfaces_1.PackageManagerVariableType.CONNECTION)
152
+ .forEach(variable => variablesByKey.set(variable.key, variable));
153
+ }
154
+ return variablesByKey;
155
+ }
156
+ mapSpaces(exportedFiles, studioManifests) {
157
+ return __awaiter(this, void 0, void 0, function* () {
158
+ if (studioManifests == null) {
159
+ return exportedFiles;
160
+ }
161
+ for (const file of exportedFiles.getEntries()) {
162
+ if (file.name.endsWith(batch_export_import_constants_1.BatchExportImportConstants.ZIP_EXTENSION)) {
163
+ const packageKey = this.getPackageKeyAndVersion(file.name).packageKey;
164
+ if (this.isStudioPackage(studioManifests, packageKey)) {
165
+ const studioManifest = studioManifests.find(manifest => manifest.packageKey === packageKey);
166
+ if (studioManifest) {
167
+ const spaceId = yield this.findDesiredSpaceIdForPackage(studioManifest);
168
+ studioManifest.space.id = spaceId;
169
+ const packageZip = new AdmZip(file.getData());
170
+ packageZip.getEntries().forEach(nodeFile => {
171
+ if (nodeFile.entryName.endsWith(batch_export_import_constants_1.BatchExportImportConstants.YAML_EXTENSION)) {
172
+ const updatedNodeFile = this.updateSpaceIdForNode(nodeFile.getData().toString(), spaceId);
173
+ packageZip.updateFile(nodeFile, Buffer.from(updatedNodeFile));
174
+ }
175
+ });
176
+ exportedFiles.updateFile(file, packageZip.toBuffer());
177
+ }
178
+ }
179
+ }
180
+ }
181
+ return exportedFiles;
182
+ });
183
+ }
184
+ isStudioPackage(studioManifests, packageKey) {
185
+ return studioManifests.some(manifest => manifest.packageKey === packageKey);
186
+ }
187
+ findDesiredSpaceIdForPackage(studioPackageManifest) {
188
+ return __awaiter(this, void 0, void 0, function* () {
189
+ const allSpaces = yield space_service_1.spaceService.refreshAndGetAllSpaces();
190
+ if (studioPackageManifest.space.id) {
191
+ const targetSpace = allSpaces.find(space => space.id === studioPackageManifest.space.id);
192
+ if (!targetSpace) {
193
+ throw Error("Provided space ID does not exist.");
194
+ }
195
+ return targetSpace.id;
196
+ }
197
+ const targetSpaceByName = allSpaces.find(space => space.name === studioPackageManifest.space.name);
198
+ if (targetSpaceByName) {
199
+ return targetSpaceByName.id;
200
+ }
201
+ const spaceTransport = yield space_service_1.spaceService.createSpace(studioPackageManifest.space.name, studioPackageManifest.space.iconReference);
202
+ return spaceTransport.id;
203
+ });
204
+ }
205
+ assignRuntimeVariables(manifest) {
206
+ return __awaiter(this, void 0, void 0, function* () {
207
+ if (manifest.runtimeVariableAssignments.length) {
208
+ yield variable_service_1.variableService.assignVariableValues(manifest.packageKey, manifest.runtimeVariableAssignments);
209
+ }
210
+ });
211
+ }
212
+ updateSpaceIdForNode(nodeContent, spaceId) {
213
+ const exportedNode = (0, yaml_1.parse)(nodeContent);
214
+ const oldSpaceId = exportedNode.unversionedMetadata.spaceId;
215
+ nodeContent = nodeContent.replace(new RegExp(oldSpaceId, "g"), spaceId);
216
+ return nodeContent;
217
+ }
56
218
  }
57
219
  exports.studioService = new StudioService();
58
220
  });