-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (78 loc) · 3.27 KB
/
app.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict';
const chalk = require('chalk');
const chokidar = require('chokidar');
const express = require('express');
const fs = require('fs');
const http = require('http');
const pseudoclasses = require('rework-pseudo-classes');
const rework = require('rework');
const socketIo = require('socket.io');
const parser = require('./parser/index');
const markdowner = require('./markdown/index');
const markupper = require('./markup/index');
const lintComponents = require('./linter');
const defaultConfig = require('./config.js');
const app = express();
const server = http.Server(app);
const io = socketIo(server);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get(['/*'], function(req, res) {
res.sendFile(__dirname + req.originalUrl);
});
function getReworkedCss(filePath) {
var css = fs.readFileSync(filePath, 'utf-8');
var reworkedCss = rework(css).use(pseudoclasses({
blacklist: [],
allCombinations: true }))
.toString();
return reworkedCss;
}
function build(options, config, callback) {
const css = getReworkedCss(options.file);
parser(css).then(function({ components, themes }) {
if (config.strict) {
lintComponents(components);
}
if (config.componentsFolder) {
markdowner(components);
}
const markup = markupper({ css, components, themes });
if (callback) {
callback(markup);
}
console.log(`🌈 ${chalk.green('Rebuild complete!')}`);
}).catch(err => {
console.log('parsing failed', err);
});
}
module.exports.run = function(options) {
options = options || {};
if (!options.file || !options){
throw('not invoked with required parameters');
}
const userConfig = options.config ? require(options.config) : {};
const config = Object.assign(defaultConfig, userConfig);
global.config = config;
const componentsPath = config.componentsFolder;
const watcher = chokidar.watch([`${componentsPath}/**/*.html`, options.file], {persistent: true});
build(options, config);
io.on('connection', function (socket) {
build(options, config, markup => socket.emit('data', markup));
watcher.on('change', function() {
build(options, config, markup => socket.emit('data', markup));
});
});
console.log('');
console.log(' (\\. \\ ,/) / / ');
console.log(' \\( |\\ )/ (___ ___ _ _ ___ ___ ___ ');
console.log(' //\\ | \\ /\\\\ | )|___)| | )| )| )| | ');
console.log(' (/ /\\_#oo#_/\\ \\) |__/ |__ | / |__/||__/ | |__ ');
console.log(' \\/\\ #### /\\/ __/ ');
console.log(' "##" ');
console.log('-------------------------------------------------------------------------------------');
console.log(` (ノ◕ヮ◕)ノ*:・゚✧ Watch the magic happen at ${chalk.green('http://localhost:1234')} `);
console.log('-------------------------------------------------------------------------------------');
};
server.listen(1234);