Skip to content

Commit

Permalink
feat: implement a migrator to auto add eslint to deps (#4549)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodatea committed Sep 7, 2019
1 parent b799b83 commit 74fae44
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
26 changes: 26 additions & 0 deletions packages/@vue/cli-plugin-eslint/migrator/index.js
@@ -0,0 +1,26 @@
module.exports = (api) => {
// if project is scaffolded by Vue CLI 3.0.x or earlier,
// the ESLint dependency (ESLint v4) is inside @vue/cli-plugin-eslint;
// in Vue CLI v4 it should be extracted to the project dependency list.
if (api.fromVersion('^3')) {
const pkg = require(api.resolve('package.json'))
const hasESLint = [
'dependencies',
'devDependencies',
'peerDependencies',
'optionalDependencies'
].some(depType =>
Object.keys(pkg[depType] || {}).includes('eslint')
)

if (!hasESLint) {
api.extendPackage({
devDependencies: {
eslint: '^4.19.1'
}
})
}

// TODO: add a prompt for users to optionally upgrade their eslint configs to a new major version
}
}
28 changes: 24 additions & 4 deletions packages/@vue/cli/__tests__/Upgrader.spec.js
Expand Up @@ -15,8 +15,11 @@ beforeAll(() => {
}
})

test('upgrade: plugin-babel v3.5', async () => {
beforeEach(() => {
process.env.VUE_CLI_TEST_DO_INSTALL_PLUGIN = true
})

test('upgrade: plugin-babel v3.5', async () => {
const project = await create('plugin-babel-legacy', {
plugins: {
'@vue/cli-plugin-babel': {
Expand All @@ -28,7 +31,7 @@ test('upgrade: plugin-babel v3.5', async () => {
const pkg = JSON.parse(await project.read('package.json'))
expect(pkg.dependencies).not.toHaveProperty('core-js')

await (new Upgrader(project.dir)).upgrade('@vue/babel', {})
await (new Upgrader(project.dir)).upgrade('babel', {})

const updatedPkg = JSON.parse(await project.read('package.json'))
expect(updatedPkg.dependencies).toHaveProperty('core-js')
Expand All @@ -40,7 +43,6 @@ test('upgrade: plugin-babel v3.5', async () => {
})

test('upgrade: plugin-babel with core-js 2', async () => {
process.env.VUE_CLI_TEST_DO_INSTALL_PLUGIN = true
const project = await create('plugin-babel-v3', {
plugins: {
'@vue/cli-plugin-babel': {
Expand All @@ -52,8 +54,26 @@ test('upgrade: plugin-babel with core-js 2', async () => {
const pkg = JSON.parse(await project.read('package.json'))
expect(pkg.dependencies['core-js']).toMatch('^2')

await (new Upgrader(project.dir)).upgrade('@vue/babel', {})
await (new Upgrader(project.dir)).upgrade('babel', {})

const updatedPkg = JSON.parse(await project.read('package.json'))
expect(updatedPkg.dependencies['core-js']).toMatch('^3')
})

test('upgrade: should add eslint to devDependencies', async () => {
const project = await create('plugin-eslint-v3.0', {
plugins: {
'@vue/cli-plugin-eslint': {
version: '3.0.0'
}
}
}, outsideTestFolder)

const pkg = JSON.parse(await project.read('package.json'))
expect(pkg.devDependencies).not.toHaveProperty('eslint')

await (new Upgrader(project.dir)).upgrade('eslint', {})

const updatedPkg = JSON.parse(await project.read('package.json'))
expect(updatedPkg.devDependencies.eslint).toMatch('^4')
})

0 comments on commit 74fae44

Please sign in to comment.