-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
166 lines (146 loc) · 5.22 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
/*eslint no-console:0, quotes:0, no-debugger:0*/
/*global
require, __dirname
*/
const bluebird = require('bluebird');
const fs = bluebird.promisifyAll(require('fs'));
const path = require('path');
const timers = bluebird.promisifyAll(require('timers'));
const pug = require('pug');
// ------------------------------------------------------------
// Compilation of .vue components.
// Watches the specified dir.
// Calls OnChange(full_file_path) when that file is first discovered or changed.
// No action on file removals, unless they are re-introduced back again.
function WatchDir(dir, OnChange) {
const file_list = {};
function ScanDir() {
// DO: test non-existing folder
fs.readdirAsync(dir).then(files => {
for (const file of files) {
if (!file.endsWith('.vue')) {
continue;
}
const full_path = dir + '/' + file;
fs.statAsync(full_path).then(function (stats) {
if (file_list[full_path] !== stats.mtime.toJSON()) {
file_list[full_path] = stats.mtime.toJSON();
OnChange(full_path);
}
}).catch(e => { debugger; });
}
}).catch(e => { debugger; 'Fix the path to UI components!'; });
}
ScanDir();
timers.setInterval(ScanDir, 3000);
}
// Compiles all updated .vue components into static renderers.
const template_compiler = require('vue-template-compiler');
function CompileVueComponents(path_to_vue_components, path_to_output_modules) {
WatchDir(path_to_vue_components, function (filename) {
fs.readFileAsync(filename, 'utf8').then(file_content => {
const pug_content = template_compiler.parseComponent(file_content).template.content;
const html = pug.render(pug_content);
const renderers = template_compiler.compile(html);
// Put into different files as
// RegisterMe(component-name, render, staticRenderFns);
//+ scan for changes off timer & update; full scan in the beginning
const component_name = path.basename(filename, '.vue');
let static_fns = '';
for (const static_fn of renderers.staticRenderFns) {
if (static_fns.length > 0) {
static_fns += ',';
}
static_fns += `function() { ${static_fn} }`;
}
const register_render_code = `
RegisterRender(
"${component_name}",
function() { ${renderers.render} },
[ ${static_fns} ]
);`;
fs.writeFileAsync(path_to_output_modules + '/' + component_name + '.js', register_render_code)
.then(() => { console.info("Successfully compiled:", filename); })
.catch(e => { debugger; });
}).catch(e => { debugger; });
});
}
CompileVueComponents('../../ui/secure_vault_ui/src/components', './public/gens');
// ------------------------------------------------------------
// Serving.
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
// Views are pug templates: resnpose.render(...).
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.get('/', function (request, response) {
// render with a parameter
// response.render('index', { title: 'First App!' });
response.render('index', {});
});
app.listen(3000);
// ------------------------------------------------------------
// Open the browser for interactive live coding.
const opn = require('opn');
opn('http://127.0.0.1:3000/');
// ------------------------------------------------------------
// Compile as a Chrome extension.
// Per instructions from
// https://developer.chrome.com/webstore/get_started_simple#step5
// at
// https://chrome.google.com/webstore/developer/dashboard
const fse = require('fs-extra');
const child_process = require('child_process');
// const child_process = bluebird.promisifyAll(require('child_process'));
Promise.all([
RenderPugFile('views/index.pug', 'extension/index.html'),
CopyAllJsFiles('public/gens', 'extension/gens'),
CopyAllJsFiles('public', 'extension'),
CopyFile('public/style.css', 'extension/style.css')
]).then(() => {
child_process.spawn('zip', ['-r', 'extension.zip', 'extension'])
.on('close', (code) => {
debugger;
});
});
function RenderPugFile(source_pug_file, dest_html_file) {
return new Promise((resolve, reject) => {
fs.readFileAsync(source_pug_file, 'utf8').then(file_content => {
const html = pug.render(file_content);
fs.writeFileAsync(dest_html_file, html)
.then(resolve)
.catch(reject);
});
});
}
function CopyAllJsFiles(from_dir, to_dir) {
return new Promise((resolve, reject) => {
fs.readdirAsync(from_dir).then(files => {
let all_copy_promises = [];
for (const file of files) {
if (!file.endsWith('.js')) {
continue;
}
const base_name = path.basename(file);
all_copy_promises.push(CopyFile(
from_dir + '/' + base_name,
to_dir + '/' + base_name));
}
Promise.all(all_copy_promises).then(resolve).catch(reject);
});
});
}
function CopyFile(from, to) {
return new Promise((resolve, reject) => {
fse.copy(from, to, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
//fs.createReadStream(from).pipe(fs.createWriteStream(to));
});
}