diff --git a/src/backends/backend.js b/src/backends/backend.js index 3db54493..70824268 100644 --- a/src/backends/backend.js +++ b/src/backends/backend.js @@ -1,3 +1,4 @@ +import { attempt, isError } from 'lodash'; import TestRepoBackend from "./test-repo/implementation"; import GitHubBackend from "./github/implementation"; import NetlifyAuthBackend from "./netlify-auth/implementation"; @@ -91,7 +92,7 @@ class Backend { collection.get("name"), selectEntrySlug(collection, loadedEntry.file.path), loadedEntry.file.path, - { raw: loadedEntry.data, label: loadedEntry.file.label } + { raw: loadedEntry.data || '', label: loadedEntry.file.label } )) )) .then(entries => ( @@ -121,8 +122,9 @@ class Backend { entryWithFormat(collectionOrEntity) { return (entry) => { const format = resolveFormat(collectionOrEntity, entry); - if (entry && entry.raw) { - return Object.assign(entry, { data: format && format.fromFile(entry.raw) }); + if (entry && entry.raw !== undefined) { + const data = (format && attempt(format.fromFile.bind(null, entry.raw))) || {}; + return Object.assign(entry, { data: isError(data) ? {} : data }); } return format.fromFile(entry); }; diff --git a/src/backends/test-repo/implementation.js b/src/backends/test-repo/implementation.js index 0c3ab1a5..dfea59a2 100644 --- a/src/backends/test-repo/implementation.js +++ b/src/backends/test-repo/implementation.js @@ -1,13 +1,15 @@ import AuthenticationPage from './AuthenticationPage'; import { fileExtension } from '../../lib/pathHelper' +window.repoFiles = window.repoFiles || {}; + function getFile(path) { const segments = path.split('/'); let obj = window.repoFiles; while (obj && segments.length) { obj = obj[segments.shift()]; } - return obj; + return obj || {}; } function nameFromEmail(email) { @@ -22,9 +24,6 @@ function nameFromEmail(email) { export default class TestRepo { constructor(config) { this.config = config; - if (window.repoFiles == null) { - throw 'The TestRepo backend needs a "window.repoFiles" object.'; - } } setUser() {} @@ -84,6 +83,8 @@ export default class TestRepo { const newEntry = options.newEntry || false; const folder = entry.path.substring(0, entry.path.lastIndexOf('/')); const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1); + window.repoFiles[folder] = window.repoFiles[folder] || {}; + window.repoFiles[folder][fileName] = window.repoFiles[folder][fileName] || {}; if (newEntry) { window.repoFiles[folder][fileName] = { content: entry.raw }; } else {