-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from stevent-team/refactor/express
Refactor as node library using express
- Loading branch information
Showing
17 changed files
with
2,751 additions
and
271 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,19 +1,5 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
|
||
# Added by cargo | ||
|
||
/target | ||
|
||
|
||
# Custom | ||
static/ | ||
.parcel-cache | ||
.vscode | ||
dist | ||
node_modules | ||
.DS_Store |
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,4 @@ | ||
{ | ||
"extends": "@parcel/config-default", | ||
"resolvers": ["parcel-resolver-ts-base-url", "..."] | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,56 @@ | ||
#!/usr/bin/env node | ||
import yargs from 'yargs' | ||
|
||
import { loadRoutesFile } from 'routes' | ||
import startServer from 'server' | ||
import path from 'path' | ||
|
||
const serve = async ({ target, routes, host, port, index }) => { | ||
// Load routes | ||
const resolvedRoutes = routes ? path.resolve(__dirname, routes) : '' | ||
const routesMap = routes ? (await loadRoutesFile(resolvedRoutes) | ||
.catch(() => { throw new Error(`Failed to load routes file: ${resolvedRoutes}`) })) : {} | ||
|
||
if (!Object.keys(routesMap)?.length) { | ||
console.warn('No dynamic routes configured') | ||
} else { | ||
console.log(`🪴 Configured ${Object.keys(routesMap).length} dynamic route(s)`) | ||
} | ||
|
||
// Start server | ||
return startServer({ | ||
host, | ||
port, | ||
target, | ||
routes: routesMap, | ||
index: path.join(target, index), | ||
}) | ||
} | ||
|
||
yargs | ||
.scriptName("epoxy") | ||
.usage('$0 serve <target> [routes] [options]') | ||
.command(['serve <target> [routes]', '$0 <target>'], 'Serve the provided static folder', (yargs) => { | ||
yargs | ||
.positional('target', { describe: 'Path to static directory', require: true }) | ||
.positional('routes', { describe: 'Path to routes script' }) | ||
}, serve) | ||
.option('port', { | ||
alias: 'p', | ||
type: 'string', | ||
description: 'port to use for http server', | ||
default: 8080, | ||
}) | ||
.option('host', { | ||
alias: 'h', | ||
type: 'string', | ||
description: 'host to use for http server', | ||
default: '0.0.0.0', | ||
}) | ||
.option('index', { | ||
alias: 'i', | ||
type: 'string', | ||
description: 'path to index html inside of target', | ||
default: 'index.html', | ||
}) | ||
.argv |
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,29 @@ | ||
{ | ||
"name": "epoxy", | ||
"version": "1.0.0", | ||
"description": "Lightweight server-side per-route html injection", | ||
"repository": "https://github.com/stevent-team/epoxy", | ||
"author": "Stevent", | ||
"license": "MIT", | ||
"private": false, | ||
"bin": "./dist/index.js", | ||
"main": "./dist/index.js", | ||
"source": "./index.ts", | ||
"scripts": { | ||
"build": "parcel build" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^17.0.33", | ||
"nodemon": "^2.0.16", | ||
"parcel": "^2.5.0", | ||
"parcel-resolver-ts-base-url": "^1.1.5", | ||
"typescript": "^4.6.4" | ||
}, | ||
"dependencies": { | ||
"@types/express": "^4.17.13", | ||
"cors": "^2.8.5", | ||
"dotenv": "^16.0.1", | ||
"express": "^4.18.1", | ||
"yargs": "^17.5.1" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
export const injectHTML = (htmlText, { head, body }) => { | ||
htmlText = inject(htmlText, '</head>', head) | ||
htmlText = inject(htmlText, '</body>', body) | ||
return htmlText | ||
} | ||
|
||
const inject = (text, on, insert) => | ||
insert ? text.split(on).join(`${insert}${on}`) : text |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,4 @@ | ||
export const loadRoutesFile = async routes => { | ||
const module = await import(routes) | ||
return module?.default | ||
} |
Oops, something went wrong.