Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set default port to 3020 #37

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions cli/dev/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,33 @@ import { getLocalFileDependencies } from "lib/dependency-analysis/getLocalFileDe
import { installNodeModuleTypesForSnippet } from "../../lib/dependency-analysis/installNodeModuleTypesForSnippet"
import { EventsWatcher } from "../../lib/server/EventsWatcher"
import { DevServer } from "./DevServer"
import * as net from "node:net"

export const registerDev = (program: Command) => {
program
.command("dev")
.description("Start development server for a snippet")
.argument("[file]", "Path to the snippet file")
.option("-p, --port <number>", "Port to run server on", "3000")
.option("-p, --port <number>", "Port to run server on", "3020")
.action(async (file: string, options: { port: string }) => {
const port = parseInt(options.port)
let port = parseInt(options.port)

const isPortAvailable = (port: number): Promise<boolean> => {
return new Promise((resolve) => {
const server = net.createServer()
server.once("error", () => resolve(false))
server.once("listening", () => {
server.close(() => resolve(true))
})
server.listen(port)
})
}

while (!(await isPortAvailable(port))) {
console.log(`Port ${port} is in use, trying port ${port + 1}...`)
port += 1
}

let absolutePath: string

if (file) {
Expand Down
2 changes: 1 addition & 1 deletion lib/server/createHttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import pkg from "../../package.json"
import winterspecBundle from "@tscircuit/file-server/dist/bundle.js"
import { getIndex } from "../site/getIndex"

export const createHttpServer = async (port = 3000) => {
export const createHttpServer = async (port = 3020) => {
const fileServerHandler = getNodeHandler(winterspecBundle as any, {})

const server = http.createServer(async (req, res) => {
Expand Down
36 changes: 36 additions & 0 deletions tests/test3-dev-server-port.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { test, expect, afterEach } from "bun:test"
import { DevServer } from "cli/dev/DevServer"
import { getTestFixture } from "tests/fixtures/get-test-fixture"
import * as http from "node:http"

test("test3 dev server port handling", async () => {
const { tempDirPath } = await getTestFixture({
vfs: {
"snippet.tsx": `
export const MyCircuit = () => (
<board width="10mm" height="10mm">
<chip name="U1" footprint="soic8" />
</board>
)
`,
},
})

const server = http.createServer(() => {}).listen(3020)

const devServer = new DevServer({
port: 3021,
componentFilePath: `${tempDirPath}/snippet.tsx`,
})
await devServer.start()

await new Promise((resolve) => setTimeout(resolve, 1000))

const res = await fetch(`http://localhost:3021`)
expect(res.status).toBe(200)

afterEach(async () => {
server.close()
await devServer.stop()
})
})
Loading