|  | 
|  | 1 | +import yargs from 'yargs'; | 
|  | 2 | +import chalk from 'chalk'; | 
|  | 3 | +import { environmentConfigExists, getEnvironmentsConfig } from '../utils/environmentUtils'; | 
|  | 4 | +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'; | 
|  | 7 | + | 
|  | 8 | +const kontentBackupCommand: yargs.CommandModule = { | 
|  | 9 | +    command: 'backup', | 
|  | 10 | +    describe: 'Kontent backup tool to backup & restore Kentico Kontent projects through Management API.', | 
|  | 11 | +    builder: (yargs: any) => | 
|  | 12 | +        yargs | 
|  | 13 | +            .options({ | 
|  | 14 | +                action: { | 
|  | 15 | +                    alias: 'a', | 
|  | 16 | +                    describe: 'Action for backup', | 
|  | 17 | +                    type: 'string', | 
|  | 18 | +                }, | 
|  | 19 | +                name: { | 
|  | 20 | +                    alias: 'n', | 
|  | 21 | +                    describe: 'Name of zip file', | 
|  | 22 | +                    type: 'string', | 
|  | 23 | +                }, | 
|  | 24 | +                log: { | 
|  | 25 | +                    alias: 'l', | 
|  | 26 | +                    describe: 'Enables/Disables logging', | 
|  | 27 | +                    type: 'boolean', | 
|  | 28 | +                    default: true, | 
|  | 29 | +                }, | 
|  | 30 | +                'project-id': { | 
|  | 31 | +                    alias: 'p', | 
|  | 32 | +                    describe: 'Project ID to run the migration script on', | 
|  | 33 | +                    type: 'string', | 
|  | 34 | +                }, | 
|  | 35 | +                'api-key': { | 
|  | 36 | +                    alias: 'k', | 
|  | 37 | +                    describe: 'Management API key', | 
|  | 38 | +                    type: 'string', | 
|  | 39 | +                }, | 
|  | 40 | +                environment: { | 
|  | 41 | +                    alias: 'e', | 
|  | 42 | +                    describe: 'Environment name', | 
|  | 43 | +                    type: 'string', | 
|  | 44 | +                }, | 
|  | 45 | +            }) | 
|  | 46 | +            .conflicts('environment', 'api-key') | 
|  | 47 | +            .conflicts('environment', 'project-id') | 
|  | 48 | +            .check((args: any) => { | 
|  | 49 | +                if (!args.environment && !(args.projectId && args.apiKey)) { | 
|  | 50 | +                    throw new Error(chalk.red('Specify an environment or a project ID with its Management API key.')); | 
|  | 51 | +                } | 
|  | 52 | + | 
|  | 53 | +                if (args.environment) { | 
|  | 54 | +                    if (!environmentConfigExists()) { | 
|  | 55 | +                        throw new Error(chalk.red(`Cannot find the environment configuration file. Add an environment named \"${args.environment}\" first.`)); | 
|  | 56 | +                    } | 
|  | 57 | + | 
|  | 58 | +                    const environments = getEnvironmentsConfig(); | 
|  | 59 | + | 
|  | 60 | +                    if (!environments[args.environment]) { | 
|  | 61 | +                        throw new Error(chalk.red(`Cannot find the \"${args.environment}\" environment.`)); | 
|  | 62 | +                    } | 
|  | 63 | +                } | 
|  | 64 | + | 
|  | 65 | +                return true; | 
|  | 66 | +            }), | 
|  | 67 | +    handler: async (argv: any) => { | 
|  | 68 | +        let projectId = argv.projectId; | 
|  | 69 | +        let apiKey = argv.apiKey; | 
|  | 70 | +        if (argv.environment) { | 
|  | 71 | +            const environments = getEnvironmentsConfig(); | 
|  | 72 | + | 
|  | 73 | +            projectId = environments[argv.environment].projectId || argv.projectId; | 
|  | 74 | +            apiKey = environments[argv.environment].apiKey || argv.apiKey; | 
|  | 75 | +        } | 
|  | 76 | + | 
|  | 77 | +        const defaultBackupName = getFileBackupName(); | 
|  | 78 | +        const zipService = new ZipService({ | 
|  | 79 | +            filename: argv.name || defaultBackupName, | 
|  | 80 | +            enableLog: argv.log, | 
|  | 81 | +        }); | 
|  | 82 | + | 
|  | 83 | +        console.log('Starting backup tool'); | 
|  | 84 | + | 
|  | 85 | +        switch (argv.action) { | 
|  | 86 | +            case 'backup': | 
|  | 87 | +                const exportService = new ExportService({ | 
|  | 88 | +                    apiKey: apiKey, | 
|  | 89 | +                    projectId: projectId, | 
|  | 90 | +                    onExport: (item: IProcessedItem) => { | 
|  | 91 | +                        if (argv.log) { | 
|  | 92 | +                            console.log(`Exported: ${item.title} | ${item.type}`); | 
|  | 93 | +                        } | 
|  | 94 | +                    }, | 
|  | 95 | +                }); | 
|  | 96 | +                const exportedData = await exportService.exportAllAsync(); | 
|  | 97 | +                await zipService.createZipAsync(exportedData); | 
|  | 98 | +                break; | 
|  | 99 | + | 
|  | 100 | +            case 'restore': | 
|  | 101 | +                const zipData = await zipService.extractZipAsync(); | 
|  | 102 | +                const importService = new ImportService({ | 
|  | 103 | +                    onImport: (item: IProcessedItem) => { | 
|  | 104 | +                        if (argv.log) { | 
|  | 105 | +                            console.log(`Imported: ${item.title} | ${item.type}`); | 
|  | 106 | +                        } | 
|  | 107 | +                    }, | 
|  | 108 | +                    projectId: projectId, | 
|  | 109 | +                    apiKey: apiKey, | 
|  | 110 | +                    enableLog: argv.log, | 
|  | 111 | +                    fixLanguages: true, | 
|  | 112 | +                    workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000', | 
|  | 113 | +                }); | 
|  | 114 | +                await importService.importFromSourceAsync(zipData); | 
|  | 115 | +                break; | 
|  | 116 | + | 
|  | 117 | +            case 'clean': | 
|  | 118 | +                const cleanService = new CleanService({ | 
|  | 119 | +                    onDelete: (item: IProcessedItem) => { | 
|  | 120 | +                        if (argv.log) { | 
|  | 121 | +                            console.log(`Deleted: ${item.title} | ${item.type}`); | 
|  | 122 | +                        } | 
|  | 123 | +                    }, | 
|  | 124 | +                    projectId: projectId, | 
|  | 125 | +                    apiKey: apiKey, | 
|  | 126 | +                }); | 
|  | 127 | + | 
|  | 128 | +                await cleanService.cleanAllAsync(); | 
|  | 129 | +                break; | 
|  | 130 | + | 
|  | 131 | +            default: | 
|  | 132 | +                throw new Error('Unknown action type'); | 
|  | 133 | +        } | 
|  | 134 | + | 
|  | 135 | +        console.log('Completed'); | 
|  | 136 | +        process.exit(0); | 
|  | 137 | +    }, | 
|  | 138 | +}; | 
|  | 139 | + | 
|  | 140 | +// yargs needs exported command in exports object | 
|  | 141 | +Object.assign(exports, kontentBackupCommand); | 
0 commit comments