Skip to content

Commit

Permalink
Throw config error if unsupported format.
Browse files Browse the repository at this point in the history
  • Loading branch information
tech4him1 committed Dec 1, 2017
1 parent 43d742e commit 2ecc535
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/formats/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import tomlFormatter from './toml';
import jsonFormatter from './json';
import FrontmatterFormatter from './frontmatter';

export const supportedFormats = [
'markdown',
'yaml',
'toml',
'json',
'html',
];

export const formatToExtension = format => ({
markdown: 'md',
yaml: 'yml',
Expand All @@ -29,8 +37,11 @@ function formatByName(name) {
yaml: yamlFormatter,
toml: tomlFormatter,
json: jsonFormatter,
md: FrontmatterFormatter,
markdown: FrontmatterFormatter,
html: FrontmatterFormatter,
frontmatter: FrontmatterFormatter,
}[name] || FrontmatterFormatter;
}[name];
}

export function resolveFormat(collectionOrEntity, entry) {
Expand All @@ -46,7 +57,4 @@ export function resolveFormat(collectionOrEntity, entry) {
const fileExtension = filePath.split('.').pop();
return formatByExtension(fileExtension);
}

// If no format is specified and it cannot be inferred, return the default.
return formatByName();
}
7 changes: 5 additions & 2 deletions src/reducers/collections.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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 { formatToExtension, supportedFormats } from '../formats/formats';

const collections = (state = null, action) => {
const configCollections = action.payload && action.payload.collections;
Expand All @@ -31,6 +31,9 @@ function validateCollection(configCollection) {
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(',') }`);
}
}

const selectors = {
Expand Down

0 comments on commit 2ecc535

Please sign in to comment.