-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
159 lines (140 loc) · 5.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as path from 'path'
import { ESLint } from 'eslint'
import * as glob from 'glob'
import {
validateEnvPrint as validateEnvPrintBinding,
validateConfigPrint as validateConfigPrintBinding,
validatePackagePrint as validatePackagePrintBinding,
validateRecommendPrint as validateRecommendPrintBinding,
validateEnv as validateEnvBinding,
validateConfig as validateConfigBinding,
validatePackage as validatePackageBinding,
validateRecommend as validateRecommendBinding,
MessageKind,
Message,
ValidateResult,
} from './js-binding'
export default (ctx) => {
ctx.registerCommand({
name: 'doctor',
async fn() {
const { appPath, nodeModulesPath, configPath } = ctx.paths
const { fs, chalk, PROJECT_CONFIG } = ctx.helper
if (!configPath || !fs.existsSync(configPath)) {
console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`))
process.exit(1)
}
validateEnvPrint()
await validateConfigPrint(ctx.initialConfig, ctx.helper)
validatePackagePrint(appPath, nodeModulesPath)
validateRecommendPrint(appPath)
await validateEslintPrint(ctx.initialConfig, chalk)
},
})
}
async function getValidateConfigParams(projectConfig: any, helper: any) {
const configStr = JSON.stringify(projectConfig, (_, v) => {
if (typeof v === 'function') {
return '__function__'
}
return v
})
let remoteSchemaUrl = 'https://raw.githubusercontent.com/NervJS/taro-doctor/main/assets/config_schema.json'
let useRemoteSchema = true
const homedir = helper.getUserHomeDir()
if (homedir) {
const taroConfigPath = path.join(homedir, helper.TARO_CONFIG_FOLDER)
const taroConfig = path.join(taroConfigPath, helper.TARO_BASE_CONFIG)
if (helper.fs.existsSync(taroConfig)) {
const config = await helper.fs.readJSON(taroConfig)
remoteSchemaUrl = config && config.remoteConfigSchemaUrl ? config.remoteConfigSchemaUrl : remoteSchemaUrl
useRemoteSchema = config && config.useRemoteConfigSchema ? config.useRemoteConfigSchema : useRemoteSchema
} else {
await helper.fs.createFile(taroConfig)
await helper.fs.writeJSON(taroConfig, { remoteSchemaUrl, useRemoteSchema })
}
}
return { configStr, remoteSchemaUrl, useRemoteSchema }
}
export async function validateConfig(projectConfig: any, helper: any): Promise<ValidateResult> {
const { configStr, remoteSchemaUrl, useRemoteSchema } = await getValidateConfigParams(projectConfig, helper)
return validateConfigBinding(configStr, remoteSchemaUrl, useRemoteSchema)
}
export async function validateConfigPrint(projectConfig: any, helper: any): Promise<boolean> {
const { configStr, remoteSchemaUrl, useRemoteSchema } = await getValidateConfigParams(projectConfig, helper)
return validateConfigPrintBinding(configStr, remoteSchemaUrl, useRemoteSchema)
}
export function validateEnv(): ValidateResult {
return validateEnvBinding()
}
export function validateEnvPrint(): boolean {
return validateEnvPrintBinding()
}
export function validatePackage(appPath: string, nodeModulesPath: string): ValidateResult {
return validatePackageBinding(appPath, nodeModulesPath)
}
export function validatePackagePrint(appPath: string, nodeModulesPath: string): boolean {
return validatePackagePrintBinding(appPath, nodeModulesPath)
}
export function validateRecommend(appPath: string): ValidateResult {
return validateRecommendBinding(appPath)
}
export function validateRecommendPrint(appPath: string): boolean {
return validateRecommendPrintBinding(appPath)
}
export async function validateEslint(projectConfig, chalk): Promise<ValidateResult> {
const result = await validateEslintCore(projectConfig, chalk)
result.messages.unshift({
kind: MessageKind.Info,
content: `\u{1F3AF} 检查 ESLint (以下为 ESLint 的输出)!`,
})
return result
}
export async function validateEslintPrint(projectConfig, chalk): Promise<boolean> {
const result = await validateEslintCore(projectConfig, chalk)
let is_valid = result.isValid
let rawReport = result.messages[0].content
console.log(`\u{1F3AF} 检查 ESLint (以下为 ESLint 的输出)!`)
if (is_valid) {
console.log(`${chalk.green('[\u{2713}]')} Eslint 检查通过!`)
} else {
console.log(rawReport)
}
return is_valid
}
async function validateEslintCore(projectConfig, chalk): Promise<ValidateResult> {
const appPath = process.cwd()
const globPattern = glob.sync(path.join(appPath, '.eslintrc*'))
const eslintCli = new ESLint({
cwd: process.cwd(),
useEslintrc: Boolean(globPattern.length),
baseConfig: {
extends: [`taro/${projectConfig.framework}`],
},
})
const sourceFiles = path.join(process.cwd(), projectConfig.sourceRoot, '**/*.{js,ts,jsx,tsx}')
const report = await eslintCli.lintFiles([sourceFiles])
const formatter = await eslintCli.loadFormatter()
let rawReport = formatter.format(report)
let is_valid = true
if (rawReport) {
is_valid = false
}
if (is_valid) {
rawReport = `${chalk.green('[\u{2713}]')} Eslint 检查通过!`
}
return {
isValid: is_valid,
messages: [
{
kind: is_valid ? MessageKind.Success : MessageKind.Error,
content: rawReport,
},
],
}
}
export {
MessageKind,
ValidateResult,
Message
}