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 Nov 18, 2017
1 parent dab5872 commit 07e9b5d
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 @@ -28,8 +36,11 @@ function formatByName(name) {
yml: yamlFormatter,
yaml: yamlFormatter,
toml: tomlFormatter,
md: FrontmatterFormatter,
markdown: FrontmatterFormatter,
html: FrontmatterFormatter,
frontmatter: FrontmatterFormatter,
}[name] || FrontmatterFormatter;
}[name];
}

export function resolveFormat(collectionOrEntity, entry) {
Expand All @@ -45,7 +56,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 07e9b5d

Please sign in to comment.