mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-30 06:12:03 +03:00
31e9434466
refs https://linear.app/tryghost/issue/CORE-35/refactor-route-and-redirect-settings - Actions logic related to file system operations (like ensuring files exist) should be done on the backend. The frontend will be receiving a unified JSON blob config without needing to know about filesystem
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
|
|
});
|
|
});
|
|
};
|