@dashai/cli 0.3.1 → 0.3.2

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.
package/dist/bin.js CHANGED
@@ -2748,7 +2748,13 @@ async function devCommand(options) {
2748
2748
  return 1;
2749
2749
  }
2750
2750
  const apiUrl = options.apiUrl ?? process.env.DASHWISE_API_URL ?? savedConfig?.apiUrl ?? "http://localhost:3000";
2751
- const port = options.port ?? DEFAULT_NEXT_PORT;
2751
+ let port;
2752
+ try {
2753
+ port = await resolveDevPort(options.port);
2754
+ } catch (err) {
2755
+ fail(err.message);
2756
+ return 1;
2757
+ }
2752
2758
  const explicitInstallationId = options.installationId ?? process.env.DASHWISE_INSTALLATION_ID;
2753
2759
  const reuseDevSessionId = process.env.DASHWISE_DEV_SESSION_ID;
2754
2760
  const moduleEntry = manifestSlug !== void 0 ? readModuleEntry(manifestSlug) : null;
@@ -2951,6 +2957,44 @@ function computeCallbackOrigins(port, extra) {
2951
2957
  const def = `http://localhost:${port}`;
2952
2958
  return Array.from(/* @__PURE__ */ new Set([def, ...extra]));
2953
2959
  }
2960
+ var PORT_PROBE_LIMIT = 10;
2961
+ async function resolveDevPort(requested) {
2962
+ if (requested !== void 0) {
2963
+ if (await isPortFree(requested)) return requested;
2964
+ const suggestion = await findFirstFreePort(requested + 1, PORT_PROBE_LIMIT);
2965
+ const suggestionHint = suggestion !== null ? `Try \`dashwise dev --port ${suggestion}\`.` : `No free port found between ${requested + 1} and ${requested + PORT_PROBE_LIMIT} either \u2014 check what's listening with \`lsof -i :${requested}\`.`;
2966
+ throw new Error(
2967
+ `Port ${requested} is already in use. ${suggestionHint}`
2968
+ );
2969
+ }
2970
+ const found = await findFirstFreePort(DEFAULT_NEXT_PORT, PORT_PROBE_LIMIT);
2971
+ if (found === null) {
2972
+ throw new Error(
2973
+ `No free port found between ${DEFAULT_NEXT_PORT} and ${DEFAULT_NEXT_PORT + PORT_PROBE_LIMIT - 1}. Free a port (e.g. \`lsof -i :${DEFAULT_NEXT_PORT}\`) or pass \`--port <N>\` explicitly.`
2974
+ );
2975
+ }
2976
+ return found;
2977
+ }
2978
+ async function isPortFree(port) {
2979
+ const { createServer } = await import('net');
2980
+ return new Promise((resolve11) => {
2981
+ const server = createServer();
2982
+ server.once("error", () => {
2983
+ resolve11(false);
2984
+ });
2985
+ server.once("listening", () => {
2986
+ server.close(() => resolve11(true));
2987
+ });
2988
+ server.listen(port, "0.0.0.0");
2989
+ });
2990
+ }
2991
+ async function findFirstFreePort(start, limit) {
2992
+ for (let i = 0; i < limit; i++) {
2993
+ const port = start + i;
2994
+ if (await isPortFree(port)) return port;
2995
+ }
2996
+ return null;
2997
+ }
2954
2998
  async function resolveTargetWorkspace(apiUrl, apiToken, flag) {
2955
2999
  const workspaces = await apiRequest(
2956
3000
  "GET",
@@ -5944,7 +5988,7 @@ fieldCmd.command("set-type <table-slug> <field-slug> <new-type>").description(
5944
5988
  }
5945
5989
  })();
5946
5990
  function getVersion() {
5947
- return "0.3.1";
5991
+ return "0.3.2";
5948
5992
  }
5949
5993
  function parseIntOption(value) {
5950
5994
  const n = Number.parseInt(value, 10);