0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00

Fixed invalid settings file path configuration

refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings
refs e457fd5fe0 (diff-b292e8480eee007786cc602f55ed05006a06b8da9fe6934d51fbef8328013278R36)

- There were two separate instances of the SettingsPathManager in route-settings and settings-loader causing the configured paths missmatching on test environment. Because of this missmatch, uploading and resetting the routes.yaml file didn't work!
This commit is contained in:
Naz 2021-11-23 20:17:38 +04:00 committed by naz
parent 04ab59c859
commit 6ee94f66b4
4 changed files with 32 additions and 45 deletions

View file

@ -1,5 +1,6 @@
const config = require('../../../shared/config'); const config = require('../../../shared/config');
const parseYaml = require('./yaml-parser'); const parseYaml = require('./yaml-parser');
const SettingsPathManager = require('@tryghost/settings-path-manager');
let settingsLoader; let settingsLoader;
let routeSettings; let routeSettings;
@ -10,8 +11,13 @@ module.exports = {
const SettingsLoader = require('./settings-loader'); const SettingsLoader = require('./settings-loader');
const DefaultSettingsManager = require('./default-settings-manager'); const DefaultSettingsManager = require('./default-settings-manager');
routeSettings = new RouteSettings(); const settingsPathManager = new SettingsPathManager({type: 'routes', paths: [config.getContentPath('settings')]});
settingsLoader = new SettingsLoader({parseYaml, settingFilePath: settingsPathManager.getDefaultFilePath()});
routeSettings = new RouteSettings({
settingsLoader,
settingsPath: settingsPathManager.getDefaultFilePath(),
backupPath: settingsPathManager.getBackupFilePath()
});
const defaultSettingsManager = new DefaultSettingsManager({ const defaultSettingsManager = new DefaultSettingsManager({
type: 'routes', type: 'routes',
extension: '.yaml', extension: '.yaml',
@ -19,11 +25,6 @@ module.exports = {
sourceFolderPath: config.get('paths').defaultSettings sourceFolderPath: config.get('paths').defaultSettings
}); });
settingsLoader = new SettingsLoader({
parseYaml,
storageFolderPath: config.getContentPath('settings')
});
return await defaultSettingsManager.ensureSettingsFileExists(); return await defaultSettingsManager.ensureSettingsFileExists();
}, },

View file

@ -6,21 +6,7 @@ const urlService = require('../url');
const debug = require('@tryghost/debug')('services:route-settings'); const debug = require('@tryghost/debug')('services:route-settings');
const errors = require('@tryghost/errors'); const errors = require('@tryghost/errors');
const tpl = require('@tryghost/tpl'); const tpl = require('@tryghost/tpl');
const config = require('../../../shared/config');
const bridge = require('../../../bridge'); const bridge = require('../../../bridge');
const SettingsLoader = require('./settings-loader');
const parseYaml = require('./yaml-parser');
const SettingsPathManager = require('@tryghost/settings-path-manager');
const settingsPathManager = new SettingsPathManager({
type: 'routes',
paths: [config.getContentPath('settings')]
});
const settingsLoader = new SettingsLoader({
parseYaml,
storageFolderPath: config.getContentPath('settings')
});
const messages = { const messages = {
loadError: 'Could not load {filename} file.' loadError: 'Could not load {filename} file.'
@ -39,7 +25,14 @@ const messages = {
*/ */
class RouteSettings { class RouteSettings {
constructor() { /**
*
* @param {Object} options
* @param {Object} options.settingsLoader
* @param {String} options.settingsPath
* @param {String} options.backupPath
*/
constructor({settingsLoader, settingsPath, backupPath}) {
/** /**
* md5 hashes of default routes settings * md5 hashes of default routes settings
* @private * @private
@ -53,6 +46,10 @@ class RouteSettings {
* @private * @private
*/ */
this.ext = 'yaml'; this.ext = 'yaml';
this.settingsLoader = settingsLoader;
this.settingsPath = settingsPath;
this.backupPath = backupPath;
} }
/** /**
@ -104,18 +101,15 @@ class RouteSettings {
} }
async setFromFilePath(filePath) { async setFromFilePath(filePath) {
const settingsPath = settingsPathManager.getDefaultFilePath(); await this.createBackupFile(this.settingsPath, this.backupPath);
const backupPath = settingsPathManager.getBackupFilePath(); await this.saveFile(filePath, this.settingsPath);
await this.createBackupFile(settingsPath, backupPath);
await this.saveFile(filePath, settingsPath);
urlService.resetGenerators({releaseResourcesOnly: true}); urlService.resetGenerators({releaseResourcesOnly: true});
const bringBackValidRoutes = async () => { const bringBackValidRoutes = async () => {
urlService.resetGenerators({releaseResourcesOnly: true}); urlService.resetGenerators({releaseResourcesOnly: true});
await this.restoreBackupFile(settingsPath, backupPath); await this.restoreBackupFile(this.settingsPath, this.backupPath);
return bridge.reloadFrontend(); return bridge.reloadFrontend();
}; };
@ -160,9 +154,7 @@ class RouteSettings {
} }
async get() { async get() {
const settingsFilePath = settingsPathManager.getDefaultFilePath(); return this.readFile(this.settingsPath);
return this.readFile(settingsFilePath);
} }
calculateHash(data) { calculateHash(data) {
@ -176,7 +168,7 @@ class RouteSettings {
} }
async getCurrentHash() { async getCurrentHash() {
const data = await settingsLoader.loadSettings(); const data = await this.settingsLoader.loadSettings();
return this.calculateHash(JSON.stringify(data)); return this.calculateHash(JSON.stringify(data));
} }

View file

@ -3,7 +3,6 @@ const debug = require('@tryghost/debug')('frontend:services:settings:settings-lo
const tpl = require('@tryghost/tpl'); const tpl = require('@tryghost/tpl');
const errors = require('@tryghost/errors'); const errors = require('@tryghost/errors');
const validate = require('./validate'); const validate = require('./validate');
const SettingsPathManager = require('@tryghost/settings-path-manager');
const messages = { const messages = {
settingsLoaderError: `Error trying to load YAML setting for {setting} from '{path}'.` settingsLoaderError: `Error trying to load YAML setting for {setting} from '{path}'.`
@ -13,17 +12,12 @@ class SettingsLoader {
/** /**
* @param {Object} options * @param {Object} options
* @param {Function} options.parseYaml yaml parser * @param {Function} options.parseYaml yaml parser
* @param {String} options.storageFolderPath routes settings file path * @param {String} options.settingFilePath routes settings file path
*/ */
constructor({parseYaml, storageFolderPath}) { constructor({parseYaml, settingFilePath}) {
this.parseYaml = parseYaml; this.parseYaml = parseYaml;
const settingsPathManager = new SettingsPathManager({ this.settingFilePath = settingFilePath;
type: 'routes',
paths: [storageFolderPath]
});
this.settingFilePath = settingsPathManager.getDefaultFilePath();
} }
/** /**

View file

@ -34,7 +34,7 @@ describe('UNIT > SettingsLoader:', function () {
it('reads a settings object for routes.yaml file', function () { it('reads a settings object for routes.yaml file', function () {
const settingsLoader = new SettingsLoader({ const settingsLoader = new SettingsLoader({
parseYaml: yamlParserStub, parseYaml: yamlParserStub,
storageFolderPath: '/content/data' settingFilePath: '/content/data/routes.yaml'
}); });
const settingsStubFile = { const settingsStubFile = {
routes: null, routes: null,
@ -69,7 +69,7 @@ describe('UNIT > SettingsLoader:', function () {
const settingsLoader = new SettingsLoader({ const settingsLoader = new SettingsLoader({
parseYaml: yamlParserStub, parseYaml: yamlParserStub,
storageFolderPath: storageFolderPath settingFilePath: expectedSettingsFile
}); });
const setting = settingsLoader.loadSettingsSync(); const setting = settingsLoader.loadSettingsSync();
should.exist(setting); should.exist(setting);
@ -89,7 +89,7 @@ describe('UNIT > SettingsLoader:', function () {
const settingsLoader = new SettingsLoader({ const settingsLoader = new SettingsLoader({
parseYaml: yamlParserStub, parseYaml: yamlParserStub,
storageFolderPath: storageFolderPath settingFilePath: path.join(storageFolderPath, 'routes.yaml')
}); });
try { try {
settingsLoader.loadSettingsSync(); settingsLoader.loadSettingsSync();
@ -122,7 +122,7 @@ describe('UNIT > SettingsLoader:', function () {
const settingsLoader = new SettingsLoader({ const settingsLoader = new SettingsLoader({
parseYaml: yamlParserStub, parseYaml: yamlParserStub,
storageFolderPath: storageFolderPath settingFilePath: expectedSettingsFile
}); });
try { try {