Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@celonis/content-cli",
"version": "1.1.1",
"version": "1.1.2",
"description": "CLI Tool to help manage content in Celonis Platform",
"main": "content-cli.js",
"bin": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class BatchImportExportService {
this.exportListOfPackages(packagesToExport);
}

public async batchExportPackages(packageKeys: string[], packageKeysByVersion: string[], withDependencies: boolean, gitBranch: string): Promise<void> {
public async batchExportPackages(packageKeys: string[], packageKeysByVersion: string[], withDependencies: boolean, gitBranch: string, unzip: boolean): Promise<void> {
let exportedPackagesData: Buffer;
if (packageKeys) {
exportedPackagesData = await this.batchImportExportApi.exportPackages(packageKeys, withDependencies);
Expand Down Expand Up @@ -97,11 +97,9 @@ export class BatchImportExportService {
const extractedDirectory = fileService.extractExportedZipWithNestedZips(exportedPackagesZip);
await this.gitService.pushToBranch(extractedDirectory, gitBranch);
logger.info("Successfully exported packages to branch: " + gitBranch);
fs.rmSync(extractedDirectory, { recursive: true });
} else {
const fileDownloadedMessage = "File downloaded successfully. New filename: ";
const filename = `export_${uuidv4()}.zip`;
exportedPackagesZip.writeZip(filename);
logger.info(fileDownloadedMessage + filename);
this.downloadZip(exportedPackagesZip, unzip);
}
}

Expand All @@ -117,16 +115,18 @@ export class BatchImportExportService {
}
}

public async batchImportPackages(file: string, overwrite: boolean, gitBranch: string): Promise<void> {
let fileToBeImported: string;
public async batchImportPackages(sourcePath: string, overwrite: boolean, gitBranch: string): Promise<void> {
let sourceToBeImported: string;
if (gitBranch) {
fileToBeImported = await this.gitService.pullFromBranch(gitBranch);
fileToBeImported = fileService.zipDirectoryInBatchExportFormat(fileToBeImported);
sourceToBeImported = await this.gitService.pullFromBranch(gitBranch);
} else {
fileToBeImported = file;
sourceToBeImported = sourcePath;
if (fileService.isDirectory(sourcePath)) {
sourceToBeImported = fileService.zipDirectoryInBatchExportFormat(sourceToBeImported);
}
}

let configs = new AdmZip(fileToBeImported);
let configs = new AdmZip(sourceToBeImported);
const studioManifests = this.parseEntryData(configs, BatchExportImportConstants.STUDIO_FILE_NAME) as StudioPackageManifest[];
const variablesManifests: VariableManifestTransport[] = this.parseEntryData(configs, BatchExportImportConstants.VARIABLES_FILE_NAME) as VariableManifestTransport[];

Expand All @@ -138,7 +138,7 @@ export class BatchImportExportService {
await this.studioService.processImportedPackages(configs, existingStudioPackages, studioManifests);

if (gitBranch) {
fs.rmSync(fileToBeImported);
fs.rmSync(sourceToBeImported);
}

const reportFileName = "config_import_report_" + uuidv4() + ".json";
Expand Down Expand Up @@ -227,4 +227,18 @@ export class BatchImportExportService {
}
return null;
}

private downloadZip(exportedZip: AdmZip, unzip: boolean): void {
if (unzip) {
const fileDownloadedMessage = "Successful download. Downloaded directory: ";
const targetDirectoryName = `export_${uuidv4()}`;
fileService.extractExportedZipWithNestedZipsToDir(exportedZip, targetDirectoryName);
logger.info(fileDownloadedMessage + targetDirectoryName);
} else {
const fileDownloadedMessage = "File downloaded successfully. New filename: ";
const filename = `export_${uuidv4()}.zip`;
exportedZip.writeZip(filename);
logger.info(fileDownloadedMessage + filename);
}
}
}
27 changes: 19 additions & 8 deletions src/commands/configuration-management/config-command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Context } from "../../core/command/cli-context";
import { BatchImportExportService } from "./batch-import-export.service";
import { VariableService } from "./variable.service";
import { DiffService } from "./diff.service";
import { fileService } from "../../core/utils/file-service";

export class ConfigCommandService {

Expand Down Expand Up @@ -36,22 +37,32 @@ export class ConfigCommandService {
}
}

public batchExportPackages(packageKeys: string[], packageKeysByVersion: string[], withDependencies: boolean, gitBranch: string): Promise<void> {
return this.batchImportExportService.batchExportPackages(packageKeys, packageKeysByVersion, withDependencies, gitBranch);
public batchExportPackages(packageKeys: string[], packageKeysByVersion: string[], withDependencies: boolean, gitBranch: string, unzip: boolean): Promise<void> {
return this.batchImportExportService.batchExportPackages(packageKeys, packageKeysByVersion, withDependencies, gitBranch, unzip);
}

public batchExportPackagesMetadata(packageKeys: string[], jsonResponse: boolean): Promise<void> {
return this.batchImportExportService.batchExportPackagesMetadata(packageKeys, jsonResponse);
}

public batchImportPackages(file: string, overwrite: boolean, gitBranch: string): Promise<void> {
if (file && gitBranch) {
throw new Error("You cannot use both file and gitBranch options at the same time. Only one import source can be defined.");
public batchImportPackages(file: string, directory: string, overwrite: boolean, gitBranch: string): Promise<void> {
if ((directory || file) && gitBranch) {
throw new Error("You cannot use both file/directory and gitBranch options at the same time. Only one import source can be defined.");
}
if (!file && !gitBranch) {
throw new Error("You must provide either a file or a gitBranch option to import packages.");
if (!directory && !file && !gitBranch) {
throw new Error("You must provide either a file/directory or a gitBranch option to import packages.");
}
return this.batchImportExportService.batchImportPackages(file, overwrite, gitBranch);
if (file && directory) {
throw new Error("You cannot use both file and directory options at the same time. Only one import source can be defined.");
}
if (file && fileService.isDirectory(file)) {
throw new Error("The file option accepts only zip files.");
}
if (directory && !fileService.isDirectory(directory)) {
throw new Error("The directory option accepts only directories.");
}
const sourcePath = file ?? directory;
return this.batchImportExportService.batchImportPackages(sourcePath, overwrite, gitBranch);
}

public diffPackages(file: string, hasChanges: boolean, jsonResponse: boolean): Promise<void> {
Expand Down
6 changes: 4 additions & 2 deletions src/commands/configuration-management/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Module extends IModule {
.option("--packageKeys <packageKeys...>", "Keys of packages to export. Exports the latest deployed version only")
.option("--keysByVersion <keysByVersion...>", "Keys of packages to export by version")
.option("--withDependencies", "Include variables and dependencies", "")
.option("--unzip", "Unzip the exported file", "")
.betaOption("--gitProfile <gitProfile>", "Git profile which you want to use for the Git operations")
.betaOption("--gitBranch <gitBranch>", "Git branch in which you want to push the exported file")
.action(this.batchExportPackages);
Expand All @@ -47,6 +48,7 @@ class Module extends IModule {
.betaOption("--gitProfile <gitProfile>", "Git profile which you want to use for the Git operations")
.betaOption("--gitBranch <gitBranch>", "Git branch from which you want to pull the exported file and import")
.option("-f, --file <file>", "Exported packages file (relative path)")
.option("-d, --directory <directory>", "Exported packages directory (relative path)")
.action(this.batchImportPackages);

configCommand.command("diff")
Expand Down Expand Up @@ -84,15 +86,15 @@ class Module extends IModule {
throw new Error("Please provide either --packageKeys or --keysByVersion, but not both.");
}
options.withDependencies = options.withDependencies ?? false;
await new ConfigCommandService(context).batchExportPackages(options.packageKeys, options.keysByVersion, options.withDependencies, options.gitBranch);
await new ConfigCommandService(context).batchExportPackages(options.packageKeys, options.keysByVersion, options.withDependencies, options.gitBranch, options.unzip);
}

private async batchExportPackagesMetadata(context: Context, command: Command, options: OptionValues): Promise<void> {
await new ConfigCommandService(context).batchExportPackagesMetadata(options.packageKeys, options.json);
}

private async batchImportPackages(context: Context, command: Command, options: OptionValues): Promise<void> {
await new ConfigCommandService(context).batchImportPackages(options.file, options.overwrite, options.gitBranch);
await new ConfigCommandService(context).batchImportPackages(options.file, options.directory, options.overwrite, options.gitBranch);
}

private async diffPackages(context: Context, command: Command, options: OptionValues): Promise<void> {
Expand Down
25 changes: 17 additions & 8 deletions src/core/utils/file-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ export class FileService {
});
}

public createDirectoryWithGivenName(dirName: string): void {
fs.mkdirSync(path.resolve(process.cwd(), dirName));
}

public async readFileToJson<T>(fileName: string): Promise<T> {
const fileContent = this.readFile(fileName);

Expand All @@ -35,11 +31,15 @@ export class FileService {
const tempDir = path.join(os.tmpdir(), `${uuidv4()}`);
fs.mkdirSync(tempDir, { recursive: true });

zipFile.extractAllTo(tempDir, true);
return this.extractExportedZipWithNestedZipsToDir(zipFile, tempDir);
}

public extractExportedZipWithNestedZipsToDir(zipFile: AdmZip, targetDir: string): string {
zipFile.extractAllTo(targetDir, true);

const files = fs.readdirSync(tempDir);
const files = fs.readdirSync(targetDir);
for (const file of files) {
const innerZipPath = path.join(tempDir, file);
const innerZipPath = path.join(targetDir, file);
if (file.endsWith(".zip")) {
const nestedZip = new AdmZip(innerZipPath);
const nestedDir = innerZipPath.replace(/\.zip$/, "");
Expand All @@ -49,7 +49,16 @@ export class FileService {
fs.rmSync(innerZipPath); // Optionally remove the inner zip
}
}
return tempDir;
return targetDir;
}

public copyDirectoryToCurrentLocation(sourceDir: string, targetName: string): void {
fs.mkdirSync(targetName, { recursive: true });
fs.cpSync(sourceDir, targetName, { recursive: true });
}

public isDirectory(sourcePath: string): boolean {
return fs.statSync(sourcePath)?.isDirectory()
}

public zipDirectoryInBatchExportFormat(sourceDir: string): string {
Expand Down
Loading