electron-vite-vue/scripts/utils.ts

27 lines
895 B
TypeScript
Raw Normal View History

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-11-09 09:00:12 +08:00
import chalk from 'chalk'
2021-02-17 22:27:40 +08:00
2021-11-09 09:00:12 +08:00
const TAG = '[waitOn]'
/** Listen http server startup */
2021-02-17 22:27:40 +08:00
export function waitOn(arg0: { port: string | number; interval?: number; }) {
2021-09-09 09:35:21 +08:00
return new Promise<number | undefined>(resolve => {
2021-02-17 22:27:40 +08:00
const { port, interval = 149 } = arg0
const url = `http://localhost:${port}`
2021-02-17 22:27:40 +08:00
let counter = 0
const timer: NodeJS.Timer = setInterval(() => {
get(url, res => {
2021-02-17 22:27:40 +08:00
clearInterval(timer)
2021-11-09 09:00:12 +08:00
console.log(TAG, chalk.green(`"${url}" are already responsive.`), `(${res.statusCode}: ${res.statusMessage})`)
2021-02-17 22:27:40 +08:00
resolve(res.statusCode)
}).on('error', err => {
2021-11-09 09:00:12 +08:00
console.log(TAG, `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))