@kentico/kontent-backup-manager
Advanced tools
Comparing version 1.4.0 to 1.5.0-next.0
{ | ||
"name": "@kentico/kontent-backup-manager", | ||
"version": "1.4.0", | ||
"version": "1.5.0-next.0", | ||
"description": "This utility enables backup & restore of Kentico Kontent projects", | ||
"preferGlobal": true, | ||
"bin": { | ||
"kbm": "./_commonjs/src/cli/app.js" | ||
"kbm": "./_commonjs/src/node-js/cli/app.js" | ||
}, | ||
@@ -45,3 +45,3 @@ "repository": { | ||
"@kentico/kontent-management": "0.3.19", | ||
"jszip": "3.3.0", | ||
"jszip": "3.4.0", | ||
"rxjs": "6.5.5", | ||
@@ -52,10 +52,10 @@ "yargs": "15.3.1", | ||
"devDependencies": { | ||
"@types/jszip": "3.1.7", | ||
"@types/node": "13.11.0", | ||
"@types/yargs": "15.0.4", | ||
"standard-version": "7.1.0", | ||
"ts-node": "8.8.2", | ||
"tslint": "6.1.1", | ||
"typescript": "3.8.3" | ||
"@types/jszip": "3.4.1", | ||
"@types/node": "14.0.1", | ||
"@types/yargs": "15.0.5", | ||
"standard-version": "8.0.0", | ||
"ts-node": "8.10.1", | ||
"tslint": "6.1.2", | ||
"typescript": "3.9.2" | ||
} | ||
} |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CleanService = void 0; | ||
var kontent_management_1 = require("@kentico/kontent-management"); | ||
@@ -41,0 +42,0 @@ var CleanService = /** @class */ (function () { |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./clean.service")); | ||
__exportStar(require("./clean.models"), exports); | ||
__exportStar(require("./clean.service"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fileHelper = exports.FileHelper = void 0; | ||
var fs = require("fs"); | ||
@@ -41,0 +42,0 @@ var FileHelper = /** @class */ (function () { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getFilenameWithoutExtension = void 0; | ||
function getFilenameWithoutExtension(filename) { | ||
@@ -4,0 +5,0 @@ if (!filename) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.idTranslateHelper = exports.IdTranslateHelper = void 0; | ||
var IdTranslateHelper = /** @class */ (function () { | ||
@@ -4,0 +5,0 @@ function IdTranslateHelper() { |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./translation-helper")); | ||
__export(require("./id-translate-helper")); | ||
__export(require("./file-helper")); | ||
__export(require("./global-helper")); | ||
__exportStar(require("./core.models"), exports); | ||
__exportStar(require("./translation-helper"), exports); | ||
__exportStar(require("./id-translate-helper"), exports); | ||
__exportStar(require("./file-helper"), exports); | ||
__exportStar(require("./global-helper"), exports); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.translationHelper = exports.TranslationHelper = void 0; | ||
var TranslationHelper = /** @class */ (function () { | ||
@@ -4,0 +5,0 @@ function TranslationHelper() { |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ExportService = void 0; | ||
var kontent_management_1 = require("@kentico/kontent-management"); | ||
@@ -41,0 +42,0 @@ var package_json_1 = require("../../package.json"); |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./export.service")); | ||
__exportStar(require("./export.models"), exports); | ||
__exportStar(require("./export.service"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ImportService = void 0; | ||
var kontent_core_1 = require("@kentico/kontent-core"); | ||
@@ -285,3 +286,3 @@ var kontent_management_1 = require("@kentico/kontent-management"); | ||
case 2: | ||
if (!importLanguage.is_default) return [3 /*break*/, 4]; | ||
if (!importLanguage.is_default) return [3 /*break*/, 5]; | ||
defaultExistingLanguage = currentLanguages.find(function (m) { return m.id === importLanguage.id; }); | ||
@@ -291,3 +292,3 @@ if (!defaultExistingLanguage) { | ||
} | ||
if (!(importLanguage.codename !== defaultExistingLanguage.codename)) return [3 /*break*/, 4]; | ||
if (!(importLanguage.codename !== defaultExistingLanguage.codename)) return [3 /*break*/, 5]; | ||
// languages do not match, change it | ||
@@ -311,4 +312,7 @@ console.log("Default language '" + importLanguage.name + "' with codename '" + importLanguage.codename + "' does not match default language in target project. Changing language codename in target project from '" + defaultExistingLanguage.codename + "' codename to '" + importLanguage.codename + "'"); | ||
_a.sent(); | ||
_a.label = 4; | ||
case 4: return [2 /*return*/]; | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
console.log("Language with codename '" + importLanguage.codename + "' already exists in target project, skipping update operation"); | ||
_a.label = 5; | ||
case 5: return [2 /*return*/]; | ||
} | ||
@@ -315,0 +319,0 @@ }); |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./import.service")); | ||
__exportStar(require("./import.models"), exports); | ||
__exportStar(require("./import.service"), exports); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// Public API | ||
__export(require("./core")); | ||
__export(require("./export")); | ||
__export(require("./import")); | ||
__export(require("./clean")); | ||
__export(require("./zip")); | ||
__exportStar(require("./core"), exports); | ||
__exportStar(require("./export"), exports); | ||
__exportStar(require("./import"), exports); | ||
__exportStar(require("./clean"), exports); | ||
__exportStar(require("./zip"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -41,7 +41,15 @@ "use strict"; | ||
var export_1 = require("../export"); | ||
var node_js_1 = require("../node-js"); | ||
var run = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var exportService, data, zipService; | ||
var fileService, zipService, exportService, data, zipFile; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
fileService = new node_js_1.FileService({ | ||
enableLog: true | ||
}); | ||
zipService = new zip_1.ZipService({ | ||
enableLog: true, | ||
context: 'node.js' | ||
}); | ||
exportService = new export_1.ExportService({ | ||
@@ -58,9 +66,13 @@ apiKey: 'sourceProjectApiKey', | ||
data = _a.sent(); | ||
zipService = new zip_1.ZipService({ | ||
filename: 'file', | ||
enableLog: true | ||
}); | ||
return [4 /*yield*/, zipService.createZipAsync(data)]; | ||
case 2: | ||
zipFile = _a.sent(); | ||
// save zip to file system (node.js only) | ||
return [4 /*yield*/, fileService.writeFileAsync('filename', zipFile)]; | ||
case 3: | ||
// save zip to file system (node.js only) | ||
_a.sent(); | ||
return [4 /*yield*/, zipService.createZipAsync(data)]; | ||
case 4: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
@@ -67,0 +79,0 @@ } |
@@ -40,5 +40,6 @@ "use strict"; | ||
var src_1 = require("src"); | ||
var zip_1 = require("src/zip"); | ||
var zip_1 = require("../zip"); | ||
var node_js_1 = require("../node-js"); | ||
var run = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var zipService, importService, data; | ||
var zipService, fileService, importService, file, data; | ||
return __generator(this, function (_a) { | ||
@@ -48,3 +49,6 @@ switch (_a.label) { | ||
zipService = new zip_1.ZipService({ | ||
filename: 'xxx', | ||
enableLog: true, | ||
context: 'node.js' | ||
}); | ||
fileService = new node_js_1.FileService({ | ||
enableLog: true | ||
@@ -63,3 +67,4 @@ }); | ||
}); | ||
return [4 /*yield*/, zipService.extractZipAsync()]; | ||
file = fileService.loadFileAsync('fileName'); | ||
return [4 /*yield*/, zipService.extractZipAsync(file)]; | ||
case 1: | ||
@@ -66,0 +71,0 @@ data = _a.sent(); |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./zip.service")); | ||
__exportStar(require("./zip.service"), exports); | ||
__exportStar(require("./zip.models"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -0,4 +1,5 @@ | ||
export declare type ZipContext = 'node.js' | 'browser'; | ||
export interface IZipServiceConfig { | ||
filename: string; | ||
enableLog: boolean; | ||
context: ZipContext; | ||
} |
@@ -6,3 +6,2 @@ import { IExportAllResult } from '../export'; | ||
private config; | ||
private readonly zipExtension; | ||
private readonly contentTypesName; | ||
@@ -19,7 +18,7 @@ private readonly contentItemsName; | ||
private readonly validationName; | ||
private readonly filenameWithExtension; | ||
constructor(config: IZipServiceConfig); | ||
extractZipAsync(): Promise<IImportSource>; | ||
createZipAsync(exportData: IExportAllResult): Promise<void>; | ||
extractZipAsync(zipFile: any): Promise<IImportSource>; | ||
createZipAsync(exportData: IExportAllResult): Promise<any>; | ||
private extractBinaryFilesAsync; | ||
private getZipOutputType; | ||
/** | ||
@@ -26,0 +25,0 @@ * Gets path to asset within zip folder. Uses tree format using asset ids such as: |
@@ -39,9 +39,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fs = require("fs"); | ||
var https_1 = require("https"); | ||
exports.ZipService = void 0; | ||
var JSZip = require("jszip"); | ||
var axios_1 = require("axios"); | ||
var ZipService = /** @class */ (function () { | ||
function ZipService(config) { | ||
this.config = config; | ||
this.zipExtension = '.zip'; | ||
this.contentTypesName = 'contentTypes.json'; | ||
@@ -58,22 +57,14 @@ this.contentItemsName = 'contentItems.json'; | ||
this.validationName = 'validation.json'; | ||
this.filenameWithExtension = config.filename + this.zipExtension; | ||
} | ||
ZipService.prototype.extractZipAsync = function () { | ||
ZipService.prototype.extractZipAsync = function (zipFile) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var filePath, file, unzippedFile, assets, result, _a, _b; | ||
var unzippedFile, assets, result, _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
filePath = "./" + this.filenameWithExtension; | ||
if (this.config.enableLog) { | ||
console.log("Reading file '" + filePath + "'"); | ||
console.log("Unzipping file"); | ||
} | ||
return [4 /*yield*/, fs.promises.readFile(filePath)]; | ||
return [4 /*yield*/, JSZip.loadAsync(zipFile)]; | ||
case 1: | ||
file = _c.sent(); | ||
if (this.config.enableLog) { | ||
console.log("Unzipping file"); | ||
} | ||
return [4 /*yield*/, JSZip.loadAsync(file)]; | ||
case 2: | ||
unzippedFile = _c.sent(); | ||
@@ -84,3 +75,3 @@ if (this.config.enableLog) { | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.assetsName)]; | ||
case 3: | ||
case 2: | ||
assets = _c.sent(); | ||
@@ -92,31 +83,31 @@ _a = {}; | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.contentTypesName)]; | ||
case 4: | ||
case 3: | ||
_b.contentTypes = _c.sent(); | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.languageVariantsName)]; | ||
case 5: | ||
case 4: | ||
_b.languageVariants = _c.sent(); | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.languages)]; | ||
case 6: | ||
case 5: | ||
_b.languages = _c.sent(); | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.contentItemsName)]; | ||
case 7: | ||
case 6: | ||
_b.contentItems = _c.sent(); | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.contentTypeSnippetsName)]; | ||
case 8: | ||
case 7: | ||
_b.contentTypeSnippets = _c.sent(); | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.taxonomiesName)]; | ||
case 9: | ||
case 8: | ||
_a.importData = (_b.taxonomies = _c.sent(), | ||
_b); | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.assetFoldersName)]; | ||
case 10: | ||
case 9: | ||
_a.assetFolders = _c.sent(); | ||
return [4 /*yield*/, this.extractBinaryFilesAsync(unzippedFile, assets)]; | ||
case 11: | ||
case 10: | ||
_a.binaryFiles = _c.sent(); | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.validationName)]; | ||
case 12: | ||
case 11: | ||
_a.validation = _c.sent(); | ||
return [4 /*yield*/, this.readAndParseJsonFile(unzippedFile, this.metadataName)]; | ||
case 13: | ||
case 12: | ||
result = (_a.metadata = _c.sent(), | ||
@@ -134,3 +125,3 @@ _a); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var zip, assetsFolder, _i, _a, asset, assetIdShortFolder, assetIdFolder, assetFilename, filePath, content; | ||
var zip, assetsFolder, _i, _a, asset, assetIdShortFolder, assetIdFolder, assetFilename, content; | ||
return __generator(this, function (_b) { | ||
@@ -166,15 +157,12 @@ switch (_b.label) { | ||
} | ||
filePath = './' + this.filenameWithExtension; | ||
if (this.config.enableLog) { | ||
console.log("Generating zip file '" + filePath + "'"); | ||
console.log("Creating zip file"); | ||
} | ||
return [4 /*yield*/, zip.generateAsync({ type: 'nodebuffer' })]; | ||
return [4 /*yield*/, zip.generateAsync({ type: this.getZipOutputType() })]; | ||
case 1: | ||
content = _b.sent(); | ||
console.log("Writing file '" + filePath + "'"); | ||
return [4 /*yield*/, fs.promises.writeFile(filePath, content)]; | ||
case 2: | ||
_b.sent(); | ||
console.log("File saved"); | ||
return [2 /*return*/]; | ||
if (this.config.enableLog) { | ||
console.log("Zip file prepared"); | ||
} | ||
return [2 /*return*/, content]; | ||
} | ||
@@ -198,3 +186,3 @@ }); | ||
assetFile = files[this.getFullAssetPath(asset.id, asset.file_name)]; | ||
return [4 /*yield*/, assetFile.async('nodebuffer')]; | ||
return [4 /*yield*/, assetFile.async(this.getZipOutputType())]; | ||
case 2: | ||
@@ -215,2 +203,11 @@ binaryData = _a.sent(); | ||
}; | ||
ZipService.prototype.getZipOutputType = function () { | ||
if (this.config.context === 'browser') { | ||
return 'blob'; | ||
} | ||
if (this.config.context === 'node.js') { | ||
return 'nodebuffer'; | ||
} | ||
throw Error("Unsupported context '" + this.config.context + "'"); | ||
}; | ||
/** | ||
@@ -243,19 +240,12 @@ * Gets path to asset within zip folder. Uses tree format using asset ids such as: | ||
ZipService.prototype.getBinaryDataFromUrl = function (url, enableLog) { | ||
return new Promise(function (resolve, reject) { | ||
https_1.get(url, function (res) { | ||
if (enableLog) { | ||
console.log("Downloading asset: " + url); | ||
} | ||
var data = []; | ||
res.on('data', function (chunk) { | ||
data.push(chunk); | ||
}) | ||
.on('end', function () { | ||
var buffer = Buffer.concat(data); | ||
resolve(buffer); | ||
}) | ||
.on('error', function (error) { | ||
reject(error); | ||
}); | ||
}); | ||
if (enableLog) { | ||
console.log("Downloading asset: " + url); | ||
} | ||
return axios_1.default.get(url, { | ||
responseType: 'arraybuffer', | ||
}).then(function (response) { | ||
if (enableLog) { | ||
console.log("Downloading asset completed: " + url); | ||
} | ||
return response.data; | ||
}); | ||
@@ -262,0 +252,0 @@ }; |
{ | ||
"name": "@kentico/kontent-backup-manager", | ||
"version": "1.4.0", | ||
"version": "1.5.0-next.0", | ||
"description": "This utility enables backup & restore of Kentico Kontent projects", | ||
"preferGlobal": true, | ||
"bin": { | ||
"kbm": "./_commonjs/src/cli/app.js" | ||
"kbm": "./_commonjs/src/node-js/cli/app.js" | ||
}, | ||
@@ -45,3 +45,3 @@ "repository": { | ||
"@kentico/kontent-management": "0.3.19", | ||
"jszip": "3.3.0", | ||
"jszip": "3.4.0", | ||
"rxjs": "6.5.5", | ||
@@ -52,10 +52,10 @@ "yargs": "15.3.1", | ||
"devDependencies": { | ||
"@types/jszip": "3.1.7", | ||
"@types/node": "13.11.0", | ||
"@types/yargs": "15.0.4", | ||
"standard-version": "7.1.0", | ||
"ts-node": "8.8.2", | ||
"tslint": "6.1.1", | ||
"typescript": "3.8.3" | ||
"@types/jszip": "3.4.1", | ||
"@types/node": "14.0.1", | ||
"@types/yargs": "15.0.5", | ||
"standard-version": "8.0.0", | ||
"ts-node": "8.10.1", | ||
"tslint": "6.1.2", | ||
"typescript": "3.9.2" | ||
} | ||
} |
@@ -284,2 +284,4 @@ import { HttpService } from '@kentico/kontent-core'; | ||
.toPromise(); | ||
} else { | ||
console.log(`Language with codename '${importLanguage.codename}' already exists in target project, skipping update operation`); | ||
} | ||
@@ -286,0 +288,0 @@ } |
import { ZipService } from 'src/zip'; | ||
import { ExportService } from '../export'; | ||
import { FileService } from '../node-js'; | ||
const run = async () => { | ||
const fileService = new FileService({ | ||
enableLog: true | ||
}); | ||
const zipService = new ZipService({ | ||
enableLog: true, | ||
context: 'node.js' | ||
}); | ||
const exportService = new ExportService({ | ||
@@ -18,8 +28,8 @@ apiKey: 'sourceProjectApiKey', | ||
// you can also save backup in file with ZipService | ||
const zipService = new ZipService({ | ||
filename: 'file', | ||
enableLog: true | ||
}); | ||
// prepare zip file | ||
const zipFile = await zipService.createZipAsync(data); | ||
// save zip to file system (node.js only) | ||
await fileService.writeFileAsync('filename', zipFile); | ||
await zipService.createZipAsync(data); | ||
@@ -26,0 +36,0 @@ }; |
import { ImportService } from 'src'; | ||
import { ZipService } from 'src/zip'; | ||
import { ZipService } from '../zip'; | ||
import { FileService } from '../node-js'; | ||
const run = async () => { | ||
const zipService = new ZipService({ | ||
filename: 'xxx', | ||
enableLog: true, | ||
context: 'node.js' | ||
}); | ||
const fileService = new FileService({ | ||
enableLog: true | ||
@@ -22,5 +27,8 @@ }); | ||
// read export data from zip | ||
const data = await zipService.extractZipAsync(); | ||
// read file | ||
const file = fileService.loadFileAsync('fileName'); | ||
// extract file | ||
const data = await zipService.extractZipAsync(file); | ||
// restore into target project | ||
@@ -27,0 +35,0 @@ await importService.importFromSourceAsync(data); |
@@ -0,4 +1,6 @@ | ||
export type ZipContext = 'node.js' | 'browser'; | ||
export interface IZipServiceConfig { | ||
filename: string; | ||
enableLog: boolean; | ||
context: ZipContext | ||
} |
import { AssetContracts } from '@kentico/kontent-management'; | ||
import * as fs from 'fs'; | ||
import { get } from 'https'; | ||
import JSZip = require('jszip'); | ||
import axios, {} from 'axios'; | ||
@@ -11,3 +10,2 @@ import { IExportAllResult } from '../export'; | ||
export class ZipService { | ||
private readonly zipExtension: string = '.zip'; | ||
@@ -26,20 +24,12 @@ private readonly contentTypesName: string = 'contentTypes.json'; | ||
private readonly filenameWithExtension: string; | ||
constructor(private config: IZipServiceConfig) { | ||
this.filenameWithExtension = config.filename + this.zipExtension; | ||
} | ||
public async extractZipAsync(): Promise<IImportSource> { | ||
const filePath = `./${this.filenameWithExtension}`; | ||
public async extractZipAsync(zipFile: any): Promise<IImportSource> { | ||
if (this.config.enableLog) { | ||
console.log(`Reading file '${filePath}'`); | ||
} | ||
const file = await fs.promises.readFile(filePath); | ||
if (this.config.enableLog) { | ||
console.log(`Unzipping file`); | ||
} | ||
const unzippedFile = await JSZip.loadAsync(file); | ||
const unzippedFile = await JSZip.loadAsync(zipFile); | ||
if (this.config.enableLog) { | ||
@@ -49,3 +39,2 @@ console.log(`Parsing zip contents`); | ||
const assets = await this.readAndParseJsonFile(unzippedFile, this.assetsName); | ||
const result = { | ||
@@ -74,3 +63,3 @@ importData: { | ||
public async createZipAsync(exportData: IExportAllResult): Promise<void> { | ||
public async createZipAsync(exportData: IExportAllResult): Promise<any> { | ||
const zip = new JSZip(); | ||
@@ -108,12 +97,13 @@ | ||
const filePath = './' + this.filenameWithExtension; | ||
if (this.config.enableLog) { | ||
console.log(`Creating zip file`); | ||
} | ||
const content = await zip.generateAsync({ type: this.getZipOutputType() }); | ||
if (this.config.enableLog) { | ||
console.log(`Generating zip file '${filePath}'`); | ||
console.log(`Zip file prepared`); | ||
} | ||
const content = await zip.generateAsync({ type: 'nodebuffer' }); | ||
console.log(`Writing file '${filePath}'`); | ||
await fs.promises.writeFile(filePath, content); | ||
console.log(`File saved`); | ||
return content; | ||
} | ||
@@ -132,3 +122,3 @@ | ||
const binaryData = await assetFile.async('nodebuffer'); | ||
const binaryData = await assetFile.async(this.getZipOutputType()); | ||
binaryFiles.push({ | ||
@@ -143,2 +133,14 @@ asset, | ||
private getZipOutputType(): 'nodebuffer' | 'blob' { | ||
if (this.config.context === 'browser') { | ||
return 'blob'; | ||
} | ||
if (this.config.context === 'node.js') { | ||
return 'nodebuffer'; | ||
} | ||
throw Error(`Unsupported context '${this.config.context}'`); | ||
} | ||
/** | ||
@@ -166,22 +168,16 @@ * Gets path to asset within zip folder. Uses tree format using asset ids such as: | ||
private getBinaryDataFromUrl(url: string, enableLog: boolean): Promise<any> { | ||
return new Promise((resolve, reject) => { | ||
get(url, res => { | ||
if (enableLog) { | ||
console.log(`Downloading asset: ${url}`); | ||
} | ||
return axios.get(url, { | ||
responseType: 'arraybuffer', | ||
}).then( | ||
response => { | ||
if (enableLog) { | ||
console.log(`Downloading asset: ${url}`); | ||
console.log(`Downloading asset completed: ${url}`); | ||
} | ||
const data: any[] = []; | ||
res.on('data', chunk => { | ||
data.push(chunk); | ||
}) | ||
.on('end', () => { | ||
const buffer = Buffer.concat(data); | ||
resolve(buffer); | ||
}) | ||
.on('error', error => { | ||
reject(error); | ||
}); | ||
}); | ||
}); | ||
return response.data; | ||
} | ||
); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
314284
111
4949
0
2
+ Addedjszip@3.4.0(transitive)
- Removedjszip@3.3.0(transitive)
Updatedjszip@3.4.0