Added getBackupFilePath to settings-path-manager module

refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings

- getBackupFilePath is yet another utility function tha's used often with Ghost config files like so:
- ca149f2c0e/core/server/services/redirects/settings.js (L147-L151)
- ca149f2c0e/core/server/services/route-settings/route-settings.js (L42-L45)
This commit is contained in:
Naz 2021-09-30 20:14:21 +02:00
parent 8f4c4f66b5
commit 98f4f90b19
3 changed files with 22 additions and 0 deletions

View File

@ -1,5 +1,6 @@
const path = require('path');
const tpl = require('@tryghost/tpl');
const format = require('date-fns/format');
const {IncorrectUsageError} = require('@tryghost/errors');
const messages = {
@ -35,6 +36,12 @@ class SettingsPathManager {
const settingsFolder = this.defaultPath;
return path.join(settingsFolder, `${this.filename}.${this.defaultExtension}`);
}
getBackupFilePath() {
const settingsFolder = this.defaultPath;
const dateStamp = format(new Date(), 'yyyy-MM-dd-HH-mm-ss');
return path.join(settingsFolder, `${this.filename}-${dateStamp}.${this.defaultExtension}`);
}
}
module.exports = SettingsPathManager;

View File

@ -25,6 +25,7 @@
"sinon": "11.1.2"
},
"dependencies": {
"date-fns": "^2.24.0",
"tpl": "^0.3.0"
}
}

View File

@ -54,4 +54,18 @@ describe('Settings Path Manager', function () {
path.should.equal('/content/data/redirects.json');
});
});
describe('getBackupFilePath', function () {
it('returns a path to store a backup', function (){
const settingsPathManager = new SettingsPathManager({
paths: ['/content/data', '/content/settings'],
type: 'routes',
extensions: ['yaml']
});
const path = settingsPathManager.getBackupFilePath();
path.should.match(/\/content\/data\/routes-\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}.yaml/);
});
});
});