chore: add code formatting and linting (#952)

This commit is contained in:
Caleb
2018-08-07 14:46:54 -06:00
committed by Shawn Erquhart
parent 32e0a9b2b5
commit f801b19221
265 changed files with 5988 additions and 4481 deletions

View File

@ -6,8 +6,8 @@ describe('config', () => {
* log test failures and associated errors as expected.
*/
beforeEach(() => {
spyOn(console, 'error')
})
jest.spyOn(console, 'error');
});
describe('validateConfig', () => {
it('should not throw if no errors', () => {
@ -15,12 +15,14 @@ describe('config', () => {
foo: 'bar',
backend: { name: 'bar' },
media_folder: 'baz',
collections: [{
name: 'posts',
label: 'Posts',
folder: '_posts',
fields: [{ name: 'title', label: 'title', widget: 'string' }],
}],
collections: [
{
name: 'posts',
label: 'Posts',
folder: '_posts',
fields: [{ name: 'title', label: 'title', widget: 'string' }],
},
],
};
expect(() => {
validateConfig(config);
@ -41,7 +43,7 @@ describe('config', () => {
it('should throw if backend name is not a string in config', () => {
expect(() => {
validateConfig({ foo: 'bar', backend: { name: { } } });
validateConfig({ foo: 'bar', backend: { name: {} } });
}).toThrowError("'backend.name' should be string");
});
@ -65,20 +67,35 @@ describe('config', () => {
it('should throw if collections not an array in config', () => {
expect(() => {
validateConfig({ foo: 'bar', backend: { name: 'bar' }, media_folder: 'baz', collections: {} });
validateConfig({
foo: 'bar',
backend: { name: 'bar' },
media_folder: 'baz',
collections: {},
});
}).toThrowError("'collections' should be array");
});
it('should throw if collections is an empty array in config', () => {
expect(() => {
validateConfig({ foo: 'bar', backend: { name: 'bar' }, media_folder: 'baz', collections: [] });
validateConfig({
foo: 'bar',
backend: { name: 'bar' },
media_folder: 'baz',
collections: [],
});
}).toThrowError("'collections' should NOT have less than 1 items");
});
it('should throw if collections is an array with a single null element in config', () => {
expect(() => {
validateConfig({ foo: 'bar', backend: { name: 'bar' }, media_folder: 'baz', collections: [null] });
validateConfig({
foo: 'bar',
backend: { name: 'bar' },
media_folder: 'baz',
collections: [null],
});
}).toThrowError("'collections[0]' should be object");
});
});
});
});

View File

@ -1,28 +1,24 @@
import AJV from 'ajv';
import ajvErrors from 'ajv-errors';
import {
formatExtensions,
frontmatterFormats,
extensionFormatters,
} from "Formats/formats";
import { IDENTIFIER_FIELDS } from "Constants/fieldInference";
import { formatExtensions, frontmatterFormats, extensionFormatters } from 'Formats/formats';
import { IDENTIFIER_FIELDS } from 'Constants/fieldInference';
/**
* Config for fields in both file and folder collections.
*/
const fieldsConfig = {
type: "array",
type: 'array',
minItems: 1,
items: {
// ------- Each field: -------
type: "object",
type: 'object',
properties: {
name: { type: "string" },
label: { type: "string" },
widget: { type: "string" },
required: { type: "boolean" },
name: { type: 'string' },
label: { type: 'string' },
widget: { type: 'string' },
required: { type: 'boolean' },
},
required: ["name"],
required: ['name'],
},
};
@ -32,72 +28,72 @@ const fieldsConfig = {
* where the imports get resolved asyncronously.
*/
const getConfigSchema = () => ({
type: "object",
type: 'object',
properties: {
backend: {
type: "object",
properties: { name: { type: "string", examples: ["test-repo"] } },
required: ["name"],
type: 'object',
properties: { name: { type: 'string', examples: ['test-repo'] } },
required: ['name'],
},
display_url: { type: "string", examples: ["https://example.com"] },
media_folder: { type: "string", examples: ["assets/uploads"] },
public_folder: { type: "string", examples: ["/uploads"] },
display_url: { type: 'string', examples: ['https://example.com'] },
media_folder: { type: 'string', examples: ['assets/uploads'] },
public_folder: { type: 'string', examples: ['/uploads'] },
publish_mode: {
type: "string",
enum: ["editorial_workflow"],
examples: ["editorial_workflow"],
type: 'string',
enum: ['editorial_workflow'],
examples: ['editorial_workflow'],
},
slug: {
type: "object",
type: 'object',
properties: {
encoding: { type: "string", enum: ["unicode", "ascii"] },
clean_accents: { type: "boolean" },
encoding: { type: 'string', enum: ['unicode', 'ascii'] },
clean_accents: { type: 'boolean' },
},
},
collections: {
type: "array",
type: 'array',
minItems: 1,
items: {
// ------- Each collection: -------
type: "object",
type: 'object',
properties: {
name: { type: "string" },
label: { type: "string" },
label_singular: { type: "string" },
description: { type: "string" },
folder: { type: "string" },
name: { type: 'string' },
label: { type: 'string' },
label_singular: { type: 'string' },
description: { type: 'string' },
folder: { type: 'string' },
files: {
type: "array",
type: 'array',
items: {
// ------- Each file: -------
type: "object",
type: 'object',
properties: {
name: { type: "string" },
label: { type: "string" },
label_singular: { type: "string" },
description: { type: "string" },
file: { type: "string" },
name: { type: 'string' },
label: { type: 'string' },
label_singular: { type: 'string' },
description: { type: 'string' },
file: { type: 'string' },
fields: fieldsConfig,
},
required: ["name", "label", "file", "fields"],
required: ['name', 'label', 'file', 'fields'],
},
},
slug: { type: "string" },
create: { type: "boolean" },
slug: { type: 'string' },
create: { type: 'boolean' },
editor: {
type: "object",
type: 'object',
properties: {
preview: { type: "boolean" },
preview: { type: 'boolean' },
},
},
format: { type: "string", enum: Object.keys(formatExtensions) },
extension: { type: "string" },
frontmatter_delimiter: { type: "string" },
format: { type: 'string', enum: Object.keys(formatExtensions) },
extension: { type: 'string' },
frontmatter_delimiter: { type: 'string' },
fields: fieldsConfig,
},
required: ["name", "label"],
oneOf: [{ required: ["files"] }, { required: ["folder", "fields"] }],
if: { required: ["extension"] },
required: ['name', 'label'],
oneOf: [{ required: ['files'] }, { required: ['folder', 'fields'] }],
if: { required: ['extension'] },
then: {
// Cannot infer format from extension.
if: {
@ -105,14 +101,14 @@ const getConfigSchema = () => ({
extension: { enum: Object.keys(extensionFormatters) },
},
},
else: { required: ["format"] },
else: { required: ['format'] },
},
dependencies: {
frontmatter_delimiter: {
properties: {
format: { enum: frontmatterFormats },
},
required: ["format"],
required: ['format'],
},
folder: {
errorMessage: {
@ -132,7 +128,7 @@ const getConfigSchema = () => ({
},
},
},
required: ["backend", "media_folder", "collections"],
required: ['backend', 'media_folder', 'collections'],
});
class ConfigError extends Error {
@ -141,13 +137,13 @@ class ConfigError extends Error {
.map(({ message, dataPath }) => {
const dotPath = dataPath
.slice(1)
.split("/")
.split('/')
.map(seg => (seg.match(/^\d+$/) ? `[${seg}]` : `.${seg}`))
.join("")
.join('')
.slice(1);
return `${dotPath ? `'${dotPath}'` : "config"} ${message}`;
return `${dotPath ? `'${dotPath}'` : 'config'} ${message}`;
})
.join("\n");
.join('\n');
super(message, ...args);
this.errors = errors;
@ -161,7 +157,7 @@ class ConfigError extends Error {
/**
* `validateConfig` is a pure function. It does not mutate
* the config that is passed in.
* the config that is passed in.
*/
export function validateConfig(config) {
const ajv = new AJV({ allErrors: true, jsonPointers: true });

View File

@ -7,7 +7,7 @@ export const INFERABLE_FIELDS = {
type: 'string',
secondaryTypes: [],
synonyms: ['title', 'name', 'label', 'headline', 'header'],
defaultPreview: value => <h1>{ value }</h1>, // eslint-disable-line react/display-name
defaultPreview: value => <h1>{value}</h1>, // eslint-disable-line react/display-name
fallbackToFirstField: true,
showError: true,
},
@ -15,7 +15,7 @@ export const INFERABLE_FIELDS = {
type: 'string',
secondaryTypes: [],
synonyms: ['short_title', 'shortTitle', 'short'],
defaultPreview: value => <h2>{ value }</h2>, // eslint-disable-line react/display-name
defaultPreview: value => <h2>{value}</h2>, // eslint-disable-line react/display-name
fallbackToFirstField: false,
showError: false,
},
@ -23,14 +23,26 @@ export const INFERABLE_FIELDS = {
type: 'string',
secondaryTypes: [],
synonyms: ['author', 'name', 'by', 'byline', 'owner'],
defaultPreview: value => <strong>{ value }</strong>, // eslint-disable-line react/display-name
defaultPreview: value => <strong>{value}</strong>, // eslint-disable-line react/display-name
fallbackToFirstField: false,
showError: false,
},
description: {
type: 'string',
secondaryTypes: ['text', 'markdown'],
synonyms: ['shortDescription', 'short_description', 'shortdescription', 'description', 'intro', 'introduction', 'brief', 'content', 'biography', 'bio', 'summary'],
synonyms: [
'shortDescription',
'short_description',
'shortdescription',
'description',
'intro',
'introduction',
'brief',
'content',
'biography',
'bio',
'summary',
],
defaultPreview: value => value,
fallbackToFirstField: false,
showError: false,