2020-07-13 23:08:29 +02:00
|
|
|
import Settings, { SETTINGS_FILE } from './Settings';
|
2020-05-21 23:13:03 +02:00
|
|
|
import Render from './Render';
|
|
|
|
import StringUtils from '../../src/com/utils/StringUtils';
|
|
|
|
import _ from 'lodash';
|
|
|
|
const settings = new Settings();
|
|
|
|
const render = new Render();
|
|
|
|
const stringUtils = new StringUtils();
|
2020-06-15 03:18:31 +02:00
|
|
|
const moment = require('moment');
|
2020-07-13 23:08:29 +02:00
|
|
|
const fs = require('fs-extra');
|
2020-08-13 21:53:51 +02:00
|
|
|
const AdmZip = require('adm-zip');
|
2020-05-21 23:13:03 +02:00
|
|
|
|
|
|
|
export default class Utils {
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves single page or pages
|
|
|
|
* @parameter pages: payload of pages
|
|
|
|
*/
|
|
|
|
organizeTags(pages) {
|
|
|
|
let tags = [];
|
|
|
|
for (let index = 0; index < pages.length; index++) {
|
|
|
|
const page = pages[index];
|
|
|
|
let temp = [];
|
|
|
|
temp = page.metadata.tags.split(',');
|
|
|
|
for (let i = 0; i < temp.length; i++) {
|
|
|
|
let label = temp[i].trim();
|
|
|
|
if (!_.find(tags, { tag_name: label })) {
|
|
|
|
tags.push({
|
|
|
|
tag_name: label,
|
|
|
|
slug: stringUtils.cleanString(label),
|
|
|
|
count: 1
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_.find(tags, { tag_name: label }).count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tags = _.orderBy(tags, ['tag_name'], ['asc']);
|
|
|
|
|
|
|
|
settings.saveTags(tags).then(() => {
|
|
|
|
render
|
|
|
|
.publishTags(pages)
|
2020-06-26 00:28:35 +02:00
|
|
|
.then(() => {
|
|
|
|
//console.log(response);
|
2020-05-21 23:13:03 +02:00
|
|
|
})
|
2020-06-26 00:28:35 +02:00
|
|
|
.catch(() => {
|
|
|
|
//console.log(err);
|
2020-05-21 23:13:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-06-15 03:18:31 +02:00
|
|
|
organizeArchive(pages) {
|
|
|
|
let years = [];
|
|
|
|
let archive = [];
|
|
|
|
for (let index = 0; index < pages.length; index++) {
|
|
|
|
let page = pages[index].metadata;
|
2020-06-21 03:13:23 +02:00
|
|
|
if (page.layout !== 'index') {
|
|
|
|
let year = moment(page.created).format('YYYY');
|
|
|
|
if (!_.find(years, { year: year })) {
|
|
|
|
years.push({ year: year, count: 1 });
|
|
|
|
} else {
|
|
|
|
_.find(years, { year: year }).count++;
|
|
|
|
}
|
2020-06-15 03:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
years.sort((a, b) => parseFloat(b.year) - parseFloat(a.year));
|
|
|
|
for (let index = 0; index < years.length; index++) {
|
|
|
|
let item = years[index];
|
|
|
|
let sorted = [];
|
|
|
|
let filtered = _.filter(pages, page => {
|
|
|
|
return moment(page.metadata.created).format('YYYY') === item.year;
|
|
|
|
});
|
|
|
|
for (let index = 0; index < filtered.length; index++) {
|
|
|
|
let obj = filtered[index].metadata;
|
|
|
|
let month = moment(obj.created).format('MM');
|
|
|
|
if (!_.find(sorted, { month: month })) {
|
|
|
|
sorted.push({
|
|
|
|
month: month,
|
|
|
|
full_month: moment(obj.created).format('MMMM'),
|
|
|
|
count: 1,
|
2020-06-15 05:18:26 +02:00
|
|
|
pages: _.filter(pages, page => {
|
|
|
|
return (
|
|
|
|
moment(page.metadata.created).format('YYYY') === item.year &&
|
|
|
|
moment(page.metadata.created).format('MM') === month &&
|
|
|
|
page.metadata.deleted === false &&
|
2020-06-26 00:15:19 +02:00
|
|
|
page.metadata.published === true &&
|
2020-06-15 05:18:26 +02:00
|
|
|
page.metadata.layout !== 'index'
|
|
|
|
);
|
|
|
|
})
|
2020-06-15 03:18:31 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_.find(sorted, { month: month }).count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
archive.push({ year: item.year, year_data: sorted });
|
|
|
|
}
|
|
|
|
render.publishArchive(archive);
|
|
|
|
}
|
2020-07-23 22:14:21 +02:00
|
|
|
reindexPages(pages) {
|
|
|
|
//let byDate = _.sortBy(pages, ['created']);
|
|
|
|
}
|
2020-07-13 23:08:29 +02:00
|
|
|
moveAssets() {
|
|
|
|
settings
|
|
|
|
.load(SETTINGS_FILE)
|
|
|
|
.then(settings => {
|
|
|
|
//move css assets to public directory
|
|
|
|
fs.copy(
|
|
|
|
'content/themes/' + settings.global.theme + '/assets/css',
|
|
|
|
'public/assets/css',
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
//console.log('An error occured while copying the folder.', err);
|
|
|
|
//return console.error(err);
|
|
|
|
}
|
|
|
|
//console.log('Copy completed!');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
//move js assets to public directory
|
|
|
|
fs.copy(
|
|
|
|
'content/themes/' + settings.global.theme + '/assets/scripts',
|
|
|
|
'public/assets/scripts',
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
|
|
|
//console.log('An error occured while copying the folder.', err);
|
|
|
|
//return console.error(err);
|
|
|
|
}
|
|
|
|
//console.log('Copy completed!');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
//console.log('ERROR', err);
|
|
|
|
});
|
|
|
|
}
|
2020-08-13 21:53:51 +02:00
|
|
|
createBackup() {
|
|
|
|
//let self = this;
|
|
|
|
var response;
|
|
|
|
return new Promise(resolve => {
|
|
|
|
var zip = new AdmZip();
|
2020-08-18 00:05:10 +02:00
|
|
|
zip.addLocalFolder('public/assets/images/blog', 'public/assets/images/blog');
|
|
|
|
zip.addLocalFolder('content/pages', 'content/pages/');
|
2020-08-13 21:53:51 +02:00
|
|
|
zip.addLocalFile('site/folks.json', 'settings/');
|
|
|
|
zip.addLocalFile('site/settings.json', 'settings/');
|
|
|
|
zip.addLocalFile('site/tags.json', 'settings/');
|
2020-08-18 00:05:10 +02:00
|
|
|
zip.writeZip('content/backup.zip');
|
2020-08-13 21:53:51 +02:00
|
|
|
fs.readJSON('site/settings.json').then(settings => {
|
|
|
|
settings.global.last_backup = moment(Date.now()).format();
|
|
|
|
fs.writeJSON('site/settings.json', settings);
|
|
|
|
});
|
|
|
|
|
|
|
|
response = {
|
|
|
|
type: '',
|
|
|
|
message: 'BACKUP CREATED'
|
|
|
|
};
|
|
|
|
|
|
|
|
resolve(response);
|
|
|
|
});
|
|
|
|
}
|
2020-08-18 00:05:10 +02:00
|
|
|
|
|
|
|
restoreBackup(file) {
|
|
|
|
//var response;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
var zip = new AdmZip(file.buffer);
|
|
|
|
zip.extractEntryTo('settings/settings.json', 'site', false, true);
|
|
|
|
zip.extractEntryTo('settings/folks.json', 'site', false, true);
|
|
|
|
zip.extractEntryTo('settings/tags.json', 'site', false, true);
|
|
|
|
zip.getEntries().forEach(function (entry) {
|
|
|
|
var entryName = entry.entryName;
|
|
|
|
var list = entryName.split('/');
|
|
|
|
if (list[0] === 'public') {
|
|
|
|
if (list[6]) {
|
|
|
|
zip.extractEntryTo(
|
|
|
|
entryName,
|
|
|
|
'public/assets/images/blog/' + list[4] + '/' + list[5],
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (list[0] === 'content') {
|
|
|
|
if (list[4]) {
|
|
|
|
zip.extractEntryTo(
|
|
|
|
entryName,
|
|
|
|
'content/pages/' + list[2] + '/' + list[3],
|
|
|
|
false,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
zip.extractEntryTo('content/pages/index.md', 'content/pages', false, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
2020-05-21 23:13:03 +02:00
|
|
|
}
|