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

Commit

Permalink
feat: updates to next version of management sdk + updates all deps, a…
Browse files Browse the repository at this point in the history
…dds yargs help for use in command line, changes distribution modules and paths
  • Loading branch information
Enngage committed Jun 7, 2021
1 parent 5e58846 commit 5bec931
Show file tree
Hide file tree
Showing 36 changed files with 350 additions and 207 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/definitions
/_commonjs
/_es2015
/_bundles
/dist
/tmp
/coverage
/out-tsc
Expand Down
4 changes: 2 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# But descend into directories

# Recursively allow files under subtree
!/src/**
!/_commonjs/**
!/lib/**
!/dist/**
!package.json
!.npmignore
!README.md
Expand Down
6 changes: 3 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "node",
"request": "launch",
"name": "Test backup",
"program": "${workspaceFolder}\\src\\node-js\\cli\\app.ts",
"program": "${workspaceFolder}\\lib\\node\\cli\\app.ts",
"preLaunchTask": "npm: build:all",
"sourceMaps": true,
"smartStep": true,
Expand All @@ -24,7 +24,7 @@
"type": "node",
"request": "launch",
"name": "Test clean",
"program": "${workspaceFolder}\\src\\node-js\\cli\\app.ts",
"program": "${workspaceFolder}\\lib\\node\\cli\\app.ts",
"preLaunchTask": "npm: build:all",
"sourceMaps": true,
"smartStep": true,
Expand All @@ -40,7 +40,7 @@
"type": "node",
"request": "launch",
"name": "Test restore",
"program": "${workspaceFolder}\\src\\node-js\\cli\\app.ts",
"program": "${workspaceFolder}\\lib\\node\\cli\\app.ts",
"preLaunchTask": "npm: build:all",
"sourceMaps": true,
"smartStep": true,
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/clean/clean.service.ts → lib/clean/clean.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AssetFolderModels, IManagementClient, ManagementClient } from '@kentico/kontent-management';
import { AssetFolderModels, ManagementClient } from '@kentico/kontent-management';

import { ItemType } from '../core';
import { ICleanConfig, ICleanResult } from './clean.models';

export class CleanService {
private readonly client: IManagementClient;
private readonly client: ManagementClient;

constructor(private config: ICleanConfig) {
this.client = new ManagementClient({
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
ContentItemContracts,
ContentTypeContracts,
ContentTypeSnippetContracts,
IManagementClient,
LanguageVariantContracts,
ManagementClient,
TaxonomyContracts,
Expand All @@ -18,7 +17,7 @@ import { ItemType } from '../core';
import { version } from '../../package.json';

export class ExportService {
private readonly client: IManagementClient;
private readonly client: ManagementClient;

constructor(private config: IExportConfig) {
this.client = new ManagementClient({
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 2 additions & 13 deletions src/import/import.service.ts → lib/import/import.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { HttpService } from '@kentico/kontent-core';
import {
AssetContracts,
AssetFolderContracts,
Expand All @@ -9,7 +8,6 @@ import {
ContentTypeContracts,
ContentTypeModels,
ContentTypeSnippetContracts,
IManagementClient,
LanguageContracts,
LanguageModels,
LanguageVariantContracts,
Expand All @@ -34,7 +32,7 @@ import { IBinaryFile, IImportConfig, IImportSource } from './import.models';

export class ImportService {
private readonly defaultLanguageId: string = '00000000-0000-0000-0000-000000000000';
private readonly client: IManagementClient;
private readonly client: ManagementClient;
private readonly publishedWorkflowStepName: string = 'Published';

/**
Expand All @@ -53,16 +51,7 @@ export class ImportService {
canRetryError: (err) => true, // so that timeout errors are retried
maxAttempts: 3,
deltaBackoffMs: 1000,
maxCumulativeWaitTimeMs: 60000
},
httpService: new HttpService({
axiosRequestConfig: {
// required for uploading large files
// https://github.com/axios/axios/issues/1362
maxContentLength: 'Infinity' as any,
maxBodyLength: 'Infinity' as any
}
})
}
});
}

Expand Down
File renamed without changes.
File renamed without changes.
85 changes: 65 additions & 20 deletions src/node-js/cli/app.ts → lib/node/cli/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import * as fs from 'fs';
import yargs = require('yargs');
import { readFileSync } from 'fs';
import * as yargs from 'yargs';

import { CleanService } from '../../clean';
import { ICliFileConfig, getFilenameWithoutExtension, CliAction, ItemType } from '../../core';
Expand All @@ -11,7 +11,45 @@ import { ProjectContracts, SharedModels } from '@kentico/kontent-management';
import { FileService } from '../file/file.service';
import { fileHelper } from '../file/file-helper';

const argv = yargs.argv;
const argv = yargs(process.argv.slice(2))
.example('kbm --action=backup --apiKey=xxx --projectId=xxx', 'Creates zip backup of Kontent project')
.example(
'kbm --action=restore --apiKey=xxx --projectId=xxx --zipFilename=backupFile',
'Read given zip file and recreates data in Kontent project'
)
.example(
'kbm --action=clean --apiKey=xxx --projectId=xxx',
'Deletes data from given Kontent project. Use with care, this action is not reversible.'
)
.alias('p', 'projectId')
.describe('p', 'ProjectId')
.alias('k', 'apiKey')
.describe('k', 'Management API Key')
.alias('a', 'action')
.describe('a', 'Action to perform. One of: backup, restore & clean')
.alias('z', 'zipFilename')
.describe('z', 'Name of zip used for export / restore')
.alias('l', 'enableLog')
.describe('l', 'Indicates if default logging is enabled (useful to indicate progress)')
.alias('f', 'force')
.describe(
'f',
'If enabled, project will we exported / restored even if there are data inconsistencies. Enabled by default.'
)
.alias('b', 'baseUrl')
.describe('b', 'Custom base URL for Management API calls.')
.alias('p', 'enablePublish')
.describe(
'p',
'Indicates if language variants published on the source project are also published on target. Enabled by default'
)
.alias('e', 'exportFilter')
.describe(
'e',
'Can be used to export only selected data types. Expects CSV of types. For example contentType,language will cause backup manager to export only content types & language data.'
)
.help('h')
.alias('h', 'help').argv;

const backupAsync = async (config: ICliFileConfig) => {
const exportService = new ExportService({
Expand Down Expand Up @@ -146,7 +184,7 @@ const validateConfig = (config?: ICliFileConfig) => {
}
};

const process = async () => {
const run = async () => {
const config = await getConfig();

validateConfig(config);
Expand Down Expand Up @@ -183,28 +221,35 @@ const canImport = (importData: IImportSource, config: ICliFileConfig) => {
};

const getConfig = async () => {
const configFilename: string = argv.config as string;
const resolvedArgs = await argv;
const configFilename: string = (await resolvedArgs.config) as string;

if (configFilename) {
// get config from file
const configFile = await fs.promises.readFile(`./${configFilename}`);
const configFile = readFileSync(`./${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 enablePublish: boolean | undefined = (argv.enablePublish as boolean | undefined) ?? true;
const projectId: string | undefined = argv.projectId as string | undefined;
const baseUrl: string | undefined = argv.baseUrl as string | undefined;
const zipFilename: string | undefined = (argv.zipFilename as string | undefined) ?? getDefaultBackupFilename();
const exportFilter: string | undefined = argv.exportFilter as string | undefined;

const exportFilterMapped: ItemType[] | undefined = exportFilter ? exportFilter.split(',').map(m => m.trim()).map(m => {
return m as ItemType;
}) : undefined;
const action: CliAction | undefined = resolvedArgs.action as CliAction | undefined;
const apiKey: string | undefined = resolvedArgs.apiKey as string | undefined;
const enableLog: boolean | undefined = (resolvedArgs.enableLog as boolean | undefined) ?? true;
const force: boolean | undefined = (resolvedArgs.force as boolean | undefined) ?? true;
const enablePublish: boolean | undefined = (resolvedArgs.enablePublish as boolean | undefined) ?? true;
const projectId: string | undefined = resolvedArgs.projectId as string | undefined;
const baseUrl: string | undefined = resolvedArgs.baseUrl as string | undefined;
const zipFilename: string | undefined =
(resolvedArgs.zipFilename as string | undefined) ?? getDefaultBackupFilename();
const exportFilter: string | undefined = resolvedArgs.exportFilter as string | undefined;

const exportFilterMapped: ItemType[] | undefined = exportFilter
? exportFilter
.split(',')
.map((m) => m.trim())
.map((m) => {
return m as ItemType;
})
: undefined;

if (!action) {
throw Error(`No action was provided`);
Expand Down Expand Up @@ -241,7 +286,7 @@ const getDefaultBackupFilename = () => {
}-${date.getFullYear()}-${date.getHours()}-${date.getMinutes()}`;
};

process()
run()
.then((m) => {})
.catch((err) => {
if (err instanceof SharedModels.ContentManagementBaseKontentError) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as fs from 'fs';
import { promises } from 'fs';

export class FileHelper {

async createFileInCurrentFolderAsync(filename: string, data: any): Promise<void> {
const filePath = './' + filename;

await fs.promises.writeFile(filePath, data);
await promises.writeFile(filePath, data);
}

}

export const fileHelper = new FileHelper();
export const fileHelper = new FileHelper();
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as fs from 'fs';
import { promises } from 'fs';
import { IFileServiceConfig } from './file.models';

export class FileService {
Expand All @@ -15,7 +15,7 @@ export class FileService {
if (this.config.enableLog) {
console.log(`Reading file '${filePath}'`);
}
const file = await fs.promises.readFile(filePath);
const file = await promises.readFile(filePath);
if (this.config.enableLog) {
console.log(`Reading file completed`);
}
Expand All @@ -27,7 +27,7 @@ export class FileService {
const filePath = this.getFilePath(fileNameWithoutExtension);

console.log(`Writing file '${filePath}'`);
await fs.promises.writeFile(filePath, content);
await promises.writeFile(filePath, content);
console.log(`File saved`);
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CleanService } from 'src';
import { CleanService } from 'lib';

const run = async () => {
const cleanService = new CleanService({
Expand Down
4 changes: 2 additions & 2 deletions src/samples/export-sample.ts → lib/samples/export-sample.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ZipService } from 'src/zip';
import { ZipService } from 'lib/zip';

import { ExportService } from '../export';
import { FileService } from '../node-js';
import { FileService } from '../node';

const run = async () => {
const fileService = new FileService({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ImportService } from 'src';
import { ImportService } from 'lib';
import { ZipService } from '../zip';
import { FileService } from '../node-js';
import { FileService } from '../node';

const run = async () => {
const zipService = new ZipService({
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/zip/zip.service.ts → lib/zip/zip.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AssetContracts } from '@kentico/kontent-management';
import JSZip = require('jszip');
import * as JSZip from 'jszip';
import axios, {} from 'axios';

import { IExportAllResult } from '../export';
Expand Down
Loading

0 comments on commit 5bec931

Please sign in to comment.