mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Refactored DB backup lib to async-await
- also improves jsdoc comments to make it easier to see the types of variables around the code
This commit is contained in:
parent
6bd2769146
commit
30327d62cd
1 changed files with 17 additions and 10 deletions
|
@ -9,12 +9,21 @@ const logging = require('@tryghost/logging');
|
||||||
const urlUtils = require('../../../shared/url-utils');
|
const urlUtils = require('../../../shared/url-utils');
|
||||||
const exporter = require('../exporter');
|
const exporter = require('../exporter');
|
||||||
|
|
||||||
const writeExportFile = function writeExportFile(exportResult) {
|
/**
|
||||||
|
* @param {object} exportResult
|
||||||
|
* @param {string} exportResult.filename
|
||||||
|
* @param {object} exportResult.data
|
||||||
|
*/
|
||||||
|
const writeExportFile = async (exportResult) => {
|
||||||
const filename = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename));
|
const filename = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename));
|
||||||
|
|
||||||
return Promise.resolve(fs.writeFile(filename, JSON.stringify(exportResult.data))).return(filename);
|
await fs.writeFile(filename, JSON.stringify(exportResult.data));
|
||||||
|
return filename;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} filename
|
||||||
|
*/
|
||||||
const readBackup = async (filename) => {
|
const readBackup = async (filename) => {
|
||||||
const parsedFileName = path.parse(filename);
|
const parsedFileName = path.parse(filename);
|
||||||
const sanitized = `${parsedFileName.name}${parsedFileName.ext}`;
|
const sanitized = `${parsedFileName.name}${parsedFileName.ext}`;
|
||||||
|
@ -35,21 +44,19 @@ const readBackup = async (filename) => {
|
||||||
* does an export, and stores this in a local file
|
* does an export, and stores this in a local file
|
||||||
* @returns {Promise<*>}
|
* @returns {Promise<*>}
|
||||||
*/
|
*/
|
||||||
const backup = function backup(options) {
|
const backup = async function backup(options = {}) {
|
||||||
logging.info('Creating database backup');
|
logging.info('Creating database backup');
|
||||||
options = options || {};
|
|
||||||
|
|
||||||
const props = {
|
const props = {
|
||||||
data: exporter.doExport(options),
|
data: exporter.doExport(options),
|
||||||
filename: exporter.fileName(options)
|
filename: exporter.fileName(options)
|
||||||
};
|
};
|
||||||
|
|
||||||
return Promise.props(props)
|
const exportResult = await Promise.props(props);
|
||||||
.then(writeExportFile)
|
const filename = await writeExportFile(exportResult);
|
||||||
.then(function successMessage(filename) {
|
|
||||||
logging.info('Database backup written to: ' + filename);
|
logging.info('Database backup written to: ' + filename);
|
||||||
return filename;
|
return filename;
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
Loading…
Reference in a new issue