UNPKG

2.84 kBPlain TextView Raw
1import * as _ from 'lodash/fp'
2import * as fs from 'fs-extra'
3import * as path from 'path'
4import { chalk } from '@tarojs/helper'
5
6import { IErrorLine } from './interface'
7
8const TEST_FRAMEWORKS = ['jest', 'mocha', 'ava', 'tape', 'jesmine', 'karma']
9const LINTERS = ['eslint', 'jslint', 'tslint', 'jshint']
10const README = ['readme', 'readme.md', 'readme.markdown']
11const GITIGNORE = ['.gitignore']
12const EDITORCONFIG = ['.editorconfig']
13
14
15export default async function ({ appPath }) {
16 const PROJECT_PACKAGE_PATH = path.join(appPath, 'package.json')
17 const PROJECT_FOLDER_FILES = fs.readdirSync('./')
18 if (!fs.existsSync(PROJECT_PACKAGE_PATH)) {
19 console.log(chalk.red(`找不到${PROJECT_PACKAGE_PATH},请确定当前目录是Taro项目根目录!`))
20 process.exit(1)
21 }
22 const projectPackage = require(PROJECT_PACKAGE_PATH)
23 const devDependencies = _.keysIn(_.get('devDependencies', projectPackage))
24
25 const inDevDependencies = dependencies => (_.intersectionBy(_.toLower, devDependencies, dependencies)).length > 0
26 const hasRecommandTestFrameworks = inDevDependencies(TEST_FRAMEWORKS)
27 const hasRecommandLinters = inDevDependencies(LINTERS)
28
29 const inProjectFolder = filenames => (_.intersectionBy(_.toLower, PROJECT_FOLDER_FILES, filenames)).length > 0
30 const hasReadme = inProjectFolder(README)
31 const hasGitignore = inProjectFolder(GITIGNORE)
32 const hasEditorconfig = inProjectFolder(EDITORCONFIG)
33
34 const errorLines: IErrorLine[] = []
35
36 if (!hasRecommandTestFrameworks) {
37 errorLines.push({
38 desc: '没有检查到常见的测试依赖(jest/mocha/ava/tape/jesmine/karma), 配置测试可以帮助提升项目质量',
39 valid: true,
40 solution: '可以参考 https://github.com/NervJS/taro-ui-sample 项目, 其中已经包含了完整的测试配置与范例'
41 })
42 }
43 if (!hasRecommandLinters) {
44 errorLines.push({
45 desc: '没有检查到常见的 linter (eslint/jslint/jshint/tslint), 配置 linter 可以帮助提升项目质量',
46 valid: true,
47 solution: 'Taro 还提供了定制的 ESLint 规则, 可以帮助开发者避免一些常见的问题. 使用 taro cli 创建新项目即可体验'
48 })
49 }
50 if (!hasReadme) {
51 errorLines.push({
52 desc: '没有检查到 Readme (readme/readme.md/readme.markdown), 编写 Readme 可以方便其他人了解项目',
53 valid: true
54 })
55 }
56 if (!hasGitignore) {
57 errorLines.push({
58 desc: '没有检查到 .gitignore 配置, 配置 .gitignore 以避免将敏感信息或不必要的内容提交到代码仓库',
59 valid: true
60 })
61 }
62 if (!hasEditorconfig) {
63 errorLines.push({
64 desc: '没有检查到 .editconfig 配置, 配置 editconfig 以统一项目成员编辑器的代码风格',
65 valid: true
66 })
67 }
68
69 return {
70 desc: '检查推荐内容',
71 lines: errorLines
72 }
73}