From 92b1489785c88b26a18ff1acd90f988aaba64a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TZ=20=7C=20=E5=A4=A9=E7=8C=AA?= Date: Sat, 24 Feb 2018 09:54:38 +0800 Subject: [PATCH] feat: egg-bin check (#87) --- .autod.conf | 3 +- .eslintignore | 1 + README.md | 10 +++++ lib/check-eslintrc | 6 +++ lib/cmd/check.js | 41 +++++++++++++++++++ package.json | 13 +++--- .../check-eggache/config/config.default.js | 9 ++++ test/fixtures/check-eggache/config/plugin.js | 7 ++++ test/fixtures/check-eggache/other.js | 7 ++++ test/fixtures/check-eggache/package.json | 3 ++ test/lib/cmd/check.test.js | 32 +++++++++++++++ 11 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 lib/check-eslintrc create mode 100644 lib/cmd/check.js create mode 100644 test/fixtures/check-eggache/config/config.default.js create mode 100644 test/fixtures/check-eggache/config/plugin.js create mode 100644 test/fixtures/check-eggache/other.js create mode 100644 test/fixtures/check-eggache/package.json create mode 100644 test/lib/cmd/check.test.js diff --git a/.autod.conf b/.autod.conf index 34c8dfd4..c048647b 100644 --- a/.autod.conf +++ b/.autod.conf @@ -14,11 +14,12 @@ module.exports = { 'intelli-espower-loader', 'power-assert', 'ypkgfiles', + 'eslint-plugin-eggache', + 'eslint', ], devdep: [ 'autod', 'eslint-config-egg', - 'eslint', 'egg-ci', ], keep: [ diff --git a/.eslintignore b/.eslintignore index 0bf2048d..04b60dd2 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ node_modules/ coverage/ test/fixtures/enzyme-example-mocha/ +test/fixtures/check-eggache/ diff --git a/README.md b/README.md index 3e10ed6f..efc1cb54 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,16 @@ Generate `pkg.dependencies` and `pkg.devDependencies` automatically, see [autod] $ egg-bin autod ``` +## check + +Check egg project for collect useful infomation to report issue. + +```bash +$ egg-bin check +``` +### Rules: +- [eslint-plugin-eggache](https://github.com/eggjs/eslint-plugin-eggache) + ## Custom egg-bin for your team You maybe need a custom egg-bin to implement more custom features if your team has develop a framework base on egg. diff --git a/lib/check-eslintrc b/lib/check-eslintrc new file mode 100644 index 00000000..6d2d89c9 --- /dev/null +++ b/lib/check-eslintrc @@ -0,0 +1,6 @@ +{ + "extends": "plugin:eggache/recommended", + "parserOptions": { + "ecmaVersion": 6 + } +} diff --git a/lib/cmd/check.js b/lib/cmd/check.js new file mode 100644 index 00000000..6fd0d772 --- /dev/null +++ b/lib/cmd/check.js @@ -0,0 +1,41 @@ +'use strict'; + +const Command = require('../command'); +const path = require('path'); + +class CheckCommand extends Command { + constructor(rawArgv) { + super(rawArgv); + this.usage = 'Usage: egg-bin check'; + this.options = { + baseDir: { + description: 'directory of application, default to `process.cwd()`', + type: 'string', + }, + }; + } + + get description() { + return 'Check egg project for collect useful infomation to report issue'; + } + + * run({ cwd, argv }) { + /* istanbul ignore next */ + let baseDir = argv._[0] || argv.baseDir || cwd; + /* istanbul ignore next */ + if (!path.isAbsolute(baseDir)) baseDir = path.join(cwd, baseDir); + + // check eggache + const eslintBin = require.resolve('eslint/bin/eslint'); + const eslintArgs = this.helper.unparseArgv({ + _: [ '.' ], + config: require.resolve('../check-eslintrc'), + 'no-eslintrc': true, + format: 'codeframe', + }); + console.info('[egg-bin] run check: %s %s', eslintBin, eslintArgs.join(' ')); + yield this.helper.forkNode(eslintBin, eslintArgs, { cwd: baseDir }); + } +} + +module.exports = CheckCommand; diff --git a/package.json b/package.json index 0f87b6a8..f7022f73 100644 --- a/package.json +++ b/package.json @@ -15,18 +15,21 @@ "debug": "^3.1.0", "detect-port": "^1.2.2", "egg-utils": "^2.3.0", + "eslint": "^4.17.0", + "eslint-plugin-eggache": "^1.0.0", "globby": "^7.1.1", "inspector-proxy": "^1.1.0", "intelli-espower-loader": "^1.0.1", - "mocha": "^4.0.1", + "mocha": "^5.0.0", "mz-modules": "^2.0.0", - "nyc": "^11.3.0", + "nyc": "^11.4.1", "power-assert": "^1.4.4", - "semver": "^5.4.1", + "semver": "^5.5.0", "test-exclude": "^4.1.1", "ypkgfiles": "^1.5.0" }, "devDependencies": { + "autod": "^3.0.1", "babel": "^6.3.26", "babel-preset-airbnb": "^1.0.1", "babel-register": "^6.4.3", @@ -34,10 +37,10 @@ "cross-env": "^3.1.3", "egg": "^1.8.0", "egg-ci": "^1.8.0", - "egg-mock": "^3.13.1", + "egg-mock": "^3.14.0", "enzyme": "^2.0.0", "eslint": "^4.12.1", - "eslint-config-egg": "^5.1.1", + "eslint-config-egg": "^6.0.0", "jsdom": "^8.0.1", "mm": "^2.2.0", "mz": "^2.7.0", diff --git a/test/fixtures/check-eggache/config/config.default.js b/test/fixtures/check-eggache/config/config.default.js new file mode 100644 index 00000000..ebea593e --- /dev/null +++ b/test/fixtures/check-eggache/config/config.default.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = appInfo => { + const config = exports = {}; + config.keys = appInfo.name; + return config; +}; + +exports.test = '12345'; diff --git a/test/fixtures/check-eggache/config/plugin.js b/test/fixtures/check-eggache/config/plugin.js new file mode 100644 index 00000000..ffcf48cc --- /dev/null +++ b/test/fixtures/check-eggache/config/plugin.js @@ -0,0 +1,7 @@ +'use strict'; + +exports.view = { + enable: true, + package: '', + foo: 'bar', +}; diff --git a/test/fixtures/check-eggache/other.js b/test/fixtures/check-eggache/other.js new file mode 100644 index 00000000..8797df52 --- /dev/null +++ b/test/fixtures/check-eggache/other.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = ''; + +exports.test = '12345'; + +let a; diff --git a/test/fixtures/check-eggache/package.json b/test/fixtures/check-eggache/package.json new file mode 100644 index 00000000..4c34bb7f --- /dev/null +++ b/test/fixtures/check-eggache/package.json @@ -0,0 +1,3 @@ +{ + "name": "check-eggache" +} \ No newline at end of file diff --git a/test/lib/cmd/check.test.js b/test/lib/cmd/check.test.js new file mode 100644 index 00000000..20192d42 --- /dev/null +++ b/test/lib/cmd/check.test.js @@ -0,0 +1,32 @@ +'use strict'; + +const path = require('path'); +const coffee = require('coffee'); + +const eggBin = require.resolve('../../../bin/egg-bin.js'); + +describe('test/lib/cmd/check.test.js', () => { + it('should check eggache', function* () { + const cwd = path.join(__dirname, '../../fixtures/check-eggache'); + yield coffee.fork(eggBin, [ 'check' ], { cwd }) + // .debug() + .expect('stdout', /eggache\/no-override-exports/) + .expect('stdout', /eggache\/no-unexpected-plugin-keys/) + .notExpect('stdout', /no-unused-vars/) + .notExpect('stdout', /\/other.js/) + .expect('code', 1) + .end(); + }); + + it('should check eggache with --baseDir', function* () { + const cwd = path.join(__dirname, '../../fixtures/check-eggache'); + yield coffee.fork(eggBin, [ 'check', '--baseDir', cwd ]) + // .debug() + .expect('stdout', /eggache\/no-override-exports/) + .expect('stdout', /eggache\/no-unexpected-plugin-keys/) + .notExpect('stdout', /no-unused-vars/) + .notExpect('stdout', /\/other.js/) + .expect('code', 1) + .end(); + }); +});