Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
小浣熊撸代码
uni-app
提交
5024d510
U
uni-app
项目概览
小浣熊撸代码
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
5024d510
编写于
7月 09, 2020
作者:
d-u-a
提交者:
qiang
7月 09, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cli: add check update
上级
cb54f294
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
260 addition
and
33 deletion
+260
-33
packages/vue-cli-plugin-uni/lib/check-update.js
packages/vue-cli-plugin-uni/lib/check-update.js
+260
-33
未找到文件。
packages/vue-cli-plugin-uni/lib/check-update.js
浏览文件 @
5024d510
module
.
exports
=
async
function
checkUpdate
()
{
const
fs
=
require
(
'
fs
'
)
const
{
const
os
=
require
(
'
os
'
)
isInHBuilderX
const
path
=
require
(
'
path
'
)
}
=
require
(
'
@dcloudio/uni-cli-shared
'
)
if
(
isInHBuilderX
)
{
// 仅 cli 提供检测更新
class
Upate
{
return
constructor
()
{
this
.
_https
=
null
this
.
_crypto
=
null
this
.
_interval
=
this
.
DEFAULT_INTERVAL
this
.
_platform
=
process
.
env
.
UNI_PLATFORM
this
.
_fileData
=
{}
this
.
_compilerVersion
=
''
this
.
_isAlpha
=
false
this
.
_uniId
=
''
this
.
_appId
=
''
}
get
uniId
()
{
return
this
.
_uniId
}
set
uniId
(
value
)
{
this
.
_uniId
=
value
}
get
appId
()
{
return
this
.
_appId
}
set
appId
(
value
)
{
this
.
_appId
=
value
}
get
compilerVersion
()
{
return
this
.
_compilerVersion
}
set
compilerVersion
(
value
)
{
this
.
_compilerVersion
=
value
}
get
isAlpha
()
{
return
this
.
_isAlpha
}
set
isAlpha
(
value
)
{
this
.
_isAlpha
=
value
}
get
versionType
()
{
return
(
this
.
isAlpha
?
'
a
'
:
'
r
'
)
}
get
https
()
{
if
(
this
.
_https
==
null
)
{
this
.
_https
=
require
(
'
https
'
)
}
return
this
.
_https
}
get
crypto
()
{
if
(
this
.
_crypto
==
null
)
{
this
.
_crypto
=
require
(
'
crypto
'
)
}
return
this
.
_crypto
}
getBuildType
()
{
return
(
process
.
env
.
NODE_ENV
===
'
production
'
?
'
build
'
:
'
dev
'
)
}
async
check
()
{
await
this
.
readFile
()
const
fileData
=
this
.
_fileData
const
currentDate
=
Date
.
now
()
if
(
!
fileData
.
lastCheck
||
(
Math
.
abs
(
currentDate
-
fileData
.
lastCheck
)
>
this
.
_interval
))
{
this
.
_fileData
.
lastCheck
=
currentDate
this
.
checkUpdate
()
}
else
{
if
(
fileData
.
newVersion
&&
fileData
.
newVersion
!==
this
.
compilerVersion
)
{
console
.
log
()
console
.
log
(
fileData
.
note
)
}
}
await
this
.
update
()
}
async
readFile
()
{
const
filePath
=
await
this
.
getFilePath
()
let
fileData
=
{}
if
(
fs
.
existsSync
(
filePath
))
{
fileData
=
require
(
filePath
)
}
else
{
fileData
.
vid
=
this
.
_buildUUID
()
}
if
(
!
fileData
[
this
.
_platform
])
{
fileData
[
this
.
_platform
]
=
{}
}
this
.
_fileData
=
fileData
}
}
const
pkg
=
require
(
'
../package.json
'
)
async
update
()
{
const
checkForUpdate
=
require
(
'
update-check
'
)
const
bt
=
this
.
getBuildType
()
let
update
=
null
const
info
=
this
.
_fileData
[
this
.
_platform
]
const
count
=
parseInt
(
info
[
bt
]
||
0
)
info
[
bt
]
=
(
count
+
1
)
this
.
writeFile
()
}
async
writeFile
(
file
)
{
try
{
try
{
const
options
=
{}
const
filePath
=
await
this
.
getFilePath
()
if
(
pkg
.
version
.
indexOf
(
'
alpha
'
)
!==
-
1
)
{
const
content
=
JSON
.
stringify
(
file
||
this
.
_fileData
)
options
.
distTag
=
'
alpha
'
fs
.
writeFileSync
(
filePath
,
content
,
'
utf8
'
)
}
catch
(
error
)
{
}
}
checkUpdate
()
{
const
postData
=
JSON
.
stringify
({
id
:
this
.
getPostData
()
})
var
responseData
=
''
const
req
=
this
.
https
.
request
({
hostname
:
this
.
HOST
,
path
:
this
.
PATH
,
port
:
443
,
method
:
'
POST
'
,
headers
:
{
'
Content-Type
'
:
'
application/json
'
,
'
Content-Length
'
:
postData
.
length
}
}
update
=
await
checkForUpdate
(
pkg
,
options
)
},
(
res
)
=>
{
}
catch
(
err
)
{
res
.
setEncoding
(
'
utf8
'
)
// console.error(`检查更新失败: ${err}`)
res
.
on
(
'
data
'
,
(
chunk
)
=>
{
responseData
+=
chunk
})
res
.
on
(
'
end
'
,
()
=>
{
this
.
checkUpdateSuccess
(
JSON
.
parse
(
responseData
))
})
})
req
.
write
(
postData
)
req
.
end
()
}
}
if
(
update
)
{
getPostData
()
{
if
(
pkg
.
version
.
split
(
'
.
'
)[
0
]
!==
update
.
latest
.
split
(
'
.
'
)[
0
])
{
var
data
=
JSON
.
parse
(
JSON
.
stringify
(
this
.
_fileData
))
console
.
log
(
`发现 uni-app cli 新版本
${
update
.
latest
}
`
)
data
.
device
=
this
.
_getMD5
(
this
.
_getMac
())
console
.
log
(
`1.修改 package.json 中 @dcloudio 相关包版本为 ^
${
update
.
latest
}
`
)
data
.
appid
=
this
.
uniId
console
.
log
(
'
2.删除 package-lock.json 或 yarn.lock
'
)
data
.
vtype
=
this
.
versionType
console
.
log
(
'
3.执行 npm install 或 yarn
'
)
data
.
vcode
=
this
.
compilerVersion
delete
data
.
lastCheck
if
(
this
.
appId
)
{
data
[
this
.
_platform
].
appid
=
this
.
appId
}
if
(
data
.
appid
)
{
delete
data
.
vid
}
else
{
}
else
{
console
.
log
(
`发现 uni-app cli 新版本
${
update
.
latest
}
. 请执行 npm update 升级`
)
delete
data
.
appid
}
}
return
JSON
.
stringify
(
data
)
}
checkUpdateSuccess
(
data
)
{
if
(
data
.
code
!==
0
)
{
return
}
var
fileData
=
{
vid
:
this
.
_fileData
.
vid
,
lastCheck
:
this
.
_fileData
.
lastCheck
}
if
(
data
.
isUpdate
===
true
)
{
fileData
.
newVersion
=
data
.
newVersion
fileData
.
note
=
data
.
note
}
this
.
writeFile
(
fileData
)
}
async
getFilePath
()
{
const
rootDir
=
os
.
tmpdir
()
const
fileName
=
this
.
_getMD5
(
process
.
env
.
UNI_INPUT_DIR
)
return
path
.
join
(
rootDir
,
`
${
this
.
UPDATE_FILE_NAME
}
_
${
fileName
}
.json`
)
}
_getMac
()
{
let
mac
const
network
=
os
.
networkInterfaces
()
for
(
const
key
in
network
)
{
const
array
=
network
[
key
]
for
(
let
i
=
0
;
i
<
array
.
length
;
i
++
)
{
const
item
=
array
[
i
]
if
(
!
item
.
family
||
(
item
.
mac
&&
item
.
mac
===
'
00:00:00:00:00:00
'
))
{
continue
}
if
(
item
.
family
===
'
IPv4
'
||
item
.
family
===
'
IPv6
'
)
{
mac
=
item
.
mac
break
}
}
}
return
mac
}
_getMD5
(
str
)
{
return
this
.
crypto
.
createHash
(
'
md5
'
).
update
(
str
).
digest
(
'
hex
'
)
}
_buildUUID
()
{
var
result
=
''
for
(
let
i
=
0
;
i
<
4
;
i
++
)
{
result
+=
(
65536
*
(
1
+
Math
.
random
())
|
0
).
toString
(
16
).
substring
(
1
)
}
return
'
UNI_
'
+
result
.
toUpperCase
()
}
}
Object
.
assign
(
Upate
.
prototype
,
{
HOST
:
'
uniapp.dcloud.net.cn
'
,
PATH
:
'
/update/cli
'
,
UPDATE_FILE_NAME
:
'
uni_app_cli_update
'
,
DEFAULT_TIME
:
2000
,
DEFAULT_INTERVAL
:
1000
*
60
*
60
*
24
})
module
.
exports
=
async
function
checkUpdate
()
{
const
{
isInHBuilderX
,
getManifestJson
}
=
require
(
'
@dcloudio/uni-cli-shared
'
)
if
(
isInHBuilderX
)
{
// 仅 cli 提供检测更新
return
}
const
plp
=
require
(
'
@dcloudio/webpack-uni-pages-loader/package.json
'
)
const
ppj
=
require
(
path
.
join
(
process
.
env
.
UNI_CLI_CONTEXT
,
'
package.json
'
))
const
manifest
=
getManifestJson
()
try
{
const
update
=
new
Upate
()
update
.
compilerVersion
=
plp
[
'
uni-app
'
].
compilerVersion
update
.
isAlpha
=
ppj
.
devDependencies
[
'
@dcloudio/vue-cli-plugin-uni
'
].
includes
(
'
alpha
'
)
update
.
uniId
=
manifest
.
appid
const
appIdKey
=
process
.
env
.
UNI_PLATFORM
.
includes
(
'
quickapp
'
)
?
'
package
'
:
'
appid
'
update
.
appId
=
manifest
[
process
.
env
.
UNI_PLATFORM
]
?
(
manifest
[
process
.
env
.
UNI_PLATFORM
][
appIdKey
]
||
''
)
:
''
update
.
check
()
}
catch
(
e
)
{
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录