2016-06-21 20:24:52 -07:00
|
|
|
import TestRepoBackend from './test-repo/implementation';
|
|
|
|
import GitHubBackend from './github/implementation';
|
2016-09-04 19:55:14 +02:00
|
|
|
import NetlifyGitBackend from './netlify-git/implementation';
|
2016-02-25 20:40:35 -08:00
|
|
|
import { resolveFormat } from '../formats/formats';
|
2016-08-24 21:36:44 -03:00
|
|
|
import { createEntry } from '../valueObjects/Entry';
|
2016-02-25 12:31:21 -08:00
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
class LocalStorageAuthStore {
|
|
|
|
storageKey = 'nf-cms-user';
|
|
|
|
|
|
|
|
retrieve() {
|
|
|
|
const data = window.localStorage.getItem(this.storageKey);
|
|
|
|
return data && JSON.parse(data);
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
store(userData) {
|
|
|
|
window.localStorage.setItem(this.storageKey, JSON.stringify(userData));
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Backend {
|
|
|
|
constructor(implementation, authStore = null) {
|
|
|
|
this.implementation = implementation;
|
|
|
|
this.authStore = authStore;
|
2016-09-05 12:12:38 -03:00
|
|
|
if (this.implementation === null) {
|
2016-02-25 12:31:21 -08:00
|
|
|
throw 'Cannot instantiate a Backend with no implementation';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentUser() {
|
|
|
|
if (this.user) { return this.user; }
|
2016-06-05 01:52:18 -07:00
|
|
|
const stored = this.authStore && this.authStore.retrieve();
|
|
|
|
if (stored) {
|
|
|
|
this.implementation.setUser(stored);
|
|
|
|
return stored;
|
|
|
|
}
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
authComponent() {
|
|
|
|
return this.implementation.authComponent();
|
|
|
|
}
|
|
|
|
|
2016-02-25 20:40:35 -08:00
|
|
|
authenticate(credentials) {
|
2016-05-30 16:55:32 -07:00
|
|
|
return this.implementation.authenticate(credentials).then((user) => {
|
|
|
|
if (this.authStore) { this.authStore.store(user); }
|
|
|
|
return user;
|
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
entries(collection, page, perPage) {
|
|
|
|
return this.implementation.entries(collection, page, perPage).then((response) => {
|
|
|
|
return {
|
|
|
|
pagination: response.pagination,
|
|
|
|
entries: response.entries.map(this.entryWithFormat(collection))
|
|
|
|
};
|
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
entry(collection, slug) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return this.implementation.entry(collection, slug).then(this.entryWithFormat(collection));
|
|
|
|
}
|
|
|
|
|
2016-08-24 21:36:44 -03:00
|
|
|
newEntry(collection) {
|
|
|
|
const newEntry = createEntry();
|
|
|
|
return this.entryWithFormat(collection)(newEntry);
|
|
|
|
}
|
|
|
|
|
2016-09-06 13:04:17 -03:00
|
|
|
entryWithFormat(collectionOrEntity) {
|
2016-06-05 01:52:18 -07:00
|
|
|
return (entry) => {
|
2016-09-06 13:04:17 -03:00
|
|
|
const format = resolveFormat(collectionOrEntity, entry);
|
2016-06-05 01:52:18 -07:00
|
|
|
if (entry && entry.raw) {
|
|
|
|
entry.data = format && format.fromFile(entry.raw);
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
};
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
2016-06-06 21:53:22 -03:00
|
|
|
|
2016-09-06 13:04:17 -03:00
|
|
|
unpublishedEntries(page, perPage) {
|
|
|
|
return this.implementation.unpublishedEntries(page, perPage).then((response) => {
|
|
|
|
return {
|
|
|
|
pagination: response.pagination,
|
|
|
|
entries: response.entries.map(this.entryWithFormat('editorialWorkflow'))
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-25 16:11:00 -03:00
|
|
|
slugFormatter(template, entry) {
|
|
|
|
var date = new Date();
|
|
|
|
return template.replace(/\{\{([^\}]+)\}\}/g, function(_, name) {
|
|
|
|
switch (name) {
|
|
|
|
case 'year':
|
|
|
|
return date.getFullYear();
|
|
|
|
case 'month':
|
|
|
|
return ('0' + (date.getMonth() + 1)).slice(-2);
|
|
|
|
case 'day':
|
|
|
|
return ('0' + date.getDate()).slice(-2);
|
|
|
|
case 'slug':
|
|
|
|
return entry.getIn(['data', 'title']).trim().toLowerCase().replace(/[^a-z0-9\.\-\_]+/gi, '-');
|
|
|
|
default:
|
|
|
|
return entry.getIn(['data', name]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:09:04 -03:00
|
|
|
persistEntry(config, collection, entryDraft, MediaFiles) {
|
2016-08-25 16:11:00 -03:00
|
|
|
const newEntry = entryDraft.getIn(['entry', 'newRecord']) || false;
|
2016-09-05 12:12:38 -03:00
|
|
|
|
|
|
|
const parsedData = {
|
|
|
|
title: entryDraft.getIn(['entry', 'data', 'title'], 'No Title'),
|
|
|
|
description: entryDraft.getIn(['entry', 'data', 'description'], 'No Description'),
|
|
|
|
};
|
|
|
|
|
2016-08-29 17:09:04 -03:00
|
|
|
const entryData = entryDraft.getIn(['entry', 'data']).toJS();
|
2016-08-25 16:11:00 -03:00
|
|
|
let entryObj;
|
|
|
|
if (newEntry) {
|
|
|
|
const slug = this.slugFormatter(collection.get('slug'), entryDraft.get('entry'));
|
|
|
|
entryObj = {
|
|
|
|
path: `${collection.get('folder')}/${slug}.md`,
|
|
|
|
slug: slug,
|
|
|
|
raw: this.entryToRaw(collection, entryData)
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
entryObj = {
|
|
|
|
path: entryDraft.getIn(['entry', 'path']),
|
|
|
|
slug: entryDraft.getIn(['entry', 'slug']),
|
|
|
|
raw: this.entryToRaw(collection, entryData)
|
|
|
|
};
|
|
|
|
}
|
2016-07-19 17:11:22 -03:00
|
|
|
|
2016-08-25 16:11:00 -03:00
|
|
|
const commitMessage = (newEntry ? 'Created ' : 'Updated ') +
|
2016-07-19 17:11:22 -03:00
|
|
|
collection.get('label') + ' “' +
|
|
|
|
entryDraft.getIn(['entry', 'data', 'title']) + '”';
|
|
|
|
|
2016-09-06 13:04:17 -03:00
|
|
|
const mode = config.get('publish_mode');
|
2016-08-30 19:06:20 -03:00
|
|
|
|
|
|
|
const collectionName = collection.get('name');
|
|
|
|
|
2016-09-05 12:12:38 -03:00
|
|
|
return this.implementation.persistEntry(entryObj, MediaFiles, {
|
|
|
|
newEntry, parsedData, commitMessage, collectionName, mode
|
|
|
|
});
|
2016-06-06 21:53:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
entryToRaw(collection, entry) {
|
|
|
|
const format = resolveFormat(collection, entry);
|
|
|
|
return format && format.toFile(entry);
|
|
|
|
}
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
|
|
|
|
2016-05-30 16:55:32 -07:00
|
|
|
export function resolveBackend(config) {
|
|
|
|
const name = config.getIn(['backend', 'name']);
|
|
|
|
if (name == null) {
|
|
|
|
throw 'No backend defined in configuration';
|
|
|
|
}
|
|
|
|
|
|
|
|
const authStore = new LocalStorageAuthStore();
|
|
|
|
|
|
|
|
switch (name) {
|
|
|
|
case 'test-repo':
|
|
|
|
return new Backend(new TestRepoBackend(config), authStore);
|
2016-06-05 01:52:18 -07:00
|
|
|
case 'github':
|
|
|
|
return new Backend(new GitHubBackend(config), authStore);
|
2016-09-04 19:55:14 +02:00
|
|
|
case 'netlify-git':
|
|
|
|
return new Backend(new NetlifyGitBackend(config), authStore);
|
2016-05-30 16:55:32 -07:00
|
|
|
default:
|
|
|
|
throw `Backend not found: ${name}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
export const currentBackend = (function() {
|
|
|
|
let backend = null;
|
|
|
|
|
|
|
|
return (config) => {
|
|
|
|
if (backend) { return backend; }
|
|
|
|
if (config.get('backend')) {
|
|
|
|
return backend = resolveBackend(config);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|