@tsed/cli 7.5.0-rc.3 → 7.5.0-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,10 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { existsSync } from "node:fs";
3
+ import path from "node:path";
3
4
  import process from "node:process";
4
- import { fileURLToPath } from "node:url";
5
- import { normalizePath } from "@tsed/cli-core";
6
- import { logger } from "@tsed/di";
5
+ import { fileURLToPath, pathToFileURL } from "node:url";
6
+ const runnerFile = fileURLToPath(import.meta.url);
7
+ const configFile = path.resolve(path.dirname(runnerFile), "../../vite.config.ts");
7
8
  const RUN_MODE = "TSED_VITE_RUN_MODE";
8
9
  function parseWatchValue(args) {
9
10
  if (args.includes("--no-watch")) {
@@ -30,7 +31,6 @@ function parseWatchValue(args) {
30
31
  return true;
31
32
  }
32
33
  function assertViteProject() {
33
- const configFile = normalizePath("vite.config.ts");
34
34
  if (!existsSync(configFile)) {
35
35
  throw new Error("tsed dev is only available for ViteRuntime projects. Missing vite.config.ts in the current directory.");
36
36
  }
@@ -39,9 +39,9 @@ async function createViteDevServer() {
39
39
  // @ts-ignore
40
40
  const { createServer } = await import("vite");
41
41
  return createServer({
42
- configFile: normalizePath("vite.config.ts"),
42
+ configFile,
43
43
  server: {
44
- middlewareMode: true,
44
+ middlewareMode: "ssr",
45
45
  hmr: false,
46
46
  ws: false
47
47
  }
@@ -59,30 +59,40 @@ async function runViteApp() {
59
59
  await new Promise(() => { });
60
60
  }
61
61
  async function runViteController(rawArgs) {
62
- const runnerFile = fileURLToPath(import.meta.url);
63
62
  const watch = parseWatchValue(rawArgs);
64
63
  const vite = await createViteDevServer();
65
64
  let childProcess;
65
+ let childStarted = false;
66
66
  let restarting = false;
67
67
  let queued = false;
68
68
  const startChild = () => {
69
- const cliEntry = process.argv[1];
70
- childProcess = spawn(process.execPath, cliEntry ? [cliEntry, "dev", ...rawArgs] : [runnerFile, ...rawArgs], {
69
+ if (childStarted) {
70
+ return;
71
+ }
72
+ childStarted = true;
73
+ childProcess = spawn(process.execPath, [runnerFile, ...rawArgs], {
71
74
  env: {
72
75
  ...process.env,
73
76
  [RUN_MODE]: "app"
74
77
  },
75
78
  stdio: "inherit"
76
79
  });
80
+ childProcess.once("exit", () => {
81
+ childStarted = false;
82
+ childProcess = undefined;
83
+ });
77
84
  };
78
85
  const stopChild = async () => {
79
- if (!childProcess || childProcess.killed) {
86
+ if (!childProcess || childProcess.killed || childProcess.exitCode !== null || childProcess.signalCode !== null) {
87
+ childStarted = false;
88
+ childProcess = undefined;
80
89
  return;
81
90
  }
82
91
  await new Promise((resolve) => {
83
92
  childProcess.once("exit", resolve);
84
93
  childProcess.kill("SIGTERM");
85
94
  });
95
+ childStarted = false;
86
96
  };
87
97
  const restartChild = async (reason, file = "") => {
88
98
  if (restarting) {
@@ -91,7 +101,7 @@ async function runViteController(rawArgs) {
91
101
  }
92
102
  restarting = true;
93
103
  const suffix = file ? `: ${file}` : "";
94
- logger().info(`[tsed-dev] restart (${reason})${suffix}`);
104
+ vite.config.logger.info(`[tsed-dev] restart (${reason})${suffix}`);
95
105
  await stopChild();
96
106
  startChild();
97
107
  restarting = false;
@@ -113,6 +123,12 @@ async function runViteController(rawArgs) {
113
123
  vite.watcher.once("ready", () => {
114
124
  startChild();
115
125
  });
126
+ // Fallback: some environments can miss/lag watcher "ready" when Vite re-optimizes deps.
127
+ setTimeout(() => {
128
+ if (!childStarted) {
129
+ startChild();
130
+ }
131
+ }, 2500);
116
132
  const shutdown = async () => {
117
133
  await stopChild();
118
134
  await vite.close();
@@ -129,3 +145,9 @@ export async function dev(rawArgs = process.argv.slice(2)) {
129
145
  }
130
146
  await runViteController(rawArgs);
131
147
  }
148
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
149
+ dev().catch((error) => {
150
+ console.error(error);
151
+ process.exit(1);
152
+ });
153
+ }