@hashgraph/solo
Version:
An opinionated CLI tool to deploy and manage private Hedera Networks.
151 lines • 6.17 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
import { SoloError } from './errors/solo-error.js';
import { IllegalArgumentError } from './errors/illegal-argument-error.js';
import { MissingArgumentError } from './errors/missing-argument-error.js';
import fs from 'node:fs';
import AdmZip from 'adm-zip';
import * as tar from 'tar';
import chalk from 'chalk';
import path from 'node:path';
import { inject, injectable } from 'tsyringe-neo';
import { patchInject } from './dependency-injection/container-helper.js';
import { InjectTokens } from './dependency-injection/inject-tokens.js';
let Zippy = class Zippy {
logger;
constructor(logger) {
this.logger = logger;
this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name);
}
/**
* Zip a file or directory
* @param srcPath - path to a file or directory
* @param destPath - path to the output zip file
* @param [verbose] - if true, log the progress
* @returns path to the output zip file
*/
async zip(sourcePath, destinationPath, _verbose = false) {
if (!sourcePath) {
throw new MissingArgumentError('srcPath is required');
}
if (!destinationPath) {
throw new MissingArgumentError('destPath is required');
}
if (!destinationPath.endsWith('.zip')) {
throw new MissingArgumentError('destPath must be a path to a zip file');
}
try {
const zip = new AdmZip('', {});
const stat = fs.statSync(sourcePath);
if (stat.isDirectory()) {
zip.addLocalFolder(sourcePath, '');
}
else {
zip.addFile(path.basename(sourcePath), fs.readFileSync(sourcePath), '', stat);
}
await zip.writeZipPromise(destinationPath, { overwrite: true });
return destinationPath;
}
catch (error) {
throw new SoloError(`failed to unzip ${sourcePath}: ${error.message}`, error);
}
}
unzip(sourcePath, destinationPath, verbose = false) {
if (!sourcePath) {
throw new MissingArgumentError('srcPath is required');
}
if (!destinationPath) {
throw new MissingArgumentError('destPath is required');
}
if (!fs.existsSync(sourcePath)) {
throw new IllegalArgumentError('srcPath does not exists', sourcePath);
}
try {
const zip = new AdmZip(sourcePath, { readEntries: true });
for (const zipEntry of zip.getEntries()) {
if (verbose) {
this.logger.debug(`Extracting file: ${zipEntry.entryName} -> ${destinationPath}/${zipEntry.entryName} ...`, {
src: zipEntry.entryName,
dst: `${destinationPath}/${zipEntry.entryName}`,
});
}
zip.extractEntryTo(zipEntry, destinationPath, true, true, true, zipEntry.entryName);
if (verbose) {
this.logger.showUser(chalk.green('OK'), `Extracted: ${zipEntry.entryName} -> ${destinationPath}/${zipEntry.entryName}`);
}
}
return destinationPath;
}
catch (error) {
throw new SoloError(`failed to unzip ${sourcePath}: ${error.message}`, error);
}
}
tar(sourcePath, destinationPath) {
if (!sourcePath) {
throw new MissingArgumentError('srcPath is required');
}
if (!destinationPath) {
throw new MissingArgumentError('destPath is required');
}
if (!destinationPath.endsWith('.tar.gz')) {
throw new MissingArgumentError('destPath must be a path to a tar.gz file');
}
if (!fs.existsSync(sourcePath)) {
throw new IllegalArgumentError('srcPath does not exists', sourcePath);
}
try {
tar.c({
gzip: true,
file: destinationPath,
sync: true,
}, [sourcePath]);
return destinationPath;
}
catch (error) {
throw new SoloError(`failed to tar ${sourcePath}: ${error.message}`, error);
}
}
untar(sourcePath, destinationPath) {
if (!sourcePath) {
throw new MissingArgumentError('srcPath is required');
}
if (!destinationPath) {
throw new MissingArgumentError('destPath is required');
}
if (!fs.existsSync(sourcePath)) {
throw new IllegalArgumentError('srcPath does not exists', sourcePath);
}
if (!fs.existsSync(destinationPath)) {
fs.mkdirSync(destinationPath);
}
try {
tar.x({
C: destinationPath,
file: sourcePath,
sync: true,
});
return destinationPath;
}
catch (error) {
throw new SoloError(`failed to untar ${sourcePath}: ${error.message}`, error);
}
}
};
Zippy = __decorate([
injectable(),
__param(0, inject(InjectTokens.SoloLogger)),
__metadata("design:paramtypes", [Object])
], Zippy);
export { Zippy };
//# sourceMappingURL=zippy.js.map