UNPKG

nix-api

Version:

<div align="center"> <img src="./docs/.vuepress/public/images/logo.png" width="200px"> </div>

160 lines (131 loc) 5.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _banner = _interopRequireDefault(require("./banner")); var _chalk = _interopRequireDefault(require("chalk")); var _chokidar = _interopRequireDefault(require("chokidar")); var _clear = _interopRequireDefault(require("clear")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var Nix = /*#__PURE__*/ function () { function Nix(_ref) { var port = _ref.port, application = _ref.application, routeManager = _ref.routeManager, basePath = _ref.basePath, _ref$watch = _ref.watch, watch = _ref$watch === void 0 ? false : _ref$watch, _ref$showDir = _ref.showDir, showDir = _ref$showDir === void 0 ? false : _ref$showDir; _classCallCheck(this, Nix); this.application = application; this.port = port; this.routeManager = routeManager; this.basePath = basePath; this.watch = watch; this.showDir = showDir; } _createClass(Nix, [{ key: "listenBaseFolder", value: function listenBaseFolder() { var watcher = _chokidar.default.watch(this.basePath, { ignored: /[\/\\]\./, persistent: true, ignoreInitial: true }); watcher.on('add', this.restart.bind(this)).on('unlink', this.restart.bind(this)); } }, { key: "restart", value: function () { var _restart = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee() { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: console.log(_chalk.default.yellow('Changed files!')); this.application.http.close(); this.serve(); case 3: case "end": return _context.stop(); } } }, _callee, this); })); function restart() { return _restart.apply(this, arguments); } return restart; }() }, { key: "start", value: function () { var _start = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee2() { return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: if (this.watch) this.listenBaseFolder(); this.application.app.use(this.routeManager.routes()); case 2: case "end": return _context2.stop(); } } }, _callee2, this); })); function start() { return _start.apply(this, arguments); } return start; }() }, { key: "serve", value: function () { var _serve = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee3() { var _this = this; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return _banner.default.start(); case 2: if (this.showDir) console.log(_chalk.default.yellow("Working in ".concat(this.basePath))); this.start(); this.application.http.listen(this.port, function () { console.log('\n'); console.log(_chalk.default.green("Server running in PORT ".concat(_this.port))); }); case 5: case "end": return _context3.stop(); } } }, _callee3, this); })); function serve() { return _serve.apply(this, arguments); } return serve; }() }]); return Nix; }(); var _default = Nix; exports.default = _default;