Merge pull request #439 from netlify/empty-test-repo

allow test repo to be used without window.testRepo
This commit is contained in:
Shawn Erquhart 2017-06-01 18:30:19 -04:00 committed by GitHub
commit 0885f84db9
2 changed files with 10 additions and 7 deletions

View File

@ -1,3 +1,4 @@
import { attempt, isError } from 'lodash';
import TestRepoBackend from "./test-repo/implementation"; import TestRepoBackend from "./test-repo/implementation";
import GitHubBackend from "./github/implementation"; import GitHubBackend from "./github/implementation";
import NetlifyAuthBackend from "./netlify-auth/implementation"; import NetlifyAuthBackend from "./netlify-auth/implementation";
@ -91,7 +92,7 @@ class Backend {
collection.get("name"), collection.get("name"),
selectEntrySlug(collection, loadedEntry.file.path), selectEntrySlug(collection, loadedEntry.file.path),
loadedEntry.file.path, loadedEntry.file.path,
{ raw: loadedEntry.data, label: loadedEntry.file.label } { raw: loadedEntry.data || '', label: loadedEntry.file.label }
)) ))
)) ))
.then(entries => ( .then(entries => (
@ -121,8 +122,9 @@ class Backend {
entryWithFormat(collectionOrEntity) { entryWithFormat(collectionOrEntity) {
return (entry) => { return (entry) => {
const format = resolveFormat(collectionOrEntity, entry); const format = resolveFormat(collectionOrEntity, entry);
if (entry && entry.raw) { if (entry && entry.raw !== undefined) {
return Object.assign(entry, { data: format && format.fromFile(entry.raw) }); const data = (format && attempt(format.fromFile.bind(null, entry.raw))) || {};
return Object.assign(entry, { data: isError(data) ? {} : data });
} }
return format.fromFile(entry); return format.fromFile(entry);
}; };

View File

@ -1,13 +1,15 @@
import AuthenticationPage from './AuthenticationPage'; import AuthenticationPage from './AuthenticationPage';
import { fileExtension } from '../../lib/pathHelper' import { fileExtension } from '../../lib/pathHelper'
window.repoFiles = window.repoFiles || {};
function getFile(path) { function getFile(path) {
const segments = path.split('/'); const segments = path.split('/');
let obj = window.repoFiles; let obj = window.repoFiles;
while (obj && segments.length) { while (obj && segments.length) {
obj = obj[segments.shift()]; obj = obj[segments.shift()];
} }
return obj; return obj || {};
} }
function nameFromEmail(email) { function nameFromEmail(email) {
@ -22,9 +24,6 @@ function nameFromEmail(email) {
export default class TestRepo { export default class TestRepo {
constructor(config) { constructor(config) {
this.config = config; this.config = config;
if (window.repoFiles == null) {
throw 'The TestRepo backend needs a "window.repoFiles" object.';
}
} }
setUser() {} setUser() {}
@ -84,6 +83,8 @@ export default class TestRepo {
const newEntry = options.newEntry || false; const newEntry = options.newEntry || false;
const folder = entry.path.substring(0, entry.path.lastIndexOf('/')); const folder = entry.path.substring(0, entry.path.lastIndexOf('/'));
const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1); 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) { if (newEntry) {
window.repoFiles[folder][fileName] = { content: entry.raw }; window.repoFiles[folder][fileName] = { content: entry.raw };
} else { } else {