UNPKG

3.77 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const helper_1 = require("@tarojs/helper");
13const _ = require("lodash/fp");
14const npmCheck = require("npm-check");
15const util_1 = require("../util");
16const mustInstalledTaroPkg = [
17 '@tarojs/components',
18 '@tarojs/runtime',
19 '@tarojs/taro',
20 '@tarojs/mini-runner',
21 '@tarojs/webpack-runner',
22 'babel-preset-taro',
23 'eslint-config-taro'
24];
25const cliVersion = (0, util_1.getPkgVersion)();
26const isPkgInstalled = _.get('isInstalled');
27const isPkgNotInstalled = _.negate(isPkgInstalled);
28function checkPkgs({ appPath }) {
29 return __awaiter(this, void 0, void 0, function* () {
30 let errorLines = [];
31 const pkgs = yield npmCheck({
32 cwd: appPath
33 })
34 .then(currentState => currentState.get('packages'));
35 errorLines = _.concat(errorLines, pkgsNotInstalled(pkgs));
36 errorLines = _.concat(errorLines, taroShouldUpdate(pkgs));
37 errorLines = _.concat(errorLines, taroOutdate(pkgs));
38 errorLines = _.compact(errorLines);
39 return {
40 desc: '检查依赖',
41 lines: errorLines
42 };
43 });
44}
45function pkgsNotInstalled(pkgs) {
46 const uninstalledPkgs = _.filter(isPkgNotInstalled, pkgs);
47 const lines = _.map(pkg => Object({
48 desc: `使用到的依赖 ${pkg.moduleName} 还没有安装`,
49 valid: false
50 }), uninstalledPkgs);
51 return lines;
52}
53function taroShouldUpdate(pkgs) {
54 // sort 是为了 UPDATE_PACKAGE_LIST 顺序改变也不影响单测结果
55 const list = helper_1.UPDATE_PACKAGE_LIST
56 .sort()
57 .map(item => {
58 const taroPkg = pkgs.find(pkg => pkg.moduleName === item);
59 if (!taroPkg) {
60 if (!mustInstalledTaroPkg.includes(item))
61 return null;
62 return {
63 desc: `请安装 Taro 依赖: ${item}`,
64 valid: true
65 };
66 }
67 const { moduleName, installed, latest } = taroPkg;
68 if (installed === cliVersion) {
69 if (installed === latest)
70 return null;
71 return {
72 desc: `依赖 ${moduleName} 可更新到最新版本 ${latest},当前安装版本为 ${installed}`,
73 valid: true
74 };
75 }
76 return {
77 desc: `依赖 ${moduleName} (${installed}) 与当前使用的 Taro CLI (${cliVersion}) 版本不一致, 请更新为统一的版本`,
78 valid: false
79 };
80 });
81 return _.compact(list);
82}
83function taroOutdate(pkgs) {
84 const list = [];
85 pkgs.forEach(({ moduleName, isInstalled }) => {
86 if (!helper_1.UPDATE_PACKAGE_LIST.includes(moduleName) && /^@tarojs/.test(moduleName) && !/^@tarojs\/plugin-/.test(moduleName)) {
87 list.push({
88 desc: `Taro 3 不再依赖 ${moduleName},可以${isInstalled ? '卸载' : '从 package.json 移除'}`,
89 valid: true
90 });
91 }
92 });
93 return list;
94}
95exports.default = checkPkgs;
96//# sourceMappingURL=packageValidator.js.map
\No newline at end of file