-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprev.js
executable file
·398 lines (272 loc) · 9.03 KB
/
prev.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env node
/*
File: prev.js
Description:
Main entry point of prevjs command line tool
It processes recipe.json and calls other classes to peform operations
It also starts expressjs server to preview site
*/
//### start of required external libraries
const { program } = require('commander');
const express = require('express');
const path = require('path');
const { Table } = require('console-table-printer');
const { exec } = require("child_process");
const execFile = require('child_process').execFile;
const execSync = require('child_process').execSync;
const process = require('process');
const {resolve} = require('path');
const resolvepath = require('path').resolve;
const fs = require('fs');
const WebSocket = require('ws');
const chokidar = require('chokidar');
var minify = require('html-minifier').minify;
var minifyHTML = require('express-minify-html');
//### end of required external libraries
//### start of required internal classes
const pageRenderer = require('./main/page-renderer.js')
const pageRendererObj = new pageRenderer();
const exportSite = require('./main/export.js')
const exportSiteObj = new exportSite();
const createSite = require('./main/create.js')
const createSiteObj = new createSite();
const awsManager = require('./deploy/awsManager.js')
const awsManagerObj = new awsManager();
const prevUtils = require('./main/utils.js')
const prevUtilsObj = new prevUtils();
//### end of required internal classes
//### start of global variables
var DEFAULT_DIRS = new Object();
DEFAULT_DIRS["DRAFTS"] = true;
DEFAULT_DIRS["STATIC"] = true;
DEFAULT_DIRS["PARTIALS"] = true;
DEFAULT_DIRS["TEMPLATES"] = true;
global.DEFAULT_DIRS = DEFAULT_DIRS;
global.DEBUG = true;
global.LOCAL_PREVIEW = false;
global.pconfig = new Object();
global.app = express();
//Set the view engine to ejs
global.app.set('view engine', 'ejs');
//parse request body as JSON post request
global.app.use(express.json());
//for read the urlencode data
global.app.use(express.urlencoded({
extended: true
}))
//### end of global variable
//to check if an command line argument matched
var argPresent = false;
//parse recipe.json file and set global variables
function processRecipe(recipe)
{
try {
recipe = recipe.trim();
let configdata = fs.readFileSync(recipe);
if(configdata)
{
recipe = resolvepath(recipe);
global.pconfig = JSON.parse(configdata);
if(!global.pconfig.port)
global.pconfig.port = 3000;
global.pconfig.searchEnabled = false;
if(global.pconfig.search)
{
if(global.pconfig.search.indexing)
{
global.pconfig.searchEnabled = true;
}
}
//local path of website folder
global.pconfig.localpath = recipe.replace("recipe.json","");
if(!global.pconfig.production_url)
global.pconfig.production_url = "http://localhost:"+global.pconfig.port;
if(!global.pconfig.exportdir)
global.pconfig.exportdir = global.pconfig.localpath+"out"+path.sep;
if(!global.pconfig.createdir)
global.pconfig.createdir = recipe;
//global expressjs static folder path
global.app.use(express.static(global.pconfig.localpath+'STATIC'));
//Set the view folder
global.app.set('views', global.pconfig.localpath);
}
else
{
console.log("Error");
}
}
catch(e)
{
if(global.DEBUG)
console.log(e)
console.log("Check path of recipe.json file. Example /Users/myname/website/recipe.json");
process.exit();
}
}
//--run argument for starting local server to preview website
if(process.argv.length == 4 && process.argv[2].trim() == "--run" && process.argv[3].includes("recipe.json"))
{
argPresent = true;
processRecipe(process.argv[3]);
var wssadded = false;
//expressjs server to start listening
global.app.listen(global.pconfig.port, () => {
//watch for any file changes
chokidar.watch(global.pconfig.localpath, {ignoreInitial: true}).on('all', (event, path) => {
if(wss)
{
//send to all clients based on file changes
//TODO: Need better handling of which client needs to be updated
try
{
wss.clients.forEach((client) => {
//prune path variable from chokidar to match with browser client's address
path = path.replace(global.pconfig.localpath,"");
path = path.replace("/index.ejs","");
path = path.replace(path.sep+"index.ejs","");
path = path.replace("index.ejs","");
client.send(path);
});
}
catch(e)
{
}
}
});
wssadded = true;
const wss = new WebSocket.Server({ port: 7071 });
//Handle incoming message from browser clients
wss.on('message', (messageAsString) => {
//alert(messageAsString);
});
//Enable local preview flag
global.LOCAL_PREVIEW = true;
global.pconfig.local_url = "http://localhost:"+global.pconfig.port;
console.log("Local preview server running on URL: "+global.pconfig.local_url);
});
}
//--export argument to export prevjs site to a specific folder
if(process.argv.length == 4 && process.argv[2].trim() == "--export" && process.argv[3].includes("recipe.json"))
{
argPresent = true;
console.log("Exporting site");
processRecipe(process.argv[3]);
//minification configuration
if(global.pconfig.optimize.minify_html == "false")
{
//stub
}
else
{
global.app.use(minifyHTML({
override: true,
exception_url: false,
htmlMinifier: {
removeComments: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeEmptyAttributes: true,
minifyJS: true
}
}));
}
exportSiteObj.export();
}
//--create argument to create a new prevjs site structure to a specific folder
if(process.argv.length == 4 && process.argv[2].trim() == "--create")
{
argPresent = true;
console.log("Creating site");
global.pconfig = new Object();
global.pconfig.createdir = process.argv[3];
createSiteObj.createLocal('basic');
}
//--create argument to create a new prevjs site structure to a specific folder
if(process.argv.length == 5 && process.argv[2].trim() == "--create")
{
argPresent = true;
console.log("Creating site");
global.pconfig = new Object();
global.pconfig.createdir = process.argv[4].trim();
createSiteObj.createLocal(process.argv[3].trim());
}
//--deploy argument to deploy an exported prevjs site to AWS. Depends upon already setup aws command
if(process.argv.length == 4 && process.argv[2].trim() == "--deploy" && process.argv[3].includes("recipe.json"))
{
argPresent = true;
console.log("Deploying site");
processRecipe(process.argv[3]);
if(global.pconfig.deploy && global.pconfig.deploy.type == "aws")
{
if (!fs.existsSync(global.pconfig.exportdir))
{
console.log("Output folder does not exist at " + global.pconfig.exportdir +" .Export site first before deploying.");
process.exit();
}
awsManagerObj.deploy();
}
else
{
console.log("Deploy properties not found in recipe.json. Refer documentation.");
process.exit();
}
}
//--list-recipes argument to deploy an exported prevjs site to AWS. Depends upon already setup aws command
if(process.argv.length == 4 && process.argv[2].trim() == "--list-recipes" && process.argv[3].trim() == "local")
{
argPresent = true;
listLocalRecipes();
}
if(process.argv.length == 3 && process.argv[2].trim() == "--list-recipes")
{
argPresent = true;
listLocalRecipes();
}
function listLocalRecipes()
{
console.log("Listing local recipes");
var srcDataDir = path.join(__dirname, "recipes/list.json");
fs.readFile(srcDataDir, "utf8", (err, jsonString) => {
var obj = JSON.parse(jsonString);
const p = new Table();
p.addRows(obj, { color: 'blue' });
p.printTable();
process.exit();
});
}
//Declare routes
//Preview website in local (Note: not for production use)
global.app.get('/*', (req, res) => {
if(global.LOCAL_PREVIEW)
pageRendererObj.previewPage(req,res);
});
//Error handling to prevent app from crashing
process.on('uncaughtException', (error, origin) => {
console.log('----- Uncaught exception -----')
console.log(error)
console.log('----- Exception origin -----')
console.log(origin)
});
process.on('unhandledRejection', (reason, promise) => {
console.log('----- Unhandled Rejection at -----')
console.log(promise)
console.log('----- Reason -----')
console.log(reason)
});
program
.name('prevjs')
.description('Static website builder')
.version('0.1.7')
.option('--list-recipes local','List locally available website recipes')
.option('--create <path-to-create-new-site>','Enter local path for creating a new prevjs site')
.option('--create <recipe-id> <path-to-create-new-site>','Enter local path to install a particular recipe')
.option('--run <path-to-recipe.json>','To preview website in local server')
.option('--export <path-to-recipe.json>','To export website')
.option('--deploy <path-to-recipe.json>','To deploy exported website');
program.parse();
const options = program.opts();
if(!argPresent)
{
program.help();
}