UNPKG

3.52 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const path = require("path");
12const _ = require("lodash/fp");
13const ora = require("ora");
14exports.default = (ctx) => {
15 ctx.registerCommand({
16 name: 'doctor',
17 fn() {
18 return __awaiter(this, void 0, void 0, function* () {
19 const { validators } = require('../../doctor').default;
20 const { abilityXMLValidator } = require('../../doctor/abilityXMLValidator');
21 const { appPath, configPath } = ctx.paths;
22 const { fs, chalk, PROJECT_CONFIG } = ctx.helper;
23 if (!configPath || !fs.existsSync(configPath)) {
24 console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`));
25 process.exit(1);
26 }
27 const QUICKAPP_CONF_PATH = path.join(appPath, 'project.quickapp.json');
28 if (fs.existsSync(QUICKAPP_CONF_PATH)) {
29 validators.push(abilityXMLValidator);
30 }
31 const NOTE_ALL_RIGHT = chalk.green('[✓] ');
32 const NOTE_VALID = chalk.yellow('[!] ');
33 const NOTE_INVALID = chalk.red('[✗] ');
34 const titleChalk = chalk.hex('#aaa');
35 const lineChalk = chalk.hex('#fff');
36 const solutionChalk = chalk.hex('#999');
37 function printReport(reports) {
38 _.forEach(report => {
39 console.log('\n' + titleChalk(report.desc));
40 if (report.raw) {
41 console.log(report.raw);
42 return;
43 }
44 if (_.getOr(0, 'lines.length', report) === 0) {
45 console.log(` ${NOTE_ALL_RIGHT}没有发现问题`);
46 return;
47 }
48 _.forEach(line => {
49 console.log(' ' +
50 (line.valid ? NOTE_VALID : NOTE_INVALID) +
51 lineChalk(line.desc));
52 if (line.solution) {
53 console.log(' ' + solutionChalk(line.solution));
54 }
55 }, report.lines);
56 }, reports);
57 }
58 const spinner = ora('正在诊断项目...').start();
59 const reportsP = _.map(validator => validator({
60 appPath,
61 projectConfig: ctx.initialConfig,
62 configPath
63 }), validators);
64 const reports = yield Promise.all(reportsP);
65 spinner.succeed('诊断完成');
66 printReport(reports);
67 });
68 }
69 });
70};