2021-04-01 10:30:24 +08:00
|
|
|
import { builtinModules } from 'module'
|
2021-02-17 22:27:40 +08:00
|
|
|
import { get } from 'http'
|
2021-02-18 14:45:20 +08:00
|
|
|
import { green } from 'chalk'
|
2021-02-17 22:27:40 +08:00
|
|
|
|
|
|
|
/** 轮询监听 vite 启动 */
|
|
|
|
export function waitOn(arg0: { port: string | number; interval?: number; }) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const { port, interval = 149 } = arg0
|
2021-02-18 14:45:20 +08:00
|
|
|
const url = `http://localhost:${port}`
|
2021-02-17 22:27:40 +08:00
|
|
|
let counter = 0
|
|
|
|
const timer: NodeJS.Timer = setInterval(() => {
|
2021-02-18 14:45:20 +08:00
|
|
|
get(url, res => {
|
2021-02-17 22:27:40 +08:00
|
|
|
clearInterval(timer)
|
2021-02-18 14:45:20 +08:00
|
|
|
console.log('[waitOn]', green(`"${url}" are already responsive.`), `(${res.statusCode}: ${res.statusMessage})`)
|
2021-02-17 22:27:40 +08:00
|
|
|
resolve(res.statusCode)
|
|
|
|
}).on('error', err => {
|
2021-02-18 14:45:20 +08:00
|
|
|
console.log('[waitOn]', `counter: ${counter++}`)
|
2021-02-17 22:27:40 +08:00
|
|
|
})
|
|
|
|
}, interval)
|
|
|
|
})
|
|
|
|
}
|
2021-02-18 16:11:08 +08:00
|
|
|
|
2021-05-15 16:30:59 +08:00
|
|
|
/** node.js builtins module */
|
2021-04-01 10:30:24 +08:00
|
|
|
export const builtins = () => builtinModules.filter(x => !/^_|^(internal|v8|node-inspect)\/|\//.test(x))
|