Advertisement
djbob2000

Untitled

Jan 20th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import "module-alias/register";
  2. import { createServer } from "node:http";
  3. import { parse } from "url";
  4. import next from "next";
  5. import { isProd } from "@/lib/environments";
  6. import { envSettingKeys } from "@root/infra/env-settings";
  7. import { getNumberEnvSetting, getStringEnvSetting } from "@root/infra/env-functions";
  8. import * as dotenv from "dotenv";
  9. import { initSocket } from "@server/fd/fd-toolbox-web/services/web-socket-server";
  10. import { initializeInternalMetaCache } from "./server/meta/fd-meta/resources/init-internal-resource-metas-from-json";
  11. import chokidar from "chokidar";
  12.  
  13. dotenv.config();
  14.  
  15. const dev = process.env.NODE_ENV !== "production";
  16. const protocol = getStringEnvSetting(envSettingKeys.protocol) || "http";
  17. const hostname = isProd() ? getStringEnvSetting(envSettingKeys.hostname) || "localhost" : "localhost";
  18. const port = getNumberEnvSetting(envSettingKeys.port) ?? 3000;
  19. const url = `${protocol}://${hostname}:${port}`;
  20.  
  21. const pathToWatch = "/Users/air/develop/saas-kit/";
  22. const watcher = chokidar.watch(pathToWatch, {
  23.     persistent: true,
  24.     ignoreInitial: true,
  25.     ignored: /node_modules/,
  26.     depth: 99,
  27. });
  28.  
  29. const app = next({ dev, hostname, port });
  30. const handler = app.getRequestHandler();
  31.  
  32. async function run() {
  33.     watcher
  34.         .on("add", (path: string) => console.log(`Файл добавлен: ${path}`))
  35.         .on("change", (path: string) => console.log(`Файл изменен: ${path}`))
  36.         .on("unlink", (path: string) => console.log(`Файл удален: ${path}`))
  37.         .on("addDir", (path: string) => console.log(`Папка добавлена: ${path}`))
  38.         .on("unlinkDir", (path: string) => console.log(`Папка удалена: ${path}`))
  39.         .on("error", (error: any) => console.error(`Ошибка: ${error}`));
  40.     await app.prepare();
  41.  
  42.     const httpServer = createServer((req, res) => {
  43.         const parsedUrl = parse(req.url!, true);
  44.         handler(req, res, parsedUrl);
  45.     });
  46.  
  47.     initSocket(httpServer);
  48.  
  49.     httpServer
  50.         .once("error", (err) => {
  51.             console.error(err);
  52.             process.exit(1);
  53.         })
  54.         .listen(port, () => {
  55.             console.log(`> Ready on ${url}`);
  56.         });
  57.  
  58.     await initializeInternalMetaCache();
  59. }
  60.  
  61. run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement