pegjs/website/server.js

146 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-09-06 06:52:02 +02:00
"use strict";
const bodyParser = require( "body-parser" );
const bundle = require( "@pegjs/bundle-generator" );
2018-09-06 06:52:02 +02:00
const express = require( "express" );
const layout = require( "express-layout" );
const logger = require( "morgan" );
const { readFileSync } = require( "fs" );
2018-09-06 06:52:02 +02:00
const { join } = require( "path" );
2018-11-04 09:26:22 +01:00
const path = ( ...parts ) => join( __dirname, "..", ...parts );
2018-09-06 13:38:26 +02:00
2018-09-06 06:52:02 +02:00
/* Setup */
const app = express();
2018-09-10 05:57:12 +02:00
const NODE_ENV = process.env.NODE_ENV;
const WARNINGS = process.argv.includes( "--show-warnings" );
2018-09-06 06:52:02 +02:00
2018-09-06 13:38:26 +02:00
app.set( "views", path( "website", "views" ) );
2018-09-06 06:52:02 +02:00
app.set( "view engine", "ejs" );
app.use( logger( "dev" ) );
2018-09-06 13:38:26 +02:00
app.use( express.static( path( "website" ) ) );
2018-11-02 18:03:30 +01:00
app.use( "/benchmark", express.static( path( "tools", "benchmark" ) ) );
2018-09-06 13:38:26 +02:00
app.use( "/examples", express.static( path( "examples" ) ) );
2018-09-06 06:52:02 +02:00
app.use( layout() );
app.use( ( req, res, next ) => {
res.locals.req = req;
next();
} );
app.locals.menuItem = ( req, id, title ) => {
const className = req.path === "/" + id ? " class=\"current\"" : "";
return `<a ${ className } href="/${ id }">${ title }</a>`;
};
/* Routes */
app.get( "/", ( req, res ) => {
res.render( "index", { title: "" } );
} );
const LEGACY_EXAMPLE = readFileSync( path( "website", "vendor", "pegjs", "arithmetics.pegjs" ), "utf8" );
2018-09-06 06:52:02 +02:00
app.get( "/online", ( req, res ) => {
2018-09-17 15:27:59 +02:00
res.render( "online", {
title: "Online version",
layout: "layout-online",
pegjs: "/vendor/pegjs/peg.js",
example: LEGACY_EXAMPLE,
2018-09-17 15:27:59 +02:00
} );
2018-09-06 06:52:02 +02:00
} );
app.post( "/online/download", bodyParser.urlencoded( { extended: false, limit: "1024kb" } ), ( req, res ) => {
res.set( "Content-Type", "application/javascript" );
res.set( "Content-Disposition", "attachment;filename=parser.js" );
res.send( req.body.source );
} );
app.get( "/documentation", ( req, res ) => {
res.render( "documentation", { title: "Documentation" } );
} );
app.get( "/development", ( req, res ) => {
res.render( "development", { title: "Development" } );
} );
const DEV_EXAMPLE = readFileSync( path( "examples", "arithmetics.pegjs" ) );
2018-09-17 15:27:59 +02:00
app.get( "/development/try", ( req, res ) => {
res.render( "online", {
title: "Online Development version",
layout: "layout-online",
pegjs: "/js/peg-bundle.js",
example: DEV_EXAMPLE,
2018-09-17 15:27:59 +02:00
} );
} );
2018-09-06 06:52:02 +02:00
app.get( "/download", ( req, res ) => {
res.redirect( 301, "/#download" );
} );
app.get( "/development/test", ( req, res ) => {
2018-09-06 13:38:26 +02:00
res.render( "test", { title: "Test Suite" } );
2018-09-06 13:38:26 +02:00
} );
app.get( "/development/benchmark", ( req, res ) => {
2018-09-06 13:38:26 +02:00
res.render( "benchmark", { title: "Benchmark Suite" } );
} );
2018-09-17 15:27:59 +02:00
/* Bundle local sources (and watch for changes on non-production NODE_ENV) */
2018-09-06 13:38:26 +02:00
2018-09-17 15:27:59 +02:00
[
2018-11-02 18:03:30 +01:00
{ name: "benchmark", input: "tools/benchmark/browser.js" },
2018-09-17 15:27:59 +02:00
{ name: "peg", input: "packages/pegjs/lib/peg.js", format: "umd" },
{ name: "test", input: "test/**/*.js" },
2018-09-17 15:27:59 +02:00
2018-11-04 08:03:59 +01:00
].forEach( project => {
2018-09-10 05:57:12 +02:00
2018-11-04 08:03:59 +01:00
bundle( {
2018-09-26 00:36:02 +02:00
2018-11-04 08:03:59 +01:00
format: project.format,
name: project.name,
source: project.input,
target: `website/js/${ project.name }-bundle.js`,
silent: !! WARNINGS,
watch: NODE_ENV !== "production",
2018-09-10 05:57:12 +02:00
} );
2018-09-06 13:38:26 +02:00
} );
2018-09-06 06:52:02 +02:00
/* Main */
2018-09-06 09:32:55 +02:00
app.listen( 80, () => {
2018-09-06 06:52:02 +02:00
2018-09-06 09:32:55 +02:00
console.log( "The PEG.js website is running on the localhost in %s mode...", app.get( "env" ) );
2018-09-06 06:52:02 +02:00
} );