2018-08-07 14:46:54 -06:00
|
|
|
import yaml from 'js-yaml';
|
|
|
|
import { Map, fromJS } from 'immutable';
|
2020-02-05 17:56:11 +02:00
|
|
|
import { trimStart, get, isPlainObject } from 'lodash';
|
2018-08-07 14:46:54 -06:00
|
|
|
import { authenticateUser } from 'Actions/auth';
|
|
|
|
import * as publishModes from 'Constants/publishModes';
|
2018-08-07 10:27:15 -06:00
|
|
|
import { validateConfig } from 'Constants/configSchema';
|
2020-04-01 06:13:27 +03:00
|
|
|
import { selectDefaultSortableFields } from '../reducers/collections';
|
|
|
|
import { currentBackend } from 'coreSrc/backend';
|
2016-02-25 00:45:56 -08:00
|
|
|
|
2018-08-07 14:46:54 -06:00
|
|
|
export const CONFIG_REQUEST = 'CONFIG_REQUEST';
|
|
|
|
export const CONFIG_SUCCESS = 'CONFIG_SUCCESS';
|
|
|
|
export const CONFIG_FAILURE = 'CONFIG_FAILURE';
|
|
|
|
export const CONFIG_MERGE = 'CONFIG_MERGE';
|
2018-04-10 16:48:04 -04:00
|
|
|
|
|
|
|
const getConfigUrl = () => {
|
|
|
|
const validTypes = { 'text/yaml': 'yaml', 'application/x-yaml': 'yaml' };
|
|
|
|
const configLinkEl = document.querySelector('link[rel="cms-config-url"]');
|
|
|
|
const isValidLink = configLinkEl && validTypes[configLinkEl.type] && get(configLinkEl, 'href');
|
|
|
|
if (isValidLink) {
|
|
|
|
const link = get(configLinkEl, 'href');
|
|
|
|
console.log(`Using config file path: "${link}"`);
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
return 'config.yml';
|
2018-08-07 14:46:54 -06:00
|
|
|
};
|
2018-04-10 16:48:04 -04:00
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
const defaults = {
|
|
|
|
publish_mode: publishModes.SIMPLE,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function applyDefaults(config) {
|
2018-02-28 15:45:16 -05:00
|
|
|
return Map(defaults)
|
|
|
|
.mergeDeep(config)
|
|
|
|
.withMutations(map => {
|
2019-03-01 09:45:23 -05:00
|
|
|
// Use `site_url` as default `display_url`.
|
2019-02-08 12:26:59 -05:00
|
|
|
if (!map.get('display_url') && map.get('site_url')) {
|
|
|
|
map.set('display_url', map.get('site_url'));
|
|
|
|
}
|
2019-03-01 09:45:23 -05:00
|
|
|
|
|
|
|
// Use media_folder as default public_folder.
|
2018-02-28 15:45:16 -05:00
|
|
|
const defaultPublicFolder = `/${trimStart(map.get('media_folder'), '/')}`;
|
2020-04-01 13:16:30 +03:00
|
|
|
if (!map.has('public_folder')) {
|
2018-02-28 15:45:16 -05:00
|
|
|
map.set('public_folder', defaultPublicFolder);
|
|
|
|
}
|
2019-03-01 09:45:23 -05:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
// default values for the slug config
|
|
|
|
if (!map.getIn(['slug', 'encoding'])) {
|
|
|
|
map.setIn(['slug', 'encoding'], 'unicode');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!map.getIn(['slug', 'clean_accents'])) {
|
|
|
|
map.setIn(['slug', 'clean_accents'], false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!map.getIn(['slug', 'sanitize_replacement'])) {
|
|
|
|
map.setIn(['slug', 'sanitize_replacement'], '-');
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:45:23 -05:00
|
|
|
// Strip leading slash from collection folders and files
|
|
|
|
map.set(
|
|
|
|
'collections',
|
|
|
|
map.get('collections').map(collection => {
|
2020-03-23 12:01:37 +02:00
|
|
|
if (!collection.has('publish')) {
|
|
|
|
collection = collection.set('publish', true);
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:45:23 -05:00
|
|
|
const folder = collection.get('folder');
|
|
|
|
if (folder) {
|
2019-12-18 18:16:02 +02:00
|
|
|
if (collection.has('path') && !collection.has('media_folder')) {
|
|
|
|
// default value for media folder when using the path config
|
|
|
|
collection = collection.set('media_folder', '');
|
|
|
|
}
|
2020-01-14 20:02:53 +02:00
|
|
|
if (collection.has('media_folder') && !collection.has('public_folder')) {
|
|
|
|
collection = collection.set('public_folder', collection.get('media_folder'));
|
|
|
|
}
|
2020-04-01 06:13:27 +03:00
|
|
|
collection = collection.set('folder', trimStart(folder, '/'));
|
2019-03-01 09:45:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const files = collection.get('files');
|
|
|
|
if (files) {
|
2020-04-01 06:13:27 +03:00
|
|
|
collection = collection.set(
|
2019-03-01 09:45:23 -05:00
|
|
|
'files',
|
|
|
|
files.map(file => {
|
|
|
|
return file.set('file', trimStart(file.get('file'), '/'));
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2020-04-01 06:13:27 +03:00
|
|
|
|
|
|
|
if (!collection.has('sortableFields')) {
|
|
|
|
const backend = currentBackend(config);
|
|
|
|
const defaultSortable = selectDefaultSortableFields(collection, backend);
|
|
|
|
collection = collection.set('sortableFields', fromJS(defaultSortable));
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
2019-03-01 09:45:23 -05:00
|
|
|
}),
|
|
|
|
);
|
2018-02-28 15:45:16 -05:00
|
|
|
});
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 15:45:16 -05:00
|
|
|
function mergePreloadedConfig(preloadedConfig, loadedConfig) {
|
|
|
|
const map = fromJS(loadedConfig) || Map();
|
|
|
|
return preloadedConfig ? preloadedConfig.mergeDeep(map) : map;
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
function parseConfig(data) {
|
|
|
|
const config = yaml.safeLoad(data);
|
2018-08-07 14:46:54 -06:00
|
|
|
if (typeof CMS_ENV === 'string' && config[CMS_ENV]) {
|
|
|
|
Object.keys(config[CMS_ENV]).forEach(key => {
|
2017-04-14 22:36:41 +01:00
|
|
|
config[key] = config[CMS_ENV][key];
|
|
|
|
});
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:25:46 -07:00
|
|
|
async function getConfig(file, isPreloaded) {
|
2018-10-09 13:53:12 -04:00
|
|
|
const response = await fetch(file, { credentials: 'same-origin' }).catch(err => err);
|
|
|
|
if (response instanceof Error || response.status !== 200) {
|
2018-03-28 13:25:46 -07:00
|
|
|
if (isPreloaded) return parseConfig('');
|
2018-10-09 13:53:12 -04:00
|
|
|
throw new Error(`Failed to load config.yml (${response.status || response})`);
|
2018-03-28 13:25:46 -07:00
|
|
|
}
|
|
|
|
const contentType = response.headers.get('Content-Type') || 'Not-Found';
|
|
|
|
const isYaml = contentType.indexOf('yaml') !== -1;
|
|
|
|
if (!isYaml) {
|
2018-08-07 14:46:54 -06:00
|
|
|
console.log(`Response for ${file} was not yaml. (Content-Type: ${contentType})`);
|
2018-03-28 13:25:46 -07:00
|
|
|
if (isPreloaded) return parseConfig('');
|
|
|
|
}
|
|
|
|
return parseConfig(await response.text());
|
|
|
|
}
|
|
|
|
|
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,
|
2018-08-07 14:46:54 -06: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) {
|
2018-08-07 14:46:54 -06:00
|
|
|
return dispatch => {
|
2016-06-06 21:53:22 -03:00
|
|
|
dispatch(configLoaded(config));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-28 15:45:16 -05:00
|
|
|
export function mergeConfig(config) {
|
|
|
|
return { type: CONFIG_MERGE, payload: config };
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:56:11 +02:00
|
|
|
export async function detectProxyServer(localBackend) {
|
|
|
|
if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') {
|
|
|
|
let proxyUrl;
|
|
|
|
if (localBackend === true) {
|
|
|
|
proxyUrl = 'http://localhost:8081/api/v1';
|
|
|
|
} else if (isPlainObject(localBackend)) {
|
|
|
|
proxyUrl = localBackend.url;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
console.log(`Looking for Netlify CMS Proxy Server at '${proxyUrl}'`);
|
2020-02-10 18:07:52 +02:00
|
|
|
const { repo, publish_modes, type } = await fetch(`${proxyUrl}`, {
|
2020-02-05 17:56:11 +02:00
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
body: JSON.stringify({ action: 'info' }),
|
|
|
|
}).then(res => res.json());
|
2020-02-10 18:07:52 +02:00
|
|
|
if (typeof repo === 'string' && Array.isArray(publish_modes) && typeof type === 'string') {
|
2020-02-05 17:56:11 +02:00
|
|
|
console.log(`Detected Netlify CMS Proxy Server at '${proxyUrl}' with repo: '${repo}'`);
|
2020-02-10 18:07:52 +02:00
|
|
|
return { proxyUrl, publish_modes, type };
|
2020-02-05 17:56:11 +02:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
console.log(`Netlify CMS Proxy Server not detected at '${proxyUrl}'`);
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 18:07:52 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function handleLocalBackend(mergedConfig) {
|
|
|
|
if (mergedConfig.has('local_backend')) {
|
|
|
|
const { proxyUrl, publish_modes, type } = await detectProxyServer(
|
|
|
|
mergedConfig.toJS().local_backend,
|
|
|
|
);
|
|
|
|
if (proxyUrl) {
|
|
|
|
mergedConfig = mergePreloadedConfig(mergedConfig, {
|
|
|
|
backend: { name: 'proxy', proxy_url: proxyUrl },
|
|
|
|
});
|
|
|
|
if (
|
|
|
|
mergedConfig.has('publish_mode') &&
|
|
|
|
!publish_modes.includes(mergedConfig.get('publish_mode'))
|
|
|
|
) {
|
|
|
|
const newPublishMode = publish_modes[0];
|
|
|
|
console.log(
|
|
|
|
`'${mergedConfig.get(
|
|
|
|
'publish_mode',
|
|
|
|
)}' is not supported by '${type}' backend, switching to '${newPublishMode}'`,
|
|
|
|
);
|
|
|
|
mergedConfig = mergePreloadedConfig(mergedConfig, {
|
|
|
|
publish_mode: newPublishMode,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mergedConfig;
|
2020-02-05 17:56:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
export function loadConfig() {
|
2016-02-25 00:45:56 -08:00
|
|
|
if (window.CMS_CONFIG) {
|
2018-02-28 15:45:16 -05:00
|
|
|
return configDidLoad(fromJS(window.CMS_CONFIG));
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
2018-02-28 15:45:16 -05:00
|
|
|
return async (dispatch, getState) => {
|
2016-02-25 00:45:56 -08:00
|
|
|
dispatch(configLoading());
|
|
|
|
|
2018-02-28 15:45:16 -05:00
|
|
|
try {
|
|
|
|
const preloadedConfig = getState().config;
|
2018-04-10 16:48:04 -04:00
|
|
|
const configUrl = getConfigUrl();
|
2020-02-05 17:56:11 +02:00
|
|
|
const isPreloaded = preloadedConfig && preloadedConfig.size > 1;
|
2019-02-03 14:48:40 -08:00
|
|
|
const loadedConfig =
|
|
|
|
preloadedConfig && preloadedConfig.get('load_config_file') === false
|
|
|
|
? {}
|
2020-02-05 17:56:11 +02:00
|
|
|
: await getConfig(configUrl, isPreloaded);
|
2018-02-28 15:45:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge any existing configuration so the result can be validated.
|
|
|
|
*/
|
2020-02-05 17:56:11 +02:00
|
|
|
let mergedConfig = mergePreloadedConfig(preloadedConfig, loadedConfig);
|
|
|
|
|
2018-08-07 10:27:15 -06:00
|
|
|
validateConfig(mergedConfig.toJS());
|
|
|
|
|
2020-02-10 18:07:52 +02:00
|
|
|
mergedConfig = await handleLocalBackend(mergedConfig);
|
2020-02-05 17:56:11 +02:00
|
|
|
|
2018-08-07 10:27:15 -06:00
|
|
|
const config = applyDefaults(mergedConfig);
|
2018-02-28 15:45:16 -05:00
|
|
|
|
2017-04-14 17:12:13 +01:00
|
|
|
dispatch(configDidLoad(config));
|
|
|
|
dispatch(authenticateUser());
|
2018-08-07 14:46:54 -06:00
|
|
|
} catch (err) {
|
2016-02-25 00:45:56 -08:00
|
|
|
dispatch(configFailed(err));
|
2018-08-07 14:46:54 -06:00
|
|
|
throw err;
|
2018-02-28 15:45:16 -05:00
|
|
|
}
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|