-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Collapsible Treeview Implementation
Set up cli and local webpage. Initial pass at logic based on the 'Rentals' example.
- Loading branch information
1 parent
8e77b36
commit c85234b
Showing
8 changed files
with
5,386 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env node | ||
"use strict"; | ||
|
||
// NPM imports | ||
const path = require("path"); | ||
const util = require("util"); | ||
const fs = require("fs"); | ||
const readFile = util.promisify(fs.readFile); | ||
const commander = require("commander"); | ||
const express = require("express"); | ||
|
||
// Set up Commander | ||
const program = new commander.Command(); | ||
program | ||
.name("morphir-elm treeview") | ||
.description("Start up a web server and expose treeview through a web UI") | ||
.option("-p, --port <port>", "Port to bind the web server to.", "3000") | ||
.option("-o, --host <host>", "Host to bind the web server to.", "localhost") | ||
.option( | ||
"-i, --project-dir <path>", | ||
"Root directory of the project where morphir.json is located.", | ||
"." | ||
) | ||
.parse(process.argv); | ||
|
||
const app = express(); | ||
const port = program.opts().port; | ||
|
||
const webDir = path.join(__dirname, "treeview", "dist"); | ||
|
||
app.use(express.static(webDir, { index: false })); | ||
app.use(express.json({ limit: "100mb" })); | ||
|
||
app.get( | ||
"/", | ||
wrap(async (req, res, next) => { | ||
res.setHeader("Content-type", "text/html"); | ||
res.send(await indexHtmlWithVersion()); | ||
}) | ||
); | ||
|
||
createSimpleGetJsonApi(app, "morphir.json"); | ||
createSimpleGetJsonApi(app, "morphir-ir.json"); | ||
|
||
app.get( | ||
"*", | ||
wrap(async (req, res, next) => { | ||
res.setHeader("Content-type", "text/html"); | ||
res.send(await indexHtmlWithVersion()); | ||
}) | ||
); | ||
|
||
app.listen(port, program.opts().host, () => { | ||
console.log( | ||
`Developer server listening at http://${program.opts().host}:${port}` | ||
); | ||
}); | ||
|
||
// --- Utility Functions --- | ||
|
||
function createSimpleGetJsonApi(app, filePath, defaultContent) { | ||
app.get( | ||
"/server/" + filePath, | ||
wrap(async (req, res, next) => { | ||
const jsonPath = path.join(program.opts().projectDir, filePath); | ||
try { | ||
const jsonContent = await readFile(jsonPath); | ||
res.send(JSON.parse(jsonContent.toString())); | ||
} catch (err) { | ||
if (defaultContent && err.code === "ENOENT") { | ||
// file does not exist, send default content | ||
res.send(defaultContent); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
}) | ||
); | ||
} | ||
|
||
async function indexHtmlWithVersion() { | ||
const packageJson = require(path.join(__dirname, "../package.json")); | ||
const _indexHtml = await readFile(path.join(webDir, "index.html"), "utf8"); | ||
return _indexHtml.replace( | ||
"__VERSION_NUMBER__", | ||
packageJson.version.toString() | ||
); | ||
} | ||
|
||
function wrap(fn) { | ||
return (...args) => fn(...args).catch(args[2]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.