UNPKG

feli

Version:

library for making quickly your mini-server for your front-end only web-app

176 lines (173 loc) 4.47 kB
#!/usr/bin/env node // src/index.ts import yargs from "yargs"; import { hideBin } from "yargs/helpers"; import fs from "fs"; import { Hono } from "hono"; import { serve } from "@hono/node-server"; import { serveStatic } from "@hono/node-server/serve-static"; 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.4", 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: { "@hono/node-server": "^1.19.7", "fs-extra": "^11.3.3", "get-port": "^7.1.0", hono: "^4.11.3", open: "^11.0.0", yargs: "^18.0.0" }, devDependencies: { "@eslint/js": "^9.10.0", "@types/eslint__js": "^8.42.3", "@types/fs-extra": "^11.0.4", "@types/yargs": "^17.0.35", eslint: "^9.39.2", "eslint-config-prettier": "^10.1.8", "npm-run-all2": "^8.0.4", prettier: "^3.7.4", rimraf: "^6.1.2", tsup: "^8.5.1", typescript: "^5.9.3", "typescript-eslint": "^8.50.1", vitest: "^4.0.16" } }; // 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 = new Hono(); if (aDirectory !== "") { app.use("*", serveStatic({ root: aDirectory })); } serve( { fetch: app.fetch, port: portnumber }, (info) => { console.log( `${package_default.name} serves on port ${info.port} 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