From b156e2e7311b7dcea21f17d1d449ba5f845aa431 Mon Sep 17 00:00:00 2001 From: Enngage Date: Fri, 28 Feb 2020 09:39:45 +0100 Subject: [PATCH] feat: updates deps & adds support for execution in CLI without config file --- package-lock.json | 12 ++++---- package.json | 5 ++-- src/cli/app.ts | 62 ++++++++++++++++++++++++++++++++++-------- src/zip/zip.service.ts | 4 +-- 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index ea1e934..0248c9c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -55,15 +55,15 @@ } }, "@types/node": { - "version": "13.7.4", - "resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.4.tgz", - "integrity": "sha512-oVeL12C6gQS/GAExndigSaLxTrKpQPxewx9bOcwfvJiJge4rr7wNaph4J+ns5hrmIV2as5qxqN8YKthn9qh0jw==", + "version": "13.7.6", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.7.6.tgz", + "integrity": "sha512-eyK7MWD0R1HqVTp+PtwRgFeIsemzuj4gBFSQxfPHY5iMjS7474e5wq+VFgTcdpyHeNxyKSaetYAjdMLJlKoWqA==", "dev": true }, "@types/yargs": { - "version": "15.0.3", - "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.3.tgz", - "integrity": "sha512-XCMQRK6kfpNBixHLyHUsGmXrpEmFFxzMrcnSXFMziHd8CoNJo8l16FkHyQq4x+xbM7E2XL83/O78OD8u+iZTdQ==", + "version": "15.0.4", + "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-15.0.4.tgz", + "integrity": "sha512-9T1auFmbPZoxHz0enUFlUuKRy3it01R+hlggyVUMtnCTQRunsQYifnSGb8hET4Xo8yiC0o0r1paW3ud5+rbURg==", "dev": true, "requires": { "@types/yargs-parser": "*" diff --git a/package.json b/package.json index 5daa8ef..ae0fa9a 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "build:commonjs": "npm run tsc-local -- -m commonjs --outDir _commonjs", "build:all": "npm run build:commonjs", "test:all": "npm run build:all", + "test:backup:cli": "npm run build:commonjs && cd output && node ../_commonjs/cli/app --action=backup --projectId=b259760f-81c5-013a-05e7-69efb4b954e5 --apiKey=ew0KICAiYWxnIjogIkhTMjU2IiwNCiAgInR5cCI6ICJKV1QiDQp9.ew0KICAianRpIjogIjE3YTkwODIyNGM3MDQyM2E4NGZhMzkzMzZiNjUyYzU0IiwNCiAgImlhdCI6ICIxNTczNDgxNjAwIiwNCiAgImV4cCI6ICIxOTE5MDgxNjAwIiwNCiAgInByb2plY3RfaWQiOiAiYjI1OTc2MGY4MWM1MDEzYTA1ZTc2OWVmYjRiOTU0ZTUiLA0KICAidmVyIjogIjIuMS4wIiwNCiAgInVpZCI6ICJ1c3JfMHZRWUJDcUF2cm5vNXJpZkhuaVlFRyIsDQogICJhdWQiOiAibWFuYWdlLmtlbnRpY29jbG91ZC5jb20iDQp9.zBj0DHYKLUGiDJQ5_f3eUx02oTEzQ47X6N413ciSQQw", "test:backup": "npm run build:commonjs && cd output && node ../_commonjs/cli/app --config=backup-config.json", "test:clean": "npm run build:commonjs && cd output && node ../_commonjs/cli/app --config=clean-config.json", "test:restore": "npm run build:commonjs && cd output && node ../_commonjs/cli/app --config=restore-config.json", @@ -48,8 +49,8 @@ }, "devDependencies": { "@types/jszip": "3.1.7", - "@types/node": "13.7.4", - "@types/yargs": "15.0.3", + "@types/node": "13.7.6", + "@types/yargs": "15.0.4", "standard-version": "7.1.0", "ts-node": "8.6.2", "tslint": "6.0.0", diff --git a/src/cli/app.ts b/src/cli/app.ts index 5b63f93..1a453b8 100644 --- a/src/cli/app.ts +++ b/src/cli/app.ts @@ -3,7 +3,7 @@ import * as fs from 'fs'; import yargs = require('yargs'); import { CleanService } from '../clean'; -import { ICliFileConfig, fileHelper } from '../core'; +import { ICliFileConfig, fileHelper, CliAction } from '../core'; import { ExportService, IExportAllResult } from '../export'; import { IImportSource, ImportService } from '../import'; import { ZipService } from '../zip'; @@ -11,13 +11,6 @@ import { ProjectContracts } from '@kentico/kontent-management'; const argv = yargs.argv; -// config -const configFilename: string = argv.config as string; - -if (!configFilename) { - throw Error(`Please provide filename of config file using 'config' argument.`); -} - const backup = async (config: ICliFileConfig) => { const exportService = new ExportService({ apiKey: config.apiKey, @@ -132,9 +125,7 @@ const validateConfig = (config: any) => { }; const process = async () => { - const configFile = await fs.promises.readFile(`./${configFilename}`); - - const config = JSON.parse(configFile.toString()) as ICliFileConfig; + const config = await getConfig(); validateConfig(config); @@ -174,4 +165,53 @@ const canImport = (importData: IImportSource, config: ICliFileConfig) => { return false; }; +const getConfig = async() => { + const configFilename: string = argv.config as string; + + if (configFilename) { + // get config from file + const configFile = await fs.promises.readFile(`./${configFilename}`); + + return JSON.parse(configFile.toString()) as ICliFileConfig; + } + + const action: CliAction | undefined = argv.action as CliAction | undefined; + const apiKey: string | undefined = argv.apiKey as string | undefined; + const enableLog: boolean | undefined = (argv.enableLog as boolean | undefined) ?? true; + const force: boolean | undefined = (argv.force as boolean | undefined) ?? true; + const importLanguages: boolean | undefined = (argv.importLanguages as boolean | undefined) ?? true; + const projectId: string | undefined = argv.projectId as string | undefined; + const zipFilename: string | undefined = (argv.zipFilename as string | undefined) ?? getDefaultBackupFilename() + + if (!action) { + throw Error(`No action was provided`); + } + + if (!apiKey) { + throw Error(`Api key was not provided`); + } + + if (!projectId) { + throw Error(`Project id was not provided`); + } + + // get config from command line + const config: ICliFileConfig = { + action, + apiKey, + enableLog, + force, + importLanguages, + projectId, + zipFilename + }; + + return config; +} + +const getDefaultBackupFilename = () => { + const date = new Date(); + return `kontent-backup-${date.getDate()}-${date.getMonth() + 1}-${date.getFullYear()}-${date.getHours()}-${date.getMinutes()}`; +} + process(); diff --git a/src/zip/zip.service.ts b/src/zip/zip.service.ts index 8d41d69..01c8b15 100644 --- a/src/zip/zip.service.ts +++ b/src/zip/zip.service.ts @@ -3,7 +3,7 @@ import * as fs from 'fs'; import { get } from 'https'; import JSZip = require('jszip'); -import { IExportData, IExportMetadata, IExportAllResult } from '../export'; +import { IExportAllResult } from '../export'; import { IBinaryFile, IImportSource } from '../import'; import { IZipServiceConfig } from './zip.models'; @@ -18,7 +18,7 @@ export class ZipService { private readonly contentTypeSnippetsName: string = 'contentTypesSnippets.json'; private readonly metadataName: string = 'metadata.json'; private readonly languages: string = 'languages.json'; - private readonly filesName: string = 'files.json'; + private readonly filesName: string = 'files'; private readonly assetFoldersName: string = 'assetFolders.json'; private readonly validationName: string = 'validation.json';