Added settings-path-manager module

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

- The module is a tiny path resolver for settings used in Ghost. A first obvious place it's used is for routes.yaml settings files. With a little bit of tweaking it should also be adopted by redirects services
This commit is contained in:
Naz 2021-09-30 19:22:09 +02:00
parent c886738bbc
commit a2a2a7c7be
11 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/node'
]
};

View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2013-2021 Ghost Foundation
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,53 @@
# Settings Path Manager
A library which helps locating configuration paths in Ghost. For example configs for dynamic routes or redirects.
## Install
`npm install @tryghost/settings-path-manager --save`
or
`yarn add @tryghost/settings-path-manager`
## Usage
Example use in to create routes.yaml configuration files:
```js
const config = require('../shared/config'); // or whatever place the storage folders are configured at
const settingsPathManager = new SettingsPathManager({
type: 'routes',
paths: [config.getContentPath('settings')]
});
const filePath = settingsPathManager.getDefaultFilePath();
console.log(config.getContentPath('settings')); // -> '/content/data/'
console.log(filePath); // -> '/content/data/routes.yaml'
```
## Develop
This is a mono repository, managed with [lerna](https://lernajs.io/).
Follow the instructions for the top-level repo.
1. `git clone` this repo & `cd` into it as usual
2. Run `yarn` to install top-level dependencies.
## Run
- `yarn dev`
## Test
- `yarn lint` run just eslint
- `yarn test` run lint and tests
# Copyright & License
Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).

View File

@ -0,0 +1 @@
module.exports = require('./lib/settings-path-manager');

View File

@ -0,0 +1,40 @@
const path = require('path');
const tpl = require('@tryghost/tpl');
const {IncorrectUsageError} = require('@tryghost/errors');
const messages = {
incorrectPathsParameter: 'Attempted to setup settings path manager without paths values.'
};
class SettingsPathManager {
/**
*
* @param {Object} options
* @param {String[]} options.paths - file location paths ordered in priority by where to locate them first
* @param {String} options.type setting file type, e.g: 'routes' or 'redirects'
* @param {String[]} [options.extensions] the supported file extensions with 'yaml' and 'json' defaults. Note 'yml' extension is ignored on purpose
*/
constructor({type, paths, extensions = ['yaml', 'json']}) {
if (!paths || !paths.length) {
throw new IncorrectUsageError({
message: tpl(messages.incorrectPathsParameter)
});
}
this.type = type;
this.filename = type;
this.paths = paths;
this.defaultPath = paths[0];
this.extensions = extensions;
this.defaultExtension = extensions[0];
}
getDefaultFilePath() {
const settingsFolder = this.defaultPath;
return path.join(settingsFolder, `${this.filename}.${this.defaultExtension}`);
}
}
module.exports = SettingsPathManager;

View File

@ -0,0 +1,30 @@
{
"name": "@tryghost/settings-path-manager",
"version": "0.0.0",
"repository": "https://github.com/TryGhost/Utils/tree/main/packages/settings-path-manager",
"author": "Ghost Foundation",
"license": "MIT",
"main": "index.js",
"scripts": {
"dev": "echo \"Implement me!\"",
"test": "NODE_ENV=testing c8 --check-coverage mocha './test/**/*.test.js'",
"lint": "eslint . --ext .js --cache",
"posttest": "yarn lint"
},
"files": [
"index.js",
"lib"
],
"publishConfig": {
"access": "public"
},
"devDependencies": {
"c8": "7.9.0",
"mocha": "9.1.2",
"should": "13.2.3",
"sinon": "11.1.2"
},
"dependencies": {
"tpl": "^0.3.0"
}
}

View File

@ -0,0 +1,6 @@
module.exports = {
plugins: ['ghost'],
extends: [
'plugin:ghost/test'
]
};

View File

@ -0,0 +1,57 @@
// Switch these lines once there are useful utils
// const testUtils = require('./utils');
require('./utils');
const should = require('should');
const SettingsPathManager = require('../');
describe('Settings Path Manager', function () {
it('throws when paths parameter is not provided', function () {
try {
const settingsPathManager = new SettingsPathManager({
paths: [],
type: 'routes'
});
should.fail(settingsPathManager, 'Should have errored');
} catch (err) {
should.exist(err);
err.message.should.match(/paths values/g);
}
});
describe('getDefaultFilePath', function () {
it('returns default file path based on routes configuration', function (){
const settingsPathManager = new SettingsPathManager({
paths: ['/content/settings', '/content/data'],
type: 'routes'
});
const path = settingsPathManager.getDefaultFilePath();
path.should.equal('/content/settings/routes.yaml');
});
it('returns default file path based on redirects configuration', function (){
const settingsPathManager = new SettingsPathManager({
paths: ['/content/data', '/content/settings'],
type: 'redirects'
});
const path = settingsPathManager.getDefaultFilePath();
path.should.equal('/content/data/redirects.yaml');
});
it('returns default file path based on redirects configuration with json extension', function (){
const settingsPathManager = new SettingsPathManager({
paths: ['/content/data', '/content/settings'],
type: 'redirects',
extensions: ['json', 'yaml']
});
const path = settingsPathManager.getDefaultFilePath();
path.should.equal('/content/data/redirects.json');
});
});
});

View File

@ -0,0 +1,11 @@
/**
* Custom Should Assertions
*
* Add any custom assertions to this file.
*/
// Example Assertion
// should.Assertion.add('ExampleAssertion', function () {
// this.params = {operator: 'to be a valid Example Assertion'};
// this.obj.should.be.an.Object;
// });

View File

@ -0,0 +1,11 @@
/**
* Test Utilities
*
* Shared utils for writing tests
*/
// Require overrides - these add globals for tests
require('./overrides');
// Require assertions - adds custom should assertions
require('./assertions');

View File

@ -0,0 +1,10 @@
// This file is required before any test is run
// Taken from the should wiki, this is how to make should global
// Should is a global in our eslint test config
global.should = require('should').noConflict();
should.extend();
// Sinon is a simple case
// Sinon is a global in our eslint test config
global.sinon = require('sinon');