feli
Version:
library for making quickly your mini-server for your front-end only web-app
168 lines (165 loc) • 4.3 kB
JavaScript
// src/index.ts
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import fs from "fs";
import express from "express";
import open from "open";
import getport from "get-port";
// package.json
var package_default = {
name: "feli",
description: "library for making quickly your mini-server for your front-end only web-app",
version: "1.0.1",
private: false,
repository: {
type: "git",
url: "git+https://github.com/charlyoleg2/feli_mono.git"
},
homepage: "https://charlyoleg2.github.io/feli_mono/",
author: "charlyoleg",
license: "ISC",
keywords: [
"web-app",
"web-ui",
"front-end",
"local installation"
],
type: "module",
exports: {
".": {
types: "./dist/index.d.ts",
default: "./dist/index.js"
}
},
files: [
"dist/",
"dist/**/*.d.ts",
"!dist/**/*.map",
"!dist/**/*.test.*",
"!dist/**/*.spec.*"
],
engines: {
node: ">=22.0.0"
},
tsup: {
entry: [
"src/index.ts"
],
format: "esm",
splitting: false,
dts: true,
sourcemap: true,
clean: true
},
prettier: {
useTabs: true,
singleQuote: true,
trailingComma: "none",
printWidth: 100,
plugins: [],
overrides: []
},
scripts: {
dev: "tsup --watch",
build: "tsup",
check: "tsc --noEmit",
pretty: "prettier --check .",
format: "prettier --write .",
lint: "eslint .",
"test:unit": "vitest",
"test:unit:once": "vitest --run",
ci: "run-s check build pretty lint test:unit:once",
clean: "rimraf node_modules build dist tmp",
"run-test": "node scr/test-feli.js"
},
dependencies: {
express: "^5.1.0",
"fs-extra": "^11.3.0",
"get-port": "^7.1.0",
open: "^10.1.2",
yargs: "^18.0.0"
},
devDependencies: {
"@eslint/js": "^9.10.0",
"@types/eslint__js": "^8.42.3",
"@types/express": "^5.0.3",
"@types/fs-extra": "^11.0.4",
"@types/yargs": "^17.0.33",
eslint: "^9.30.1",
"eslint-config-prettier": "^10.1.5",
"npm-run-all2": "^8.0.4",
prettier: "^3.6.2",
rimraf: "^6.0.1",
tsup: "^8.5.0",
typescript: "^5.8.3",
"typescript-eslint": "^8.35.1",
vitest: "^3.2.4"
}
};
// src/index.ts
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function mini_server(aDirectory, aBrowser, aPort) {
const chost = "127.0.0.1";
if (aDirectory !== "") {
if (!fs.existsSync(aDirectory)) {
const eMsg = `ERR339: Error, the path ${aDirectory} doesn't exist!`;
throw eMsg;
}
}
let portnumber = aPort;
if (portnumber === 0) {
portnumber = await getport();
}
const app = express();
if (aDirectory !== "") {
app.use(express.static(aDirectory));
}
app.listen(portnumber, chost, () => {
console.log(
`${package_default.name} serves on port ${portnumber} for host ${chost} the directory:
${aDirectory} ...`
);
});
await sleep(1e3);
const url = `http://localhost:${portnumber}`;
if (aBrowser) {
console.log(`Your browser should open automatically at ${url}`);
await open(url);
} else {
console.log(`Please, open the browser at ${url}`);
}
console.log("Press ctrl-c to stop this http-server ...");
}
async function feli_cli(defaultPublicDir, argv) {
const args = await yargs(hideBin(argv)).version(package_default.version).usage("Usage: $0 --port <port> --directoy <directory-path>").example([
[
"$0 -p 2022 -d MyPublic",
"run the webserver on port 2022 and serve the content of the folder MyPublic"
]
]).option("directory", {
alias: "d",
type: "string",
description: "path to the directory to be served.",
//default: `${__dirname}/webui`,
//default: path.join(scrDir, 'public')
default: defaultPublicDir
}).option("browser", {
alias: "b",
type: "boolean",
description: "Open the browser at the corresponding URL.",
default: true
}).option("port", {
alias: "p",
type: "number",
description: "port-number used by this web-server. If set to 0 an available port-number is automatically selected",
default: 0
}).strict().parseAsync();
await mini_server(args.directory, args.browser, args.port);
}
export {
feli_cli
};
//# sourceMappingURL=index.js.map