-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
63 lines (57 loc) · 2.02 KB
/
index.ts
File metadata and controls
63 lines (57 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { type NetworkInterfaceInfo, networkInterfaces } from 'node:os'
import type { RspackPluginInstance, Compiler } from '@rspack/core'
import type { RsbuildPlugin } from '@rsbuild/core'
import { Clipboard } from '@napi-rs/clipboard'
export class rspackCliCopyPlugin implements RspackPluginInstance {
apply(compiler: Compiler) {
compiler.hooks.afterCompile.tap('NetworkCopyPlugin', async () => {
if (compiler.options.mode === 'development' && !compiler.modifiedFiles?.size) {
const localIPv4 = getIpv4Interfaces()[0]
const port = compiler.options.devServer?.port
const v = `http://${localIPv4}:${port}`
print(v)
}
})
}
}
export const rsbuildCliCopyPlugin = (): RsbuildPlugin => ({
name: 'rsbuildCliCopyPlugin',
async setup(api) {
const localIPv4 = getIpv4Interfaces()[0]
api.onAfterStartDevServer(server => {
const v = `http://${localIPv4}:${server.port}`
print(v)
})
}
})
function print(v: string) {
try {
const clipboard = new Clipboard()
clipboard.setText(v)
console.log(`\n ${cyan('Copied to clipboard Network URL:')} ${v} \n`)
} catch (error) {
console.error(`\n ${red('Failed to copy to clipboard Network URL:')} ${JSON.stringify(error)} \n`)
}
}
function cyan(str: string) {
return `\x1b[36m${str}\x1b[0m`
}
function red(str: string) {
return `\x1b[31m${str}\x1b[0m`
}
const getIpv4Interfaces = () => {
const interfaces = networkInterfaces()
const ipv4Interfaces: Map<string, NetworkInterfaceInfo> = new Map()
for (const key of Object.keys(interfaces)) {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
for (const detail of interfaces[key]!) {
if (detail.internal) continue
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
const familyV4Value = typeof detail.family === 'string' ? 'IPv4' : 4
if (detail.family === familyV4Value && !ipv4Interfaces.has(detail.address)) {
ipv4Interfaces.set(detail.address, detail)
}
}
}
return Array.from(ipv4Interfaces.keys())
}