Load unpublished entries
This commit is contained in:
@ -100,7 +100,7 @@ export default class API {
|
||||
});
|
||||
}
|
||||
|
||||
retrieveMetadata(key, data) {
|
||||
retrieveMetadata(key) {
|
||||
const cache = LocalForage.getItem(`gh.meta.${key}`);
|
||||
return cache.then((cached) => {
|
||||
if (cached && cached.expires > Date.now()) { return cached.data; }
|
||||
@ -109,7 +109,9 @@ export default class API {
|
||||
params: { ref: 'refs/meta/_netlify_cms' },
|
||||
headers: { Accept: 'application/vnd.github.VERSION.raw' },
|
||||
cache: 'no-store',
|
||||
}).then((result) => {
|
||||
})
|
||||
.then(response => JSON.parse(response))
|
||||
.then((result) => {
|
||||
LocalForage.setItem(`gh.meta.${key}`, {
|
||||
expires: Date.now() + 300000, // In 5 minutes
|
||||
data: result,
|
||||
@ -119,20 +121,19 @@ export default class API {
|
||||
}).catch(error => null);
|
||||
}
|
||||
|
||||
readFile(path, sha) {
|
||||
readFile(path, sha, branch = this.branch) {
|
||||
const cache = sha ? LocalForage.getItem(`gh.${sha}`) : Promise.resolve(null);
|
||||
return cache.then((cached) => {
|
||||
if (cached) { return cached; }
|
||||
|
||||
return this.request(`${this.repoURL}/contents/${path}`, {
|
||||
headers: { Accept: 'application/vnd.github.VERSION.raw' },
|
||||
params: { ref: this.branch },
|
||||
params: { ref: branch },
|
||||
cache: false
|
||||
}).then((result) => {
|
||||
if (sha) {
|
||||
LocalForage.setItem(`gh.${sha}`, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
});
|
||||
@ -144,6 +145,22 @@ export default class API {
|
||||
});
|
||||
}
|
||||
|
||||
readUnpublishedBranchFile(contentKey) {
|
||||
let metaData;
|
||||
return this.retrieveMetadata(contentKey)
|
||||
.then(data => {
|
||||
metaData = data;
|
||||
return this.readFile(data.objects.entry, null, data.branch);
|
||||
})
|
||||
.then(file => {
|
||||
return { metaData, file };
|
||||
});
|
||||
}
|
||||
|
||||
listUnpublishedBranches() {
|
||||
return this.request(`${this.repoURL}/git/refs/heads/cms`);
|
||||
}
|
||||
|
||||
persistFiles(entry, mediaFiles, options) {
|
||||
let filename, part, parts, subtree;
|
||||
const fileTree = {};
|
||||
@ -180,7 +197,10 @@ export default class API {
|
||||
collection: options.collectionName,
|
||||
title: options.parsedData.title,
|
||||
description: options.parsedData.description,
|
||||
objects: files.map(file => file.path)
|
||||
objects: {
|
||||
entry: entry.path,
|
||||
files: mediaFiles.map(file => file.path)
|
||||
}
|
||||
}))
|
||||
.then(this.createPR(options.commitMessage, `cms/${contentKey}`));
|
||||
} else {
|
||||
|
@ -64,9 +64,30 @@ export default class GitHub {
|
||||
}
|
||||
|
||||
unpublishedEntries() {
|
||||
return Promise.resolve({
|
||||
pagination: {},
|
||||
entries: []
|
||||
return this.api.listUnpublishedBranches().then((branches) => {
|
||||
const sem = semaphore(MAX_CONCURRENT_DOWNLOADS);
|
||||
const promises = [];
|
||||
branches.map((branch) => {
|
||||
promises.push(new Promise((resolve, reject) => {
|
||||
const contentKey = branch.ref.split('refs/heads/cms/').pop();
|
||||
return sem.take(() => this.api.readUnpublishedBranchFile(contentKey).then((data) => {
|
||||
const entryPath = data.metaData.objects.entry;
|
||||
const entry = createEntry(entryPath, entryPath.split('/').pop().replace(/\.[^\.]+$/, ''), data.file);
|
||||
entry.metaData = data.metaData;
|
||||
resolve(entry);
|
||||
sem.leave();
|
||||
}).catch((err) => {
|
||||
sem.leave();
|
||||
reject(err);
|
||||
}));
|
||||
}));
|
||||
});
|
||||
return Promise.all(promises);
|
||||
}).then((entries) => {
|
||||
return {
|
||||
pagination: {},
|
||||
entries
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user