Skip to content

Commit

Permalink
refactor linter prompts and compat w/ TS
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 11, 2018
1 parent 7033eb1 commit 482d288
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 57 deletions.
3 changes: 3 additions & 0 deletions __mocks__/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ exports.prompt = prompts => {
}

const a = pendingAssertions[i - skipped]
if (!a) {
console.error(`no matching assertion for prompt:`, prompt)
}

if (a.message) {
const message = typeof prompt.message === 'function'
Expand Down
6 changes: 3 additions & 3 deletions packages/@vue/cli-plugin-eslint/generator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (api, { config, lintOn }) => {
module.exports = (api, { config, lintOn = [] }) => {
const pkg = {
scripts: {
lint: 'vue-cli-service lint'
Expand Down Expand Up @@ -28,13 +28,13 @@ module.exports = (api, { config, lintOn }) => {
pkg.eslintConfig.extends.push('eslint:recommended')
}

if (lintOn === 'save') {
if (lintOn.includes('save')) {
pkg.vue = {
lintOnSave: true // eslint-loader configured in runtime plugin
}
}

if (lintOn === 'commit') {
if (lintOn.includes('commit')) {
Object.assign(pkg.devDependencies, {
'lint-staged': '^6.0.0'
})
Expand Down
46 changes: 33 additions & 13 deletions packages/@vue/cli-plugin-typescript/generator/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
module.exports = (api, options) => {
api.extendPackage({
scripts: {
lint: 'vue-cli-service lint'
}
})

if (options.classComponent) {
module.exports = (api, { classComponent, lint, lintOn = [] }) => {
if (classComponent) {
api.extendPackage({
devDependencies: {
'vue-class-component': '^6.0.0',
Expand All @@ -14,6 +8,37 @@ module.exports = (api, options) => {
})
}

if (lint) {
api.extendPackage({
scripts: {
lint: 'vue-cli-service lint'
},
vue: {
lintOnSave: lintOn.includes('save')
}
})

if (lintOn.includes('commit')) {
api.extendPackage({
devDependencies: {
'lint-staged': '^6.0.0'
},
gitHooks: {
'pre-commit': 'lint-staged'
},
'lint-staged': {
'*.js': ['vue-cli-service lint', 'git add'],
'*.vue': ['vue-cli-service lint', 'git add']
}
})
}

// lint and fix files on creation complete
api.onCreateComplete(() => {
return require('../lib/tslint')({}, api, true)
})
}

// inject necessary typings for other plugins

const hasMocha = api.hasPlugin('@vue/cli-plugin-unit-mocha')
Expand Down Expand Up @@ -57,9 +82,4 @@ module.exports = (api, options) => {
}
}
})

// lint and fix files on creation complete
api.onCreateComplete(() => {
return require('../lib/tslint')({}, api, true)
})
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<%_ if (options.lint) { _%>
{
"defaultSeverity": "warning",
"extends": [
Expand All @@ -11,3 +12,4 @@
"object-literal-sort-keys": false
}
}
<%_ } _%>
5 changes: 2 additions & 3 deletions packages/@vue/cli-plugin-typescript/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = api => {
module.exports = (api, options) => {
api.chainWebpack(config => {
config.entry('app')
.clear()
Expand Down Expand Up @@ -26,8 +26,7 @@ module.exports = api => {
.plugin('fork-ts-checker')
.use(require('fork-ts-checker-webpack-plugin'), [{
vue: true,
tslint: true,
silent: true,
tslint: options.lintOnSave,
formatter: 'codeframe'
}])
})
Expand Down
8 changes: 4 additions & 4 deletions packages/@vue/cli/lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ async function create (projectName, options) {

const promptModules = [
'babel',
'typescript',
'pwa',
'router',
'vuex',
'cssPreprocessors',
'eslint',
'linter',
'unit',
'e2e',
'pwa',
'typescript'
'e2e'
].map(file => require(`./promptModules/${file}`))

const creator = new Creator(projectName, targetDir, promptModules)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jest.mock('inquirer')

const assertPromptModule = require('@vue/cli-test-utils/assertPromptModule')

const moduleToTest = require('../eslint')
const moduleToTest = require('../linter')

test('base', async () => {
const expectedPrompts = [
Expand All @@ -18,17 +18,17 @@ test('base', async () => {
choose: 0
},
{
message: 'Pick a lint mode',
choices: ['on save', 'on commit', 'Manually'],
choose: 0
message: 'Pick additional lint features',
choices: ['on save', 'on commit'],
check: [0, 1]
}
]

const expectedOptions = {
plugins: {
'@vue/cli-plugin-eslint': {
config: 'base',
lintOn: 'save'
lintOn: ['save', 'commit']
}
}
}
Expand All @@ -54,17 +54,16 @@ test('airbnb', async () => {
choose: 1
},
{
message: 'Pick a lint mode',
choices: ['on save', 'on commit', 'Manually'],
choose: 1
choices: ['on save', 'on commit'],
check: [1]
}
]

const expectedOptions = {
plugins: {
'@vue/cli-plugin-eslint': {
config: 'airbnb',
lintOn: 'commit'
lintOn: ['commit']
}
}
}
Expand All @@ -90,17 +89,16 @@ test('standard', async () => {
choose: 2
},
{
message: 'Pick a lint mode',
choices: ['on save', 'on commit', 'Manually'],
choose: 2
choices: ['on save', 'on commit'],
check: []
}
]

const expectedOptions = {
plugins: {
'@vue/cli-plugin-eslint': {
config: 'standard',
lintOn: false
lintOn: []
}
}
}
Expand Down
43 changes: 43 additions & 0 deletions packages/@vue/cli/lib/promptModules/__tests__/typescript.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
jest.mock('fs')
jest.mock('inquirer')

const assertPromptModule = require('@vue/cli-test-utils/assertPromptModule')

const moduleToTest = require('../typescript')
const linterModule = require('../linter')

test('should work', async () => {
const expectedPrompts = [
{
message: 'features',
choices: ['TypeScript', 'Linter'],
check: [0, 1]
},
{
message: 'Use class-style component',
confirm: true
},
{
message: 'Pick additional lint features',
choices: ['on save', 'on commit'],
check: [0, 1]
}
]

const expectedOptions = {
plugins: {
'@vue/cli-plugin-typescript': {
classComponent: true,
lint: true,
lintOn: ['save', 'commit']
}
}
}

await assertPromptModule(
[moduleToTest, linterModule],
expectedPrompts,
expectedOptions,
{ plguinsOnly: true }
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ module.exports = cli => {
const { hasGit } = require('@vue/cli-shared-utils')

cli.injectFeature({
name: 'Linter',
value: 'eslint',
name: 'Linter / Formatter',
value: 'linter',
short: 'Linter'
})

cli.injectPrompt({
name: 'eslintConfig',
when: answers => answers.features.includes('eslint'),
when: answers => (
answers.features.includes('linter') &&
!answers.features.includes('ts')
),
type: 'list',
message: 'Pick a lint config:',
choices: [
Expand Down Expand Up @@ -39,27 +42,24 @@ module.exports = cli => {

cli.injectPrompt({
name: 'lintOn',
message: 'Pick a lint mode:',
when: answers => answers.features.includes('eslint'),
type: 'list',
message: 'Pick additional lint features:',
when: answers => answers.features.includes('linter'),
type: 'checkbox',
choices: [
{
name: 'Lint + fix on save',
name: 'Lint on save',
value: 'save'
},
{
name: 'Lint + fix on commit' + (hasGit ? '' : chalk.red(' (requires Git)')),
name: 'Lint and fix on commit' + (hasGit ? '' : chalk.red(' (requires Git)')),
value: 'commit'
},
{
name: 'Manually run npm script',
value: false
}
]
})

cli.onPromptComplete((answers, options) => {
if (answers.features.includes('eslint')) {
if (answers.features.includes('linter') &&
!answers.features.includes('ts')) {
options.plugins['@vue/cli-plugin-eslint'] = {
config: answers.eslintConfig,
lintOn: answers.lintOn
Expand Down
7 changes: 6 additions & 1 deletion packages/@vue/cli/lib/promptModules/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ module.exports = cli => {

cli.onPromptComplete((answers, options) => {
if (answers.features.includes('ts')) {
options.plugins['@vue/cli-plugin-typescript'] = {
const tsOptions = {
classComponent: answers.tsClassComponent
}
if (answers.features.includes('linter')) {
tsOptions.lint = true
tsOptions.lintOn = answers.lintOn
}
options.plugins['@vue/cli-plugin-typescript'] = tsOptions
}
})
}

0 comments on commit 482d288

Please sign in to comment.