2019-05-08 15:42:29 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const jsdom = require('jsdom').JSDOM,
|
|
|
|
options = {
|
|
|
|
resources: "usable"
|
|
|
|
};
|
2019-05-23 05:00:49 +00:00
|
|
|
const {getBlog, outDir} = require('./utils');
|
2019-05-08 15:42:29 +00:00
|
|
|
|
2019-05-09 18:30:36 +00:00
|
|
|
function createBlog(title, subtitle, pagetitle, folder) {
|
2019-05-21 17:05:41 +00:00
|
|
|
// Checks to make sure this directory actually exists
|
|
|
|
// and creates it if it doesn't
|
2019-05-23 05:00:49 +00:00
|
|
|
if (!fs.existsSync(`${outDir}/blog/`)){
|
|
|
|
fs.mkdirSync(`${outDir}/blog/`, { recursive: true }, err => {});
|
2019-05-21 17:05:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 05:00:49 +00:00
|
|
|
if (!fs.existsSync(`${outDir}/blog/${folder}`)){
|
|
|
|
fs.mkdirSync(`${outDir}/blog/${folder}`, { recursive: true });
|
2019-05-08 15:42:29 +00:00
|
|
|
}
|
2019-05-23 05:00:49 +00:00
|
|
|
fs.copyFile(`${__dirname}/assets/blog/blogTemplate.html`, `${outDir}/blog/${folder}/index.html`, (err) => {
|
2019-05-08 15:42:29 +00:00
|
|
|
if (err) throw err;
|
2019-05-23 05:00:49 +00:00
|
|
|
jsdom.fromFile(`${outDir}/blog/${folder}/index.html`, options).then(function (dom) {
|
2019-05-08 15:42:29 +00:00
|
|
|
let window = dom.window, document = window.document;
|
|
|
|
var style = document.createElement("link");
|
|
|
|
style.setAttribute("rel","stylesheet")
|
|
|
|
style.setAttribute("href","../../index.css");
|
|
|
|
document.getElementsByTagName("head")[0].appendChild(style);
|
2019-05-09 18:30:36 +00:00
|
|
|
|
|
|
|
document.getElementsByTagName("title")[0].textContent = pagetitle;
|
|
|
|
document.getElementById("blog_title").textContent = title;
|
|
|
|
document.getElementById("blog_sub_title").textContent = subtitle;
|
|
|
|
|
2019-05-23 05:00:49 +00:00
|
|
|
fs.writeFile(`${outDir}/blog/${folder}/index.html`, '<!DOCTYPE html>'+window.document.documentElement.outerHTML, async function (error){
|
2019-05-08 15:42:29 +00:00
|
|
|
if (error) throw error;
|
|
|
|
var blog_data = {
|
2019-05-09 18:30:36 +00:00
|
|
|
"url_title": pagetitle,
|
|
|
|
"title": title,
|
|
|
|
"sub_title": subtitle,
|
2019-05-08 15:42:29 +00:00
|
|
|
"top_image": "https://images.unsplash.com/photo-1553748024-d1b27fb3f960?w=1450",
|
|
|
|
"visible": true }
|
2019-05-23 05:00:49 +00:00
|
|
|
const old_blogs = await getBlog();
|
|
|
|
old_blogs.push(blog_data);
|
|
|
|
fs.writeFile(`${outDir}/blog.json`, JSON.stringify(old_blogs, null, ' '), function(err){
|
2019-05-08 15:42:29 +00:00
|
|
|
if (err) throw err;
|
2019-05-23 05:00:49 +00:00
|
|
|
console.log('Blog Created Successfully in "blog" folder.');
|
2019-05-12 13:44:03 +00:00
|
|
|
});
|
2019-05-08 15:42:29 +00:00
|
|
|
});
|
|
|
|
}).catch(function(error){
|
|
|
|
console.log(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-23 05:00:49 +00:00
|
|
|
function blogCommand(title, program) {
|
2019-05-20 09:22:19 +00:00
|
|
|
/* Check if build has been executed before blog this will prevent it from giving "link : index.css" error */
|
2019-05-23 05:00:49 +00:00
|
|
|
if (!fs.existsSync(`${outDir}/index.html`) || !fs.existsSync(`${outDir}/index.css`)){
|
2019-05-20 09:22:19 +00:00
|
|
|
return console.log("You need to run build command before using blog one");
|
|
|
|
}
|
2019-05-09 18:30:36 +00:00
|
|
|
if (!program.pagetitle) {
|
2019-05-23 05:00:49 +00:00
|
|
|
program.pagetitle = title;
|
2019-05-09 18:30:36 +00:00
|
|
|
}
|
|
|
|
if (!program.folder) {
|
2019-05-23 05:00:49 +00:00
|
|
|
program.folder = title;
|
2019-05-09 18:30:36 +00:00
|
|
|
}
|
2019-05-23 05:00:49 +00:00
|
|
|
createBlog(title, program.subtitle, program.pagetitle, program.folder);
|
2019-05-09 18:30:36 +00:00
|
|
|
}
|
2019-05-23 05:00:49 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2019-05-23 09:57:23 +00:00
|
|
|
blogCommand
|
2019-05-23 05:00:49 +00:00
|
|
|
};
|