Skip to content

Commit 5a8ec5d

Browse files
author
Michal Paukert
committed
Default backup name as date
1 parent 3ae0c02 commit 5a8ec5d

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

src/cmds/backup.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import yargs from 'yargs';
22
import chalk from 'chalk';
33
import { environmentConfigExists, getEnvironmentsConfig } from '../utils/environmentUtils';
44
import { CleanService, ExportService, ImportService, ZipService } from '@kentico/kontent-backup-manager';
5+
import { getFileBackupName } from '../utils/fileUtils';
6+
import { IProcessedItem } from '@kentico/kontent-backup-manager/_commonjs/src';
57

68
const kontentBackupCommand: yargs.CommandModule = {
79
command: 'backup',
@@ -21,7 +23,7 @@ const kontentBackupCommand: yargs.CommandModule = {
2123
},
2224
log: {
2325
alias: 'l',
24-
describe: 'Enables/Disables logging (default: enabled)',
26+
describe: 'Enables/Disables logging',
2527
type: 'boolean',
2628
default: true,
2729
},
@@ -72,8 +74,9 @@ const kontentBackupCommand: yargs.CommandModule = {
7274
apiKey = environments[argv.environment].apiKey || argv.apiKey;
7375
}
7476

77+
const defaultBackupName = getFileBackupName();
7578
const zipService = new ZipService({
76-
filename: argv.name,
79+
filename: argv.name || defaultBackupName,
7780
enableLog: argv.log,
7881
});
7982

@@ -84,7 +87,7 @@ const kontentBackupCommand: yargs.CommandModule = {
8487
const exportService = new ExportService({
8588
apiKey: apiKey,
8689
projectId: projectId,
87-
onExport: (item) => {
90+
onExport: (item: IProcessedItem) => {
8891
if (argv.log) {
8992
console.log(`Exported: ${item.title} | ${item.type}`);
9093
}
@@ -97,7 +100,7 @@ const kontentBackupCommand: yargs.CommandModule = {
97100
case 'restore':
98101
const zipData = await zipService.extractZipAsync();
99102
const importService = new ImportService({
100-
onImport: (item) => {
103+
onImport: (item: IProcessedItem) => {
101104
if (argv.log) {
102105
console.log(`Imported: ${item.title} | ${item.type}`);
103106
}
@@ -113,7 +116,7 @@ const kontentBackupCommand: yargs.CommandModule = {
113116

114117
case 'clean':
115118
const cleanService = new CleanService({
116-
onDelete: (item) => {
119+
onDelete: (item: IProcessedItem) => {
117120
if (argv.log) {
118121
console.log(`Deleted: ${item.title} | ${item.type}`);
119122
}

src/utils/fileUtils.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import * as path from 'path';
55
export const listFiles = (fileExtension: string): Dirent[] => {
66
return fs
77
.readdirSync(getMigrationDirectory(), { withFileTypes: true })
8-
.filter(f => f.isFile())
9-
.filter(f => f.name.endsWith(fileExtension));
8+
.filter((f) => f.isFile())
9+
.filter((f) => f.name.endsWith(fileExtension));
1010
};
1111

1212
export const fileExists = (filePath: PathLike): boolean => {
@@ -22,12 +22,15 @@ export const getFileWithExtension = (filename: string, defaultExtension: string
2222
const hasFileExtension = path.extname(filename);
2323

2424
if (hasFileExtension) {
25-
const normalized = filename
26-
.split(defaultExtension)
27-
.slice(0, -1)
28-
.join(defaultExtension);
25+
const normalized = filename.split(defaultExtension).slice(0, -1).join(defaultExtension);
2926
return normalized + defaultExtension;
3027
} else {
3128
return filename + defaultExtension;
3229
}
3330
};
31+
32+
export const getFileBackupName = () => {
33+
const currentDate = new Date();
34+
const formatted = `${currentDate.getFullYear()}-${currentDate.getMonth()}-${currentDate.getDate()}-${currentDate.getTime()}`;
35+
return `backup-${formatted}`;
36+
};

0 commit comments

Comments
 (0)