-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
83 lines (71 loc) · 1.96 KB
/
utils.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const os = require('os')
const QRCode = require("./lib/qrcode/lib/index")
const fs = require("fs-extra")
const path = require('path')
const webpack = require('webpack')
const { merge } = require('webpack-merge')
const { execSync } = require('child_process')
const openQrcode = (dataString, cb) => {
QRCode.toString(dataString, {
type: 'terminal',
small: true,
}).then((qr) => {
cb && cb(qr)
})
}
const getMypcip = (family = 'IPv4') => {
let nets = os.networkInterfaces()
for (let key in nets) {
let net = nets[key]
for (let item of net) {
if (item.family === family && !item.internal) {
return item.address
}
}
}
}
const isVue = () => {
try {
execSync('vue')
return true
} catch (error) {
return false
}
}
// 合并热更新
const mergeHotWebackConfig = (webpackConfigPath) => {
let dir = path.resolve(process.cwd(), webpackConfigPath)
let hotConfig = {
plugins: [
new webpack.HotModuleReplacementPlugin()
],
stats: "none"
}
let baseConfig = require(dir)
let entry = baseConfig.entry
let hotMiddlewareScript = `${path.resolve(require.resolve('webpack-hot-middleware'), '../client')}?path=/__webpack_hmr&timeout=20000&reload=true`;
if (typeof entry === 'string') {
entry = [].concat(entry)
}
if (Array.isArray(entry)) {
entry.push(hotMiddlewareScript)
}
if (Object.prototype.toString.call(entry) === "[object Object]") {
for (let key in entry) {
entry[key] = [].concat(entry[key])
entry[key].push(hotMiddlewareScript)
}
}
baseConfig.entry = entry
baseConfig = merge(baseConfig, hotConfig)
return baseConfig
}
const getWebpackConfig = (webpackConfigPath) => {
return mergeHotWebackConfig(webpackConfigPath)
}
module.exports = {
getMypcip,
openQrcode,
isVue,
getWebpackConfig
}