2016-12-23 16:59:48 -02:00
|
|
|
import yaml from "js-yaml";
|
2017-04-14 22:36:41 +01:00
|
|
|
import { set, defaultsDeep, get } from "lodash";
|
2017-01-26 19:23:42 -02:00
|
|
|
import { authenticateUser } from "../actions/auth";
|
2016-12-23 16:59:48 -02:00
|
|
|
import * as publishModes from "../constants/publishModes";
|
2016-02-25 00:45:56 -08:00
|
|
|
|
2016-12-23 16:59:48 -02:00
|
|
|
export const CONFIG_REQUEST = "CONFIG_REQUEST";
|
|
|
|
export const CONFIG_SUCCESS = "CONFIG_SUCCESS";
|
|
|
|
export const CONFIG_FAILURE = "CONFIG_FAILURE";
|
2016-02-25 00:45:56 -08:00
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
const defaults = {
|
|
|
|
publish_mode: publishModes.SIMPLE,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function applyDefaults(config) {
|
|
|
|
// Make sure there is a public folder
|
|
|
|
set(defaults,
|
2016-12-23 16:59:48 -02:00
|
|
|
"public_folder",
|
|
|
|
config.media_folder.charAt(0) === "/" ? config.media_folder : `/${ config.media_folder }`);
|
2016-11-11 12:17:01 +01:00
|
|
|
|
|
|
|
return defaultsDeep(config, defaults);
|
|
|
|
}
|
|
|
|
|
2017-04-14 22:36:41 +01:00
|
|
|
export function validateConfig(config) {
|
|
|
|
if (!get(config, 'backend')) {
|
|
|
|
throw new Error("Error in configuration file: A `backend` wasn't found. Check your config.yml file.");
|
|
|
|
}
|
|
|
|
if (!get(config, ['backend', 'name'])) {
|
|
|
|
throw new Error("Error in configuration file: A `backend.name` wasn't found. Check your config.yml file.");
|
|
|
|
}
|
|
|
|
if (typeof config.backend.name !== 'string') {
|
|
|
|
throw new Error("Error in configuration file: Your `backend.name` must be a string. Check your config.yml file.");
|
|
|
|
}
|
|
|
|
if (!get(config, 'media_folder')) {
|
|
|
|
throw new Error("Error in configuration file: A `media_folder` wasn\'t found. Check your config.yml file.");
|
|
|
|
}
|
|
|
|
if (typeof config.media_folder !== 'string') {
|
|
|
|
throw new Error("Error in configuration file: Your `media_folder` must be a string. Check your config.yml file.");
|
|
|
|
}
|
|
|
|
if (!get(config, 'collections')) {
|
|
|
|
throw new Error("Error in configuration file: A `collections` wasn\'t found. Check your config.yml file.");
|
|
|
|
}
|
|
|
|
if (!Array.isArray(config.collections) || config.collections.length === 0 || !config.collections[0]) {
|
|
|
|
throw new Error("Error in configuration file: Your `collections` must be an array with at least one element. Check your config.yml file.");
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
function parseConfig(data) {
|
|
|
|
const config = yaml.safeLoad(data);
|
2016-12-23 16:59:48 -02:00
|
|
|
if (typeof CMS_ENV === "string" && config[CMS_ENV]) {
|
2017-04-14 22:36:41 +01:00
|
|
|
Object.keys(config[CMS_ENV]).forEach((key) => {
|
|
|
|
config[key] = config[CMS_ENV][key];
|
|
|
|
});
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2016-02-25 00:45:56 -08:00
|
|
|
export function configLoaded(config) {
|
|
|
|
return {
|
2016-02-25 12:31:21 -08:00
|
|
|
type: CONFIG_SUCCESS,
|
2016-11-11 12:17:01 +01:00
|
|
|
payload: config,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function configLoading() {
|
|
|
|
return {
|
2016-11-11 12:17:01 +01:00
|
|
|
type: CONFIG_REQUEST,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function configFailed(err) {
|
|
|
|
return {
|
2016-02-25 12:31:21 -08:00
|
|
|
type: CONFIG_FAILURE,
|
2016-12-23 16:59:48 -02:00
|
|
|
error: "Error loading config",
|
2016-11-11 12:17:01 +01:00
|
|
|
payload: err,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
export function configDidLoad(config) {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(configLoaded(config));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
export function loadConfig() {
|
2016-02-25 00:45:56 -08:00
|
|
|
if (window.CMS_CONFIG) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return configDidLoad(window.CMS_CONFIG);
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
2016-11-11 12:17:01 +01:00
|
|
|
return (dispatch) => {
|
2016-02-25 00:45:56 -08:00
|
|
|
dispatch(configLoading());
|
|
|
|
|
2017-04-14 17:12:13 +01:00
|
|
|
fetch("config.yml", { credentials: 'same-origin' })
|
|
|
|
.then((response) => {
|
2016-02-25 00:45:56 -08:00
|
|
|
if (response.status !== 200) {
|
2016-11-11 12:17:01 +01:00
|
|
|
throw new Error(`Failed to load config.yml (${ response.status })`);
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
2017-04-14 17:12:13 +01:00
|
|
|
return response.text();
|
|
|
|
})
|
|
|
|
.then(parseConfig)
|
2017-04-14 22:36:41 +01:00
|
|
|
.then(validateConfig)
|
2017-04-14 17:12:13 +01:00
|
|
|
.then(applyDefaults)
|
|
|
|
.then((config) => {
|
|
|
|
dispatch(configDidLoad(config));
|
|
|
|
dispatch(authenticateUser());
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2016-02-25 00:45:56 -08:00
|
|
|
dispatch(configFailed(err));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|