Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw on unsupported format. #831

Merged
merged 4 commits into from
Dec 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/formats/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ import tomlFormatter from './toml';
import jsonFormatter from './json';
import FrontmatterFormatter from './frontmatter';

export const supportedFormats = [
'yml',
'yaml',
'toml',
'json',
'frontmatter',
];

export const formatToExtension = format => ({
markdown: 'md',
yml: 'yml',
yaml: 'yml',
toml: 'toml',
json: 'json',
html: 'html',
frontmatter: 'md',
}[format]);

export function formatByExtension(extension) {
Expand All @@ -20,7 +28,7 @@ export function formatByExtension(extension) {
md: FrontmatterFormatter,
markdown: FrontmatterFormatter,
html: FrontmatterFormatter,
}[extension] || FrontmatterFormatter;
}[extension];
}

function formatByName(name) {
Expand All @@ -30,7 +38,7 @@ function formatByName(name) {
toml: tomlFormatter,
json: jsonFormatter,
frontmatter: FrontmatterFormatter,
}[name] || FrontmatterFormatter;
}[name];
}

export function resolveFormat(collectionOrEntity, entry) {
Expand All @@ -55,5 +63,5 @@ export function resolveFormat(collectionOrEntity, entry) {
}

// If no format is specified and it cannot be inferred, return the default.
return formatByName();
return formatByName('frontmatter');
}
23 changes: 18 additions & 5 deletions src/reducers/collections.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { OrderedMap, fromJS } from 'immutable';
import { has } from 'lodash';
import { has, get } from 'lodash';
import consoleError from '../lib/consoleError';
import { CONFIG_SUCCESS } from '../actions/config';
import { FILES, FOLDER } from '../constants/collectionTypes';
import { INFERABLE_FIELDS } from '../constants/fieldInference';
import { formatToExtension } from '../formats/formats';
import { formatByExtension, formatToExtension, supportedFormats } from '../formats/formats';

const collections = (state = null, action) => {
const configCollections = action.payload && action.payload.collections;
switch (action.type) {
case CONFIG_SUCCESS:
return OrderedMap().withMutations((map) => {
(configCollections || []).forEach((configCollection) => {
validateCollection(configCollection);
if (has(configCollection, 'folder')) {
configCollection.type = FOLDER; // eslint-disable-line no-param-reassign
} else if (has(configCollection, 'files')) {
configCollection.type = FILES; // eslint-disable-line no-param-reassign
} else {
throw new Error('Unknown collection type. Collections can be either Folder based or File based. Please verify your site configuration');
}
map.set(configCollection.name, fromJS(configCollection));
});
Expand All @@ -27,10 +26,24 @@ const collections = (state = null, action) => {
}
};

function validateCollection(configCollection) {
const collectionName = get(configCollection, 'name');
if (!has(configCollection, 'folder') && !has(configCollection, 'files')) {
throw new Error(`Unknown collection type for collection "${ collectionName }". Collections can be either Folder based or File based.`);
}
if (has(configCollection, 'format') && !supportedFormats.includes(get(configCollection, 'format'))) {
throw new Error(`Unknown collection format for collection "${ collectionName }". Supported formats are ${ supportedFormats.join(',') }`);
}
if (!has(configCollection, 'format') && has(configCollection, 'extension') && !formatByExtension(get(configCollection, 'extension'))) {
// Cannot infer format from extension.
throw new Error(`Please set a format for collection "${ collectionName }". Supported formats are ${ supportedFormats.join(',') }`);
}
}

const selectors = {
[FOLDER]: {
entryExtension(collection) {
return collection.get('extension') || formatToExtension(collection.get('format') || 'markdown');
return collection.get('extension') || formatToExtension(collection.get('format') || 'frontmatter');
},
fields(collection) {
return collection.get('fields');
Expand Down