1 | import * as _ from 'lodash/fp'
|
2 | import * as npmCheck from 'npm-check'
|
3 | import { UPDATE_PACKAGE_LIST } from '@tarojs/helper'
|
4 | import { getPkgVersion } from '../util'
|
5 |
|
6 | interface ErrorLine {
|
7 | desc: string
|
8 | valid: boolean
|
9 | }
|
10 |
|
11 | const mustInstalledTaroPkg = [
|
12 | '@tarojs/components',
|
13 | '@tarojs/runtime',
|
14 | '@tarojs/taro',
|
15 | '@tarojs/mini-runner',
|
16 | '@tarojs/webpack-runner',
|
17 | 'babel-preset-taro',
|
18 | 'eslint-config-taro'
|
19 | ]
|
20 |
|
21 | const cliVersion = getPkgVersion()
|
22 | const isPkgInstalled = _.get('isInstalled')
|
23 | const isPkgNotInstalled = _.negate(isPkgInstalled)
|
24 |
|
25 | async function checkPkgs ({ appPath }) {
|
26 | let errorLines: ErrorLine[] = []
|
27 | const pkgs = await npmCheck({
|
28 | cwd: appPath
|
29 | })
|
30 | .then(currentState => currentState.get('packages'))
|
31 |
|
32 | errorLines = _.concat(errorLines, pkgsNotInstalled(pkgs))
|
33 | errorLines = _.concat(errorLines, taroShouldUpdate(pkgs))
|
34 | errorLines = _.concat(errorLines, taroOutdate(pkgs))
|
35 | errorLines = _.compact(errorLines)
|
36 |
|
37 | return {
|
38 | desc: '检查依赖',
|
39 | lines: errorLines
|
40 | }
|
41 | }
|
42 |
|
43 | function pkgsNotInstalled (pkgs): ErrorLine[] {
|
44 | const uninstalledPkgs = _.filter(isPkgNotInstalled, pkgs)
|
45 | const lines = _.map(
|
46 | pkg =>
|
47 | Object({
|
48 | desc: `使用到的依赖 ${pkg.moduleName} 还没有安装`,
|
49 | valid: false
|
50 | }),
|
51 | uninstalledPkgs
|
52 | )
|
53 | return lines
|
54 | }
|
55 |
|
56 | function taroShouldUpdate (pkgs): ErrorLine[] {
|
57 |
|
58 | const list = UPDATE_PACKAGE_LIST
|
59 | .filter(item => !(/nerv/.test(item)))
|
60 | .sort()
|
61 | .map(item => {
|
62 | const taroPkg = pkgs.find(pkg => pkg.moduleName === item)
|
63 |
|
64 | if (!taroPkg) {
|
65 | if (!mustInstalledTaroPkg.includes(item)) return null
|
66 |
|
67 | return {
|
68 | desc: `请安装 Taro 依赖: ${item}`,
|
69 | valid: true
|
70 | }
|
71 | }
|
72 |
|
73 | const { moduleName, installed, latest } = taroPkg
|
74 |
|
75 | if (installed === cliVersion) {
|
76 | if (installed === latest) return null
|
77 |
|
78 | return {
|
79 | desc: `依赖 ${moduleName} 可更新到最新版本 ${latest},当前安装版本为 ${installed}`,
|
80 | valid: true
|
81 | }
|
82 | }
|
83 |
|
84 | return {
|
85 | desc: `依赖 ${moduleName} (${installed}) 与当前使用的 Taro CLI (${cliVersion}) 版本不一致, 请更新为统一的版本`,
|
86 | valid: false
|
87 | }
|
88 | })
|
89 |
|
90 | return _.compact(list)
|
91 | }
|
92 |
|
93 | function taroOutdate (pkgs): ErrorLine[] {
|
94 | const list: ErrorLine[] = []
|
95 | pkgs.forEach(({ moduleName, isInstalled }) => {
|
96 | if (!UPDATE_PACKAGE_LIST.includes(moduleName) && /^@tarojs/.test(moduleName)) {
|
97 | list.push({
|
98 | desc: `Taro 3 不再依赖 ${moduleName},可以${isInstalled ? '卸载' : '从 package.json 移除'}`,
|
99 | valid: true
|
100 | })
|
101 | }
|
102 | })
|
103 | return list
|
104 | }
|
105 |
|
106 | export default checkPkgs
|