From 7a91917424e6ee679cd0aaccf6aece431aef4455 Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 27 Sep 2021 12:48:48 +0200 Subject: [PATCH] Removed use of 'routes' parameter in route settings loader refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings - The only allowed route settings name is 'routes.yaml', which removes a need to parameterize the function as the location is permanent anyway - Simplifying the function in any possible way before extracting the common bits into an external lib --- core/frontend/services/settings/index.js | 2 +- core/server/services/route-settings/loader.js | 10 ++++------ core/server/services/route-settings/route-settings.js | 2 +- test/unit/services/settings/loader.test.js | 8 ++++---- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/core/frontend/services/settings/index.js b/core/frontend/services/settings/index.js index 3d28707dca..7477b7193f 100644 --- a/core/frontend/services/settings/index.js +++ b/core/frontend/services/settings/index.js @@ -18,7 +18,7 @@ module.exports = { }, getCurrentHash: async () => { - const data = await SettingsLoader.loadSettings('routes'); + const data = await SettingsLoader.loadSettings(); return calculateHash(JSON.stringify(data)); } diff --git a/core/server/services/route-settings/loader.js b/core/server/services/route-settings/loader.js index 2237707954..439ae30d97 100644 --- a/core/server/services/route-settings/loader.js +++ b/core/server/services/route-settings/loader.js @@ -28,11 +28,10 @@ const getSettingFilePath = (setting) => { * Functionally same as loadSettingsSync with exception of loading * settings asynchronously. This method is used at new places to read settings * to prevent blocking the eventloop - * - * @param {String} setting the requested settings as defined in setting knownSettings * @returns {Promise} settingsFile */ -const loadSettings = async (setting) => { +const loadSettings = async () => { + const setting = 'routes'; const {fileName, contentPath, filePath} = getSettingFilePath(setting); try { @@ -61,11 +60,10 @@ const loadSettings = async (setting) => { * Reads the desired settings YAML file and passes the * file to the YAML parser which then returns a JSON object. * NOTE: loading happens synchronously - * - * @param {String} setting the requested settings as defined in setting knownSettings * @returns {Object} settingsFile */ -module.exports.loadSettingsSync = function loadSettingsSync(setting) { +module.exports.loadSettingsSync = function loadSettingsSync() { + const setting = 'routes'; const {fileName, contentPath, filePath} = getSettingFilePath(setting); try { diff --git a/core/server/services/route-settings/route-settings.js b/core/server/services/route-settings/route-settings.js index 9e7b1add31..a958a7a2fc 100644 --- a/core/server/services/route-settings/route-settings.js +++ b/core/server/services/route-settings/route-settings.js @@ -152,7 +152,7 @@ module.exports.init = init; * @returns {Object} routes.yaml in JSON format */ module.exports.loadRouteSettingsSync = () => { - return SettingsLoader.loadSettingsSync('routes'); + return SettingsLoader.loadSettingsSync(); }; module.exports.api = { diff --git a/test/unit/services/settings/loader.test.js b/test/unit/services/settings/loader.test.js index e18922b4cd..8144586d81 100644 --- a/test/unit/services/settings/loader.test.js +++ b/test/unit/services/settings/loader.test.js @@ -61,7 +61,7 @@ describe('UNIT > Settings Service loader:', function () { it('can find yaml settings file and returns a settings object', function () { const fsReadFileSpy = sinon.spy(fs, 'readFileSync'); - const expectedSettingsFile = path.join(__dirname, '../../../utils/fixtures/settings/goodroutes.yaml'); + const expectedSettingsFile = path.join(__dirname, '../../../utils/fixtures/settings/routes.yaml'); yamlParserStub.returns(yamlStubFile); validateStub.returns({routes: {}, collections: {}, taxonomies: {}}); @@ -69,7 +69,7 @@ describe('UNIT > Settings Service loader:', function () { loadSettings.__set__('yamlParser', yamlParserStub); loadSettings.__set__('validate', validateStub); - const setting = loadSettings.loadSettingsSync('goodroutes'); + const setting = loadSettings.loadSettingsSync(); should.exist(setting); setting.should.be.an.Object().with.properties('routes', 'collections', 'taxonomies'); @@ -87,7 +87,7 @@ describe('UNIT > Settings Service loader:', function () { loadSettings.__set__('yamlParser', yamlParserStub); try { - loadSettings.loadSettingsSync('goodroutes'); + loadSettings.loadSettingsSync(); done(new Error('Loader should fail')); } catch (err) { should.exist(err); @@ -116,7 +116,7 @@ describe('UNIT > Settings Service loader:', function () { loadSettings.__set__('yamlParser', yamlParserStub); try { - loadSettings.loadSettingsSync('routes'); + loadSettings.loadSettingsSync(); done(new Error('Loader should fail')); } catch (err) { err.message.should.match(/Error trying to load YAML setting for routes from/);