Repository: gatsbyjs/gatsby
File: packages/gatsby/src/commands/develop.ts (TypeScript)

Code before:
        if (this.process) {
          this.process.removeAllListeners()
        }
        this.process = undefined
        resolve()
      })
    })
  }
  onMessage(callback: (msg: any) => void): void {
    if (!this.process) {
      throw new Error(`Trying to attach message handler before process starter`)
    }
    this.process.on(`message`, callback)
  }
  onExit(
    callback: (code: number | null, signal: NodeJS.Signals | null) => void
  ): void {
    if (!this.process) {
      throw new Error(`Trying to attach exit handler before process starter`)
    }
    this.process.on(`exit`, callback)
  }
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  send(msg: any): void {
    if (!this.process) {
      throw new Error(`Trying to attach exit handler before process starter`)
    }

    this.process.send(msg)
  }
}

let isRestarting

// checks if a string is a valid ip
const REGEX_IP = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/

module.exports = async (program: IProgram): Promise<void> => {
  // In some cases, port can actually be a string. But our codebase is expecting it to be a number.
  // So we want to early just force it to a number to ensure we always act on a correct type.
  program.port = parseInt(program.port + ``, 10)
  const developProcessPath = slash(require.resolve(`./develop-process`))

  try {
    program.port = await detectPortInUseAndPrompt(program.port)
  } catch (e) {
    if (e.message === `USER_REJECTED`) {
      process.exit(0)
    }

    throw e

Review comment (suggestion):
NIT :)
```suggestion
      throw new Error(`Trying to attach exit handler before process started`)
```

Code after:
        if (this.process) {
          this.process.removeAllListeners()
        }
        this.process = undefined
        resolve()
      })
    })
  }
  onMessage(callback: (msg: any) => void): void {
    if (!this.process) {
      throw new Error(`Trying to attach message handler before process started`)
    }
    this.process.on(`message`, callback)
  }
  onExit(
    callback: (code: number | null, signal: NodeJS.Signals | null) => void
  ): void {
    if (!this.process) {
      throw new Error(`Trying to attach exit handler before process started`)
    }
    this.process.on(`exit`, callback)
  }
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  send(msg: any): void {
    if (!this.process) {
      throw new Error(`Trying to send a message before process started`)
    }

    this.process.send(msg)
  }
}

let isRestarting

// checks if a string is a valid ip
const REGEX_IP = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/

module.exports = async (program: IProgram): Promise<void> => {
  // In some cases, port can actually be a string. But our codebase is expecting it to be a number.
  // So we want to early just force it to a number to ensure we always act on a correct type.
  program.port = parseInt(program.port + ``, 10)
  const developProcessPath = slash(require.resolve(`./develop-process`))

  try {
    program.port = await detectPortInUseAndPrompt(program.port)
  } catch (e) {
    if (e.message === `USER_REJECTED`) {
      process.exit(0)
    }

    throw e