Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: updates deps & adds support for execution in CLI without config…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
Enngage committed Feb 28, 2020
1 parent a476e4d commit b156e2e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
62 changes: 51 additions & 11 deletions src/cli/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@ 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';
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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
4 changes: 2 additions & 2 deletions src/zip/zip.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';

Expand Down

0 comments on commit b156e2e

Please sign in to comment.