1 | import * as _ from 'lodash/fp'
|
2 | import * as npmCheck from 'npm-check'
|
3 |
|
4 | import { getPkgVersion } from '../util'
|
5 |
|
6 | const pkgVersion = getPkgVersion()
|
7 | const isTaroPkg = pkg => /^@tarojs\
|
8 | const isCliVersionNotMatch = _.compose(_.negate(_.equals(pkgVersion)), _.get('installed'))
|
9 | const isPkgInstalled = _.get('isInstalled')
|
10 | const isPkgNotInstalled = _.negate(isPkgInstalled)
|
11 |
|
12 | async 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 |
|
32 | function 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 |
|
41 | function taroShouldUpdate (pkgs) {
|
42 |
|
43 | const taroPkg = _.find(isPkgInstalled, pkgs)
|
44 | if (!taroPkg || taroPkg.latest === taroPkg.installed) return []
|
45 |
|
46 | return [{
|
47 |
|
48 | desc: `检测到最新稳定版本 Taro ${taroPkg.latest} , 当前 cli 版本 ${pkgVersion}`,
|
49 | valid: true,
|
50 | solution: `前往 https://github.com/NervJS/taro/releases 了解详情`
|
51 | }]
|
52 | }
|
53 |
|
54 | function 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 |
|
63 | export default checkPkgs
|