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