mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
- All var declarations are now const or let as per ES6 - All comma-separated lists / chained declarations are now one declaration per line - This is for clarity/readability but also made running the var-to-const/let switch smoother - ESLint rules updated to match How this was done: - npm install -g jscodeshift - git clone https://github.com/cpojer/js-codemod.git - git clone git@github.com:TryGhost/Ghost.git shallow-ghost - cd shallow-ghost - jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2 - jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2 - yarn - yarn test - yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode - grunt test-regression - sorted!
60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
// # Backup Database
|
|
// Provides for backing up the database before making potentially destructive changes
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
const Promise = require('bluebird');
|
|
const config = require('../../config');
|
|
const common = require('../../lib/common');
|
|
const urlUtils = require('../../lib/url-utils');
|
|
const exporter = require('../exporter');
|
|
let writeExportFile;
|
|
let backup;
|
|
|
|
writeExportFile = function writeExportFile(exportResult) {
|
|
const filename = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename));
|
|
|
|
return fs.writeFile(filename, JSON.stringify(exportResult.data)).return(filename);
|
|
};
|
|
|
|
const readBackup = async (filename) => {
|
|
const parsedFileName = path.parse(filename);
|
|
const sanitized = `${parsedFileName.name}${parsedFileName.ext}`;
|
|
const backupPath = path.resolve(urlUtils.urlJoin(config.get('paths').contentPath, 'data', sanitized));
|
|
|
|
const exists = await fs.pathExists(backupPath);
|
|
|
|
if (exists) {
|
|
const backup = await fs.readFile(backupPath);
|
|
return JSON.parse(backup);
|
|
} else {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* ## Backup
|
|
* does an export, and stores this in a local file
|
|
* @returns {Promise<*>}
|
|
*/
|
|
backup = function backup(options) {
|
|
common.logging.info('Creating database backup');
|
|
options = options || {};
|
|
|
|
const props = {
|
|
data: exporter.doExport(options),
|
|
filename: exporter.fileName(options)
|
|
};
|
|
|
|
return Promise.props(props)
|
|
.then(writeExportFile)
|
|
.then(function successMessage(filename) {
|
|
common.logging.info('Database backup written to: ' + filename);
|
|
return filename;
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
backup,
|
|
readBackup
|
|
};
|