UNPKG

@js-primer/local-server

Version:
74 lines (72 loc) 2.99 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalServer = void 0; const path = __importStar(require("path")); const connect = require("connect"); const serveStatic = require("serve-static"); const detectPort = require("detect-port"); const response_log_1 = require("./middlewares/response-log"); const chalk_1 = __importDefault(require("chalk")); const log_symbols_1 = __importDefault(require("log-symbols")); class LocalServer { constructor(options) { this.rootDir = path.normalize(path.resolve(options.rootDir || ".")); this.port = options.port || 3000; } get appName() { return this.rootDir ? path.basename(this.rootDir) : "app"; } start() { return detectPort(this.port).then(newPort => { if (this.port !== newPort) { console.log(`${log_symbols_1.default.warning} ポート番号:${this.port}はすでに使われています。利用できる別のポート番号を探索中です。`); } const serve = serveStatic(this.rootDir, { index: ["index.html", "index.htm"] }); this.server = connect() .use(response_log_1.responseLog()) .use(serve) .listen(newPort, () => { console.log(` ${chalk_1.default.underline(this.appName)}のローカルサーバを起動しました。 次のURLをブラウザで開いてください。 URL: ${chalk_1.default.underline(`http://localhost:${newPort}`)} Ctrl+Cのショートカットを押下することでローカルサーバを終了できます。 `); }); return this.server; }); } stop() { if (!this.server) { return; } this.server.close(); console.log(` ${chalk_1.default.underline(this.appName)}のローカルサーバを終了しました。 `); } } exports.LocalServer = LocalServer; //# sourceMappingURL=local-server.js.map