mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 18:01:36 +03:00
9822dbc1a2
refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings - i18n is an old pattern we are getting rid of. By removing it here we get rid of an extra dependancy on frontend's "proxy" module
44 lines
1.6 KiB
JavaScript
44 lines
1.6 KiB
JavaScript
const fs = require('fs-extra');
|
|
const Promise = require('bluebird');
|
|
const path = require('path');
|
|
const debug = require('@tryghost/debug')('frontend:services:settings:ensure-settings');
|
|
const tpl = require('@tryghost/tpl');
|
|
const errors = require('@tryghost/errors');
|
|
const config = require('../../../shared/config');
|
|
|
|
const messages = {
|
|
ensureSettings: 'Error trying to access settings files in {path}.'
|
|
};
|
|
|
|
/**
|
|
* Makes sure file is in the `/content/settings` directory. If not, copy the default over.
|
|
* @param {String} fileName - name of the setting file
|
|
* @returns {Promise<any>}
|
|
*/
|
|
module.exports = function ensureSettingsFile(fileName) {
|
|
const contentPath = config.getContentPath('settings');
|
|
const defaultSettingsPath = config.get('paths').defaultSettings;
|
|
|
|
const defaultFileName = `default-${fileName}`;
|
|
const filePath = path.join(contentPath, fileName);
|
|
|
|
return Promise.resolve(fs.readFile(filePath, 'utf8'))
|
|
.catch({code: 'ENOENT'}, () => {
|
|
const defaultFilePath = path.join(defaultSettingsPath, defaultFileName);
|
|
// CASE: file doesn't exist, copy it from our defaults
|
|
return fs.copy(
|
|
defaultFilePath,
|
|
filePath
|
|
).then(() => {
|
|
debug(`'${defaultFileName}' copied to ${contentPath}.`);
|
|
});
|
|
}).catch((error) => {
|
|
// CASE: we might have a permission error, as we can't access the directory
|
|
throw new errors.GhostError({
|
|
message: tpl(messages.ensureSettings, {path: contentPath}),
|
|
err: error,
|
|
context: error.path
|
|
});
|
|
});
|
|
};
|