2019-05-08 15:42:29 +00:00
|
|
|
const program = require('commander');
|
|
|
|
const fs = require('fs');
|
|
|
|
const jsdom = require('jsdom').JSDOM,
|
|
|
|
options = {
|
|
|
|
resources: "usable"
|
|
|
|
};
|
|
|
|
|
|
|
|
program
|
2019-05-12 13:44:03 +00:00
|
|
|
.version('0.1.1')
|
2019-05-08 15:42:29 +00:00
|
|
|
.option('-t, --title [title]', 'give blog a title')
|
2019-05-09 18:46:25 +00:00
|
|
|
.option('-s, --subtitle [subtitle]', 'give blog a subtitle', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
|
2019-05-12 13:44:03 +00:00
|
|
|
.option('-p, --pagetitle [pagetitle]', 'give blog page a title')
|
|
|
|
.option('-f, --folder [folder]', 'give folder a title (use "-" instead of spaces)')
|
2019-05-08 15:42:29 +00:00
|
|
|
.parse(process.argv);
|
|
|
|
|
2019-05-09 18:30:36 +00:00
|
|
|
function createBlog(title, subtitle, pagetitle, folder) {
|
2019-05-20 07:38:30 +00:00
|
|
|
if (!fs.existsSync(`./dist/blog/${folder}`)){
|
|
|
|
fs.mkdirSync(`./dist/blog/${folder}`, { recursive: true });
|
2019-05-08 15:42:29 +00:00
|
|
|
}
|
2019-05-20 07:38:30 +00:00
|
|
|
fs.copyFile('./assets/blog/blogTemplate.html', `./dist/blog/${folder}/index.html`, (err) => {
|
2019-05-08 15:42:29 +00:00
|
|
|
if (err) throw err;
|
2019-05-20 07:38:30 +00:00
|
|
|
jsdom.fromFile(`./dist/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-20 07:38:30 +00:00
|
|
|
fs.writeFile(`./dist/blog/${folder}/index.html`, '<!DOCTYPE html>'+window.document.documentElement.outerHTML, 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-20 09:17:39 +00:00
|
|
|
fs.readFile("./dist/blog.json", function (err , data) {
|
2019-05-08 15:42:29 +00:00
|
|
|
if (err) throw err;
|
|
|
|
var old_blogs = JSON.parse(data);
|
|
|
|
old_blogs.push(blog_data);
|
2019-05-20 09:17:39 +00:00
|
|
|
fs.writeFile('./dist/blog.json', JSON.stringify(old_blogs, null, ' '), function(err){
|
2019-05-08 15:42:29 +00:00
|
|
|
if (err) throw err;
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (program.title) {
|
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 */
|
|
|
|
if (!fs.existsSync(`./dist/index.html`) || !fs.existsSync(`./dist/index.css`)){
|
|
|
|
return console.log("You need to run build command before using blog one");
|
|
|
|
}
|
2019-05-09 18:30:36 +00:00
|
|
|
if (!program.pagetitle) {
|
|
|
|
program.pagetitle = program.title;
|
|
|
|
}
|
|
|
|
if (!program.folder) {
|
|
|
|
program.folder = program.title;
|
|
|
|
}
|
|
|
|
createBlog(program.title, program.subtitle, program.pagetitle, program.folder);
|
2019-05-08 15:42:29 +00:00
|
|
|
} else {
|
2019-05-12 13:44:03 +00:00
|
|
|
console.log("Provide a title to create a new blog");
|
2019-05-09 18:30:36 +00:00
|
|
|
}
|