-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
81 lines (75 loc) · 2.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const fs = require('fs');
const clear = require('clear');
const chalk = require('chalk');
const figlet = require('figlet');
const inquirer = require('inquirer');
const questions = require('./questions');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const { Script } = require('vm');
init();
askQuestions();
function init () {
clear();
console.log(
chalk.green(
figlet.textSync('Node Girls\nevent cli', { horizontalLayout: 'full' })
)
);
}
function askQuestions () {
inquirer.prompt(questions).then((answers) => {
if (answers.confirm === false) {
init();
askQuestions();
}
// generate HTML
console.log(answers)
prepareTemplate(answers, function (err, eventHTML) {
if (err) {
throw err;
}
generateHTML(eventHTML);
});
});
}
function prepareTemplate (answers, callback) {
fs.readFile('../website/templates/event.html', 'utf8', function (err, template) {
if (err) {
return callback(err);
}
let final = template;
for (let key in answers) {
final = final.replace(new RegExp('{{' + key + '}}', 'g'), answers[key]);
}
return callback(err, final);
});
}
function generateHTML (eventHTML) {
fs.readFile('../website/index.html', function (err, html) {
const indexDom = new JSDOM(html);
const upcomingEvent = indexDom.window.document.querySelector('.event-container');
upcomingEvent.innerHTML = eventHTML;
fs.writeFile('../website/index.html', indexDom.serialize(), (err)=>{});
});
['london'].forEach((page) => {
fs.readFile(`../website/${page}.html`, function (err, html) {
const dom = new JSDOM(html, { runScripts: 'outside-only' });
// const upcomingEvent = dom.window.document.createElement('div');
// upcomingEvent.innerHTML = eventHTML;
// console.log(upcomingEvent)
// const referenceNode = dom.window.document.querySelector('.event-container');
// referenceNode.parentNode.insertBefore(upcomingEvent, referenceNode);
const script = new Script(`
const upcomingEvent = this.document.createElement('div');
upcomingEvent.innerHTML = eventHTML;
const referenceNode = this.document.querySelector('.event-container');
referenceNode.parentNode.insertBefore(upcomingEvent, referenceNode);
`);
script.runInNewContext(eventHTML);
dom.runVMScript(script);
console.log(dom.serialize())
// fs.writeFile(`../website/${page}.html`, dom.serialize(), (err) => {});
});
})
}