refactor on slugformatter

This commit is contained in:
Cássio Zen 2016-10-27 13:12:18 -02:00
parent dabf6a1be1
commit baafe0b32f
5 changed files with 42 additions and 33 deletions

View File

@ -181,16 +181,10 @@ export function loadEntry(entry, collection, slug) {
const state = getState(); const state = getState();
const backend = currentBackend(state.config); const backend = currentBackend(state.config);
dispatch(entryLoading(collection, slug)); dispatch(entryLoading(collection, slug));
let getPromise; return backend.getEntry(collection, slug)
if (entry && entry.get('path') && entry.get('partial')) { .then(loadedEntry => (
getPromise = backend.getEntry(entry.get('collection'), entry.get('slug'), entry.get('path')); dispatch(entryLoaded(collection, loadedEntry))
} else { ));
getPromise = backend.lookupEntry(collection, slug);
}
return getPromise
.then((loadedEntry) => {
return dispatch(entryLoaded(collection, loadedEntry));
});
}; };
} }

View File

@ -86,8 +86,8 @@ class Backend {
} }
// We have the file path. Fetch and parse the file. // We have the file path. Fetch and parse the file.
getEntry(collection, slug, path) { getEntry(collection, slug) {
return this.implementation.getEntry(collection, slug, path) return this.implementation.getEntry(collection, slug, new Collection(collection).entryPath(slug))
.then(loadedEntry => this.entryWithFormat(collection, slug)(createEntry( .then(loadedEntry => this.entryWithFormat(collection, slug)(createEntry(
collection.get('name'), collection.get('name'),
slug, slug,
@ -97,10 +97,6 @@ class Backend {
); );
} }
lookupEntry(collection, slug) {
return this.getEntry(collection, slug, new Collection(collection).entryPath(slug));
}
newEntry(collection) { newEntry(collection) {
return createEntry(collection.get('name')); return createEntry(collection.get('name'));
} }
@ -197,11 +193,11 @@ export function resolveBackend(config) {
switch (name) { switch (name) {
case 'test-repo': case 'test-repo':
return new Backend(new TestRepoBackend(config, slugFormatter), authStore); return new Backend(new TestRepoBackend(config), authStore);
case 'github': case 'github':
return new Backend(new GitHubBackend(config, slugFormatter), authStore); return new Backend(new GitHubBackend(config), authStore);
case 'netlify-git': case 'netlify-git':
return new Backend(new NetlifyGitBackend(config, slugFormatter), authStore); return new Backend(new NetlifyGitBackend(config), authStore);
default: default:
throw new Error(`Backend not found: ${ name }`); throw new Error(`Backend not found: ${ name }`);
} }

View File

@ -32,7 +32,8 @@ export default class GitHub {
} }
entriesByFolder(collection) { entriesByFolder(collection) {
return this.api.listFiles(collection.get('folder')).then(files => this.entriesByFiles(collection, files)); return this.api.listFiles(collection.get('folder'))
.then(this.fetchFiles);
} }
entriesByFiles(collection) { entriesByFiles(collection) {
@ -40,26 +41,25 @@ export default class GitHub {
path: collectionFile.get('file'), path: collectionFile.get('file'),
label: collectionFile.get('label'), label: collectionFile.get('label'),
})); }));
return this.fetchFiles(files);
}
fetchFiles = (files) => {
const sem = semaphore(MAX_CONCURRENT_DOWNLOADS); const sem = semaphore(MAX_CONCURRENT_DOWNLOADS);
const promises = []; const promises = [];
files.forEach((file) => { files.forEach((file) => {
promises.push(new Promise((resolve, reject) => { promises.push(new Promise((resolve, reject) => (
return sem.take(() => this.api.readFile(file.path, file.sha).then((data) => { sem.take(() => this.api.readFile(file.path, file.sha).then((data) => {
resolve( resolve({ file, data });
{
file,
data,
}
);
sem.leave(); sem.leave();
}).catch((err) => { }).catch((err) => {
sem.leave(); sem.leave();
reject(err); reject(err);
})); }))
})); )));
}); });
return Promise.all(promises); return Promise.all(promises);
} };
// Fetches a single entry. // Fetches a single entry.
getEntry(collection, slug, path) { getEntry(collection, slug, path) {

View File

@ -31,7 +31,6 @@ const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
collection = action.payload.collection; collection = action.payload.collection;
loadedEntries = action.payload.entries; loadedEntries = action.payload.entries;
page = action.payload.page; page = action.payload.page;
return state.withMutations((map) => { return state.withMutations((map) => {
loadedEntries.forEach(entry => ( loadedEntries.forEach(entry => (
map.setIn(['entities', `${ collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false)) map.setIn(['entities', `${ collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false))

View File

@ -9,6 +9,26 @@ function formatToExtension(format) {
}[format]; }[format];
} }
function slugFormatter(template, entryData) {
const date = new Date();
const entry = (typeof entryData === 'string') ? entryData : entryData.get('title', entryData.get('path'));
const identifier = entry.match(/([^:\\/]*?)(?:\.([^ :\\/.]*))?$/)[1];
return template.replace(/\{\{([^\}]+)\}\}/g, (_, 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 identifier.trim().toLowerCase().replace(/[^a-z0-9\.\-_]+/gi, '-');
default:
return identifier.trim().toLowerCase().replace(/[^a-z0-9\.\-_]+/gi, '-');
}
});
}
class FolderCollection { class FolderCollection {
constructor(collection) { constructor(collection) {
this.collection = collection; this.collection = collection;
@ -23,7 +43,7 @@ class FolderCollection {
} }
entrySlug(path) { entrySlug(path) {
return path.split('/').pop().replace(/\.[^\.]+$/, ''); return slugFormatter(this.collection.get('slug'), path);
} }
listMethod() { listMethod() {