This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: first working prototype with CLI support
- Loading branch information
Showing
13 changed files
with
280 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,123 @@ | ||
#!/usr/bin/env node | ||
import * as fs from 'fs'; | ||
import yargs = require('yargs'); | ||
|
||
import { CleanService } from '../clean'; | ||
import { CliAction } from '../core'; | ||
import { ICliFileConfig } from '../core'; | ||
import { ExportService } from '../export'; | ||
import { ImportService } from '../import'; | ||
import { ZipService } from '../zip'; | ||
|
||
const argv = yargs.argv; | ||
|
||
// config | ||
const projectId: string = argv.projectId as string; | ||
const action: string = argv.action as string; | ||
const configFilename: string = argv.config as string; | ||
|
||
if (!projectId) { | ||
throw Error(`Please provide project id using 'projectId' argument`); | ||
if (!configFilename) { | ||
throw Error(`Please provide filename of config file using 'config' argument.`); | ||
} | ||
|
||
if (!action) { | ||
throw Error(`Please provide action type using 'action' argument.`); | ||
} | ||
const backup = async (config: ICliFileConfig) => { | ||
const exportService = new ExportService({ | ||
apiKey: config.apiKey, | ||
projectId: config.projectId, | ||
onExport: item => { | ||
if (config.enableLog) { | ||
console.log(`Exported: ${item.title} | ${item.type}`); | ||
} | ||
} | ||
}); | ||
|
||
const zipService = new ZipService({ | ||
filename: config.zipFilename, | ||
enableLog: config.enableLog | ||
}); | ||
|
||
let mappedAction: CliAction; | ||
const response = await exportService.exportAllAsync(); | ||
|
||
if (action.toLowerCase() === 'backup') { | ||
mappedAction = 'backup'; | ||
} else if (action.toLowerCase() === 'restore') { | ||
mappedAction = 'restore'; | ||
} else if (action.toLowerCase() === 'clean') { | ||
mappedAction = 'clean'; | ||
} else { | ||
throw Error(`Unsupported action type '${action}'.`); | ||
} | ||
await zipService.createZipAsync(response.data, response.metadata); | ||
}; | ||
|
||
const clean = async (config: ICliFileConfig) => { | ||
const cleanService = new CleanService({ | ||
onDelete: item => { | ||
if (config.enableLog) { | ||
console.log(`Deleted: ${item.title} | ${item.type}`); | ||
} | ||
}, | ||
projectId: config.projectId, | ||
apiKey: config.apiKey | ||
}); | ||
|
||
await cleanService.cleanAllAsync(); | ||
}; | ||
|
||
const exportService = new ExportService({ | ||
apiKey: sourceApiKey, | ||
projectId: sourceProjectId | ||
}); | ||
const restore = async (config: ICliFileConfig) => { | ||
const zipService = new ZipService({ | ||
filename: config.zipFilename, | ||
enableLog: config.enableLog | ||
}); | ||
|
||
const importService = new ImportService({ | ||
processItem: item => { | ||
console.log('imported item: ' + item.title); | ||
}, | ||
projectId: targetProjectId, | ||
apiKey: targetApiKey, | ||
workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000', | ||
skip: { | ||
languages: true | ||
} | ||
}); | ||
const data = await zipService.extractZipAsync(); | ||
|
||
const cleanService = new CleanService({ | ||
processItem: item => { | ||
console.log('deleted item: ' + item.title); | ||
}, | ||
projectId: targetProjectId, | ||
apiKey: targetApiKey | ||
}); | ||
const importService = new ImportService({ | ||
onImport: item => { | ||
if (config.enableLog) { | ||
console.log(`Imported: ${item.title} | ${item.type}`); | ||
} | ||
}, | ||
projectId: config.projectId, | ||
apiKey: config.apiKey, | ||
workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000', | ||
process: { | ||
language: item => config.importLanguages, | ||
contentItem: item => { | ||
return true; | ||
} | ||
} | ||
}); | ||
|
||
const zipService = new ZipService({ | ||
filename: 'test' | ||
}); | ||
await importService.importFromSourceAsync(data); | ||
}; | ||
|
||
const backup = async () => { | ||
const response = await exportService.exportAllAsync(); | ||
const validateConfig = (config: any) => { | ||
if (!config) { | ||
throw Error(`Invalid config file`); | ||
} | ||
|
||
await zipService.createZipAsync(response.data, response.metadata); | ||
const projectId = config.projectId; | ||
const apiKey = config.apiKey; | ||
const action = config.action; | ||
|
||
/* | ||
codenameTranslateHelper.replaceIdReferencesWithCodenames(response.data, response.data); | ||
const zip = new JSZip(); | ||
zip.file('test.json', JSON.stringify(response.data)); | ||
const content = await zip.generateAsync({ type: 'nodebuffer' }); | ||
await fs.promises.writeFile( | ||
'./processed.zip', | ||
content | ||
); | ||
*/ | ||
}; | ||
if (!projectId) { | ||
throw Error('Invalid project id'); | ||
} | ||
|
||
const clean = async () => { | ||
await cleanService.cleanAllAsync(); | ||
}; | ||
if (!apiKey) { | ||
throw Error('Invalid api key'); | ||
} | ||
|
||
const restore = async () => { | ||
// const response = await exportService.exportAllAsync(); | ||
if (!action) { | ||
throw Error('Invalid action'); | ||
} | ||
}; | ||
|
||
const data = await zipService.extractZipAsync(); | ||
const process = async () => { | ||
const configFile = await fs.promises.readFile(`./${configFilename}`); | ||
|
||
await importService.importFromSourceAsync(data); | ||
}; | ||
const config = JSON.parse(configFile.toString()) as ICliFileConfig; | ||
|
||
if (mappedAction === 'backup') { | ||
backup(); | ||
} | ||
validateConfig(config); | ||
|
||
if (mappedAction === 'clean') { | ||
clean(); | ||
} | ||
if (config.action === 'backup') { | ||
backup(config); | ||
} else if (config.action === 'clean') { | ||
clean(config); | ||
} else if (config.action === 'restore') { | ||
restore(config); | ||
} else { | ||
throw Error(`Invalid action`); | ||
} | ||
}; | ||
|
||
if (mappedAction === 'restore') { | ||
restore(); | ||
} | ||
process(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"projectId": "x", | ||
"apiKey": "y", | ||
"zipFilename": "backup", | ||
"action": "backup", | ||
"enableLog": true, | ||
"importLanguages": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.