radiant-docs 0.1.65 → 0.1.66

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.
Files changed (2) hide show
  1. package/dist/index.js +53 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -8,10 +8,14 @@ import chokidar from "chokidar";
8
8
  import { fileURLToPath } from "url";
9
9
  import envPaths from "env-paths";
10
10
  import crypto from "crypto";
11
+ import net from "net";
11
12
  var __filename = fileURLToPath(import.meta.url);
12
13
  var __dirname = path.dirname(__filename);
13
14
  var paths = envPaths("radiant", { suffix: "" });
14
15
  var VERSION = "0.1.0";
16
+ var DEV_SERVER_HOST = "localhost";
17
+ var DEV_SERVER_PORT = 3e3;
18
+ var MAX_PORT = 65535;
15
19
  var lastErrorMessage = "";
16
20
  var lastErrorTime = 0;
17
21
  var ERROR_DEDUPE_MS = 2e3;
@@ -187,17 +191,33 @@ async function runAstroSync(cacheDir) {
187
191
  } catch {
188
192
  }
189
193
  }
190
- function startDevServer(cacheDir) {
191
- const child = spawn("npm", ["run", "dev"], {
192
- cwd: cacheDir,
193
- shell: true,
194
- env: {
195
- ...process.env,
196
- FORCE_COLOR: "1"
194
+ async function startDevServer(cacheDir) {
195
+ const port = await findAvailablePort(DEV_SERVER_PORT);
196
+ if (port !== DEV_SERVER_PORT) {
197
+ log.info(`Port ${DEV_SERVER_PORT} is busy, using port ${port}...`);
198
+ }
199
+ const child = spawn(
200
+ "npm",
201
+ [
202
+ "run",
203
+ "dev",
204
+ "--",
205
+ "--host",
206
+ DEV_SERVER_HOST,
207
+ "--port",
208
+ String(port)
209
+ ],
210
+ {
211
+ cwd: cacheDir,
212
+ shell: true,
213
+ env: {
214
+ ...process.env,
215
+ FORCE_COLOR: "1"
216
+ }
197
217
  }
198
- });
218
+ );
199
219
  let serverStarted = false;
200
- let serverUrl = "http://localhost:4321";
220
+ let serverUrl = `http://${DEV_SERVER_HOST}:${port}`;
201
221
  const processOutput = (data, isError = false) => {
202
222
  const output = data.toString();
203
223
  const lines = output.split("\n");
@@ -244,6 +264,29 @@ function startDevServer(cacheDir) {
244
264
  });
245
265
  return child;
246
266
  }
267
+ async function findAvailablePort(startPort) {
268
+ for (let port = startPort; port <= MAX_PORT; port++) {
269
+ if (await isPortAvailable(port)) {
270
+ return port;
271
+ }
272
+ }
273
+ throw new Error(`No available port found from ${startPort} to ${MAX_PORT}`);
274
+ }
275
+ function isPortAvailable(port) {
276
+ return new Promise((resolve) => {
277
+ const server = net.createServer();
278
+ server.unref();
279
+ server.once("error", () => {
280
+ resolve(false);
281
+ });
282
+ server.once("listening", () => {
283
+ server.close(() => {
284
+ resolve(true);
285
+ });
286
+ });
287
+ server.listen(port, DEV_SERVER_HOST);
288
+ });
289
+ }
247
290
  function watchUserContent(docsDir, contentDir, _onSync) {
248
291
  const watcher = chokidar.watch(docsDir, {
249
292
  ignored: [
@@ -334,7 +377,7 @@ async function run() {
334
377
  await runAstroSync(cacheDir);
335
378
  log.success("Content synced.");
336
379
  log.info("Starting dev server...");
337
- const devServer = startDevServer(cacheDir);
380
+ const devServer = await startDevServer(cacheDir);
338
381
  const watcher = watchUserContent(docsDir, contentDir, async () => {
339
382
  await runAstroSync(cacheDir);
340
383
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "radiant-docs",
3
- "version": "0.1.65",
3
+ "version": "0.1.66",
4
4
  "description": "CLI tool for previewing Radiant documentation locally",
5
5
  "type": "module",
6
6
  "bin": {