2016-02-25 12:31:21 -08:00
|
|
|
import AuthenticationPage from './AuthenticationPage';
|
2017-04-14 19:19:45 +01:00
|
|
|
import { fileExtension } from '../../lib/pathHelper'
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2017-05-31 16:12:31 -04:00
|
|
|
window.repoFiles = window.repoFiles || {};
|
|
|
|
|
2016-10-27 11:47:19 +02:00
|
|
|
function getFile(path) {
|
|
|
|
const segments = path.split('/');
|
|
|
|
let obj = window.repoFiles;
|
|
|
|
while (obj && segments.length) {
|
|
|
|
obj = obj[segments.shift()];
|
|
|
|
}
|
2017-05-31 16:12:31 -04:00
|
|
|
return obj || {};
|
2016-10-27 11:47:19 +02:00
|
|
|
}
|
|
|
|
|
2016-12-30 09:40:15 -08:00
|
|
|
function nameFromEmail(email) {
|
|
|
|
return email
|
|
|
|
.split('@').shift().replace(/[.-_]/g, ' ')
|
|
|
|
.split(' ')
|
|
|
|
.filter(f => f)
|
|
|
|
.map(s => s.substr(0, 1).toUpperCase() + (s.substr(1) || ''))
|
|
|
|
.join(' ');
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
export default class TestRepo {
|
|
|
|
constructor(config) {
|
|
|
|
this.config = config;
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:52:18 -07:00
|
|
|
setUser() {}
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
authComponent() {
|
|
|
|
return AuthenticationPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticate(state) {
|
2016-12-30 09:40:15 -08:00
|
|
|
return Promise.resolve({ email: state.email, name: nameFromEmail(state.email) });
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2017-01-10 22:23:22 -02:00
|
|
|
getToken() {
|
|
|
|
return Promise.resolve('');
|
|
|
|
}
|
|
|
|
|
2017-04-14 19:19:45 +01:00
|
|
|
entriesByFolder(collection, extension) {
|
2016-02-25 20:40:35 -08:00
|
|
|
const entries = [];
|
|
|
|
const folder = collection.get('folder');
|
|
|
|
if (folder) {
|
2016-10-20 14:27:58 -02:00
|
|
|
for (const path in window.repoFiles[folder]) {
|
2017-04-14 19:19:45 +01:00
|
|
|
if (fileExtension(path) !== extension) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-21 20:42:14 -02:00
|
|
|
const file = { path: `${ folder }/${ path }` };
|
|
|
|
entries.push(
|
|
|
|
{
|
|
|
|
file,
|
|
|
|
data: window.repoFiles[folder][path].content,
|
|
|
|
}
|
|
|
|
);
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
}
|
2016-10-21 20:42:14 -02:00
|
|
|
return Promise.resolve(entries);
|
|
|
|
}
|
2016-02-25 20:40:35 -08:00
|
|
|
|
2016-10-27 14:23:36 +02:00
|
|
|
entriesByFiles(collection) {
|
|
|
|
const files = collection.get('files').map(collectionFile => ({
|
|
|
|
path: collectionFile.get('file'),
|
|
|
|
label: collectionFile.get('label'),
|
|
|
|
}));
|
2016-10-27 11:47:19 +02:00
|
|
|
return Promise.all(files.map(file => ({
|
|
|
|
file,
|
|
|
|
data: getFile(file.path).content,
|
|
|
|
})));
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
|
|
|
|
2016-10-27 14:23:36 +02:00
|
|
|
getEntry(collection, slug, path) {
|
|
|
|
return Promise.resolve({
|
|
|
|
file: { path },
|
2017-01-10 22:23:22 -02:00
|
|
|
data: getFile(path).content,
|
2016-10-27 14:23:36 +02:00
|
|
|
});
|
2016-02-25 20:40:35 -08:00
|
|
|
}
|
2016-06-06 21:53:22 -03:00
|
|
|
|
2016-08-29 17:09:04 -03:00
|
|
|
persistEntry(entry, mediaFiles = [], options) {
|
|
|
|
const newEntry = options.newEntry || false;
|
2016-07-19 17:11:22 -03:00
|
|
|
const folder = entry.path.substring(0, entry.path.lastIndexOf('/'));
|
2016-06-10 00:16:01 -03:00
|
|
|
const fileName = entry.path.substring(entry.path.lastIndexOf('/') + 1);
|
2017-05-31 16:12:31 -04:00
|
|
|
window.repoFiles[folder] = window.repoFiles[folder] || {};
|
|
|
|
window.repoFiles[folder][fileName] = window.repoFiles[folder][fileName] || {};
|
2016-08-25 16:11:00 -03:00
|
|
|
if (newEntry) {
|
|
|
|
window.repoFiles[folder][fileName] = { content: entry.raw };
|
|
|
|
} else {
|
2016-10-20 14:27:58 -02:00
|
|
|
window.repoFiles[folder][fileName].content = entry.raw;
|
2016-08-25 16:11:00 -03:00
|
|
|
}
|
2016-07-19 19:42:37 -03:00
|
|
|
return Promise.resolve();
|
2016-06-06 21:53:22 -03:00
|
|
|
}
|
2016-09-06 13:04:17 -03:00
|
|
|
|
2017-08-17 13:25:54 -06:00
|
|
|
deleteFile(path, commitMessage) {
|
2017-07-21 23:40:33 -07:00
|
|
|
const folder = path.substring(0, path.lastIndexOf('/'));
|
|
|
|
const fileName = path.substring(path.lastIndexOf('/') + 1);
|
2017-08-17 13:25:54 -06:00
|
|
|
delete window.repoFiles[folder][fileName];
|
2017-07-21 23:40:33 -07:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2016-02-25 12:31:21 -08:00
|
|
|
}
|