UNPKG

2.13 kBPlain TextView Raw
1import * as _ from 'lodash/fp'
2import * as npmCheck from 'npm-check'
3
4import { getPkgVersion } from '../util'
5
6const pkgVersion = getPkgVersion()
7const isTaroPkg = pkg => /^@tarojs\//.test(pkg.moduleName)
8const isCliVersionNotMatch = _.compose(_.negate(_.equals(pkgVersion)), _.get('installed'))
9const isPkgInstalled = _.get('isInstalled')
10const isPkgNotInstalled = _.negate(isPkgInstalled)
11
12async function checkPkgs ({ appPath }) {
13 let errorLines: any[] = []
14 const pkgs = await npmCheck({
15 cwd: appPath
16 })
17 .then(_.invoke('all'))
18 .then(_.get('packages'))
19 const taroPkgs = _.filter(isTaroPkg, pkgs)
20
21 errorLines = _.concat(errorLines, pkgsNotInstalled(pkgs))
22 errorLines = _.concat(errorLines, taroShouldUpdate(taroPkgs))
23 errorLines = _.concat(errorLines, taroCliVersionNotMatch(taroPkgs))
24 errorLines = _.compact(errorLines)
25
26 return {
27 desc: '检查依赖',
28 lines: errorLines
29 }
30}
31
32function taroCliVersionNotMatch (pkgs) {
33 const pkgsNotMatch = _.filter(pkg => isPkgInstalled(pkg) && isCliVersionNotMatch(pkg), pkgs)
34 const lines = _.map(pkg => Object({
35 desc: `${pkg.moduleName} (${pkg.installed}) 与当前使用的 @tarojs/cli (${pkgVersion}) 版本不一致, 请更新为统一的版本`,
36 valid: false
37 }), pkgsNotMatch)
38 return lines
39}
40
41function taroShouldUpdate (pkgs) {
42 // 未安装的依赖的情况下查找更新没有意义
43 const taroPkg = _.find(isPkgInstalled, pkgs)
44 if (!taroPkg || taroPkg.latest === taroPkg.installed) return []
45
46 return [{
47 // 需要正确设置 next 版本以使 npm-check 在判定最新版本时将 rc 版本也算在内
48 desc: `检测到最新稳定版本 Taro ${taroPkg.latest} , 当前 cli 版本 ${pkgVersion}`,
49 valid: true, // 并非错误,仅提示即可
50 solution: `前往 https://github.com/NervJS/taro/releases 了解详情`
51 }]
52}
53
54function pkgsNotInstalled (pkgs) {
55 const uninstalledPkgs = _.filter(isPkgNotInstalled, pkgs)
56 const lines = _.map(pkg => Object({
57 desc: `使用到的依赖 ${pkg.moduleName} 还没有安装`,
58 valid: false
59 }), uninstalledPkgs)
60 return lines
61}
62
63export default checkPkgs