-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): updates libraries for React to newest versions
- Loading branch information
Showing
13 changed files
with
240 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"schematics": {} | ||
"schematics": { | ||
"update-8.3.0": { | ||
"version": "8.3.0-beta.1", | ||
"description": "Update React libraries", | ||
"factory": "./src/migrations/update-8-3-0/update-8-3-0" | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
packages/react/src/migrations/update-8-3-0/update-8-3-0.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; | ||
import { updateJsonInTree, readJsonInTree } from '@nrwl/workspace'; | ||
|
||
import * as path from 'path'; | ||
import { stripIndents } from '@angular-devkit/core/src/utils/literals'; | ||
|
||
describe('Update 8-0-0', () => { | ||
let tree: Tree; | ||
let schematicRunner: SchematicTestRunner; | ||
|
||
beforeEach(async () => { | ||
tree = Tree.empty(); | ||
schematicRunner = new SchematicTestRunner( | ||
'@nrwl/react', | ||
path.join(__dirname, '../../../migrations.json') | ||
); | ||
tree = await schematicRunner | ||
.callRule( | ||
updateJsonInTree('package.json', () => ({ | ||
dependencies: {}, | ||
devDependencies: { | ||
'react-testing-library': '5.0.0' | ||
} | ||
})), | ||
tree | ||
) | ||
.toPromise(); | ||
}); | ||
|
||
describe('imports', () => { | ||
it(`should be migrate 'react-testing-library' to '@testing-library/react`, async () => { | ||
tree.create( | ||
'Hello.spec.ts', | ||
` | ||
import Hello from './Hello'; | ||
import React from 'react'; | ||
import { render } from 'react-testing-library'; | ||
` | ||
); | ||
|
||
tree = await schematicRunner | ||
.runSchematicAsync('update-8.3.0', {}, tree) | ||
.toPromise(); | ||
|
||
expect(tree.read('Hello.spec.ts').toString('utf-8')) | ||
.toContain(stripIndents` | ||
import Hello from './Hello'; | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
`); | ||
}); | ||
}); | ||
|
||
describe('dependencies', () => { | ||
it('should update dependencies', async () => { | ||
tree = await schematicRunner | ||
.runSchematicAsync('update-8.3.0', {}, tree) | ||
.toPromise(); | ||
|
||
const result = readJsonInTree(tree, 'package.json'); | ||
|
||
expect(result).toEqual( | ||
expect.objectContaining({ | ||
devDependencies: expect.objectContaining({ | ||
'@testing-library/react': '8.0.5' | ||
}) | ||
}) | ||
); | ||
|
||
expect(result.devDependencies['react-testing-library']).not.toBeDefined(); | ||
}); | ||
}); | ||
}); |
108 changes: 108 additions & 0 deletions
108
packages/react/src/migrations/update-8-3-0/update-8-3-0.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
chain, | ||
Rule, | ||
SchematicContext, | ||
Tree | ||
} from '@angular-devkit/schematics'; | ||
import { stripIndents } from '@angular-devkit/core/src/utils/literals'; | ||
import { | ||
addDepsToPackageJson, | ||
formatFiles, | ||
insert, | ||
updateJsonInTree | ||
} from '@nrwl/workspace'; | ||
import { | ||
createSourceFile, | ||
isImportDeclaration, | ||
isStringLiteral, | ||
ScriptTarget | ||
} from 'typescript'; | ||
import { ReplaceChange } from '@nrwl/workspace/src/utils/ast-utils'; | ||
import { relative } from 'path'; | ||
import { testingLibraryVersion } from '../../utils/versions'; | ||
|
||
const ignore = require('ignore'); | ||
|
||
export default function update(): Rule { | ||
return chain([ | ||
displayInformation, | ||
updateDependencies, | ||
updateImports, | ||
formatFiles() | ||
]); | ||
} | ||
|
||
function displayInformation(host: Tree, context: SchematicContext) { | ||
context.logger.info(stripIndents` | ||
React Testing Library has been repackaged as \`@testing-library/react\` as of version 8. | ||
We are replacing your \`react-testing-library\` imports with \`@testing-library/react\`. | ||
See: https://github.com/testing-library/react-testing-library/releases/tag/v8.0.0 | ||
`); | ||
} | ||
|
||
function updateDependencies() { | ||
const removeOld = updateJsonInTree( | ||
'package.json', | ||
(json, context: SchematicContext) => { | ||
json.dependencies = json.dependencies || {}; | ||
json.devDependencies = json.devDependencies || {}; | ||
// Just in case user installed it to dependencies instead of devDependencies. | ||
delete json.dependencies['react-testing-library']; | ||
delete json.devDependencies['react-testing-library']; | ||
context.logger.info(`Removing \`react-testing-library\` as a dependency`); | ||
return json; | ||
} | ||
); | ||
const addNew = addDepsToPackageJson( | ||
{}, | ||
{ '@testing-library/react': testingLibraryVersion } | ||
); | ||
|
||
return chain([removeOld, addNew]); | ||
} | ||
|
||
function updateImports(host: Tree) { | ||
let ig = ignore().add(['*', '!*.ts']); // include only .ts files | ||
|
||
if (host.exists('.gitignore')) { | ||
ig = ig.add(host.read('.gitignore').toString()); | ||
} | ||
|
||
host.visit(path => { | ||
if (ig && ig.ignores(relative('/', path))) { | ||
return; | ||
} | ||
|
||
const sourceFile = createSourceFile( | ||
path, | ||
host.read(path).toString(), | ||
ScriptTarget.Latest, | ||
true | ||
); | ||
const changes = []; | ||
sourceFile.statements.forEach(statement => { | ||
if ( | ||
isImportDeclaration(statement) && | ||
isStringLiteral(statement.moduleSpecifier) | ||
) { | ||
const nodeText = statement.moduleSpecifier.getText(sourceFile); | ||
const modulePath = statement.moduleSpecifier | ||
.getText(sourceFile) | ||
.substr(1, nodeText.length - 2); | ||
if (modulePath === 'react-testing-library') { | ||
changes.push( | ||
new ReplaceChange( | ||
path, | ||
statement.moduleSpecifier.getStart(sourceFile), | ||
nodeText, | ||
`'@testing-library/react'` | ||
) | ||
); | ||
} | ||
} | ||
}); | ||
insert(host, path, changes); | ||
}); | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/react/src/schematics/application/files/app/src/app/__fileName__.spec.tsx__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react/src/schematics/component/files/__name__/__fileName__.spec.tsx__tmpl__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
export const nxVersion = '*'; | ||
export const frameworkVersion = '16.8.3'; | ||
export const typesVersion = '16.8.4'; | ||
export const styledComponentVersion = '4.2.0'; | ||
export const styledComponentTypesVersion = '4.1.15'; | ||
export const emotionVersion = '10.0.10'; | ||
export const domTypesVersion = '16.8.2'; | ||
export const reactRouterVersion = '5.0.0'; | ||
export const testingLibraryVersion = '6.0.0'; | ||
export const frameworkVersion = '16.8.6'; | ||
export const typesVersion = '16.8.23'; | ||
export const styledComponentsVersion = '4.3.2'; | ||
export const styledComponentsTypesVersion = '4.1.18'; | ||
export const emotionVersion = '10.0.14'; | ||
export const domTypesVersion = '16.8.4'; | ||
export const reactRouterVersion = '5.0.1'; | ||
export const testingLibraryVersion = '8.0.5'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.