UNPKG

1.26 kBJavaScriptView Raw
1// Packages
2const {copy} = require('copy-paste')
3const ip = require('ip')
4const chalk = require('chalk')
5const boxen = require('boxen')
6
7const copyToClipboard = async text => {
8 try {
9 await copy(text)
10 return true
11 } catch (err) {
12 return false
13 }
14}
15
16module.exports = async (server, inUse) => {
17 const details = server.address()
18 const ipAddress = ip.address()
19 const url = `http://${ipAddress}:${details.port}`
20
21 const isTTY = process.stdout.isTTY
22
23 process.on('SIGINT', () => {
24 server.close()
25 process.exit(0)
26 })
27
28 if (!process.env.NOW) {
29 let message = chalk.green('Micro is running!')
30
31 if (inUse) {
32 message += ' ' + chalk.red(`(on port ${inUse.open},` +
33 ` because ${inUse.old} is already in use)`)
34 }
35
36 message += '\n\n'
37
38 const localURL = `http://localhost:${details.port}`
39
40 message += `• ${chalk.bold('Local: ')} ${localURL}\n`
41 message += `• ${chalk.bold('On Your Network: ')} ${url}\n\n`
42
43 if (isTTY) {
44 const copied = await copyToClipboard(localURL)
45
46 if (copied) {
47 message += `${chalk.grey('Copied local address to clipboard!')}`
48 }
49 }
50
51 console.log(boxen(message, {
52 padding: 1,
53 borderColor: 'green',
54 margin: 1
55 }))
56 }
57}