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

Added useful tip with local addresses #8

Merged
merged 1 commit into from
Apr 24, 2020
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
7 changes: 6 additions & 1 deletion bin/vite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
const argv = require('minimist')(process.argv.slice(2))
const getIPv4AddressList = require('../dist/utils').getIPv4AddressList

if (argv._[0] === 'build') {
require('../dist').build(argv)
Expand All @@ -21,7 +22,11 @@ if (argv._[0] === 'build') {
})

server.on('listening', () => {
console.log(`Running at http://localhost:${port}`)
console.log(`Running at:`)
getIPv4AddressList().forEach((ip) => {
console.log(` > http://${ip}:${port}`)
})
console.log(' ')
})

server.listen(port)
Expand Down
16 changes: 16 additions & 0 deletions src/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { promises as fs } from 'fs'
import LRUCache from 'lru-cache'
import os from 'os'

interface CacheEntry {
lastModified: number
Expand All @@ -25,3 +26,18 @@ export async function cachedRead(path: string, encoding?: string) {
})
return content
}

export function getIPv4AddressList(): string[] {
const networkInterfaces = os.networkInterfaces()
let result: string[] = []

Object.keys(networkInterfaces).forEach((key) => {
const ips = (networkInterfaces[key] || [])
.filter((details) => details.family === 'IPv4')
.map((detail) => detail.address)

result = result.concat(ips)
})

return result
}