Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react-native): add migration script to change the main tag to cla…
…ssName (#9957)
- Loading branch information
1 parent
7be52d0
commit 16fb1f9
Showing
4 changed files
with
99 additions
and
1 deletion.
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
48 changes: 48 additions & 0 deletions
48
packages/react-native/src/migrations/update-14-0-2/change-main-to-class-name-14-0-2.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,48 @@ | ||
import { addProjectConfiguration, Tree } from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
|
||
import update from './change-main-to-class-name-14-0-2'; | ||
|
||
describe('Change from main tag to className tag', () => { | ||
let tree: Tree; | ||
|
||
beforeEach(async () => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
addProjectConfiguration(tree, 'products', { | ||
root: 'apps/products', | ||
sourceRoot: 'apps/products/src', | ||
targets: { | ||
start: { | ||
executor: '@nrwl/react-native:start', | ||
options: { | ||
port: 8081, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it(`should udpate main file to registerComponent className`, async () => { | ||
tree.write( | ||
'apps/products/src/main.tsx', | ||
`AppRegistry.registerComponent('main', () => App);` | ||
); | ||
await update(tree); | ||
|
||
expect(tree.read('apps/products/src/main.tsx', 'utf-8')).toEqual( | ||
`AppRegistry.registerComponent('Products', () => App);` | ||
); | ||
}); | ||
|
||
it(`should not udpate main file to registerComponent className if it does not exists`, async () => { | ||
tree.write( | ||
'apps/products/src/main.tsx', | ||
`AppRegistry.registerComponent('otherTagName', () => App);` | ||
); | ||
await update(tree); | ||
|
||
expect(tree.read('apps/products/src/main.tsx', 'utf-8')).toEqual( | ||
`AppRegistry.registerComponent('otherTagName', () => App);` | ||
); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/react-native/src/migrations/update-14-0-2/change-main-to-class-name-14-0-2.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,45 @@ | ||
import { | ||
formatFiles, | ||
getProjects, | ||
logger, | ||
names, | ||
stripIndents, | ||
Tree, | ||
} from '@nrwl/devkit'; | ||
import { join } from 'path'; | ||
|
||
/** | ||
* This function changes "main" tag to project's className | ||
*/ | ||
export default async function update(tree: Tree) { | ||
const projects = getProjects(tree); | ||
|
||
for (const [name, config] of projects.entries()) { | ||
if (config.targets?.start?.executor !== '@nrwl/react-native:start') | ||
continue; | ||
let mailFilePath; | ||
if (tree.exists(join(config.root, 'src/main.tsx'))) { | ||
mailFilePath = join(config.root, 'src/main.tsx'); | ||
} | ||
if (tree.exists(join(config.root, 'src/main.js'))) { | ||
mailFilePath = join(config.root, 'src/main.js'); | ||
} | ||
if (!mailFilePath) { | ||
continue; | ||
} | ||
try { | ||
const { className } = names(name); | ||
const content = tree.read(mailFilePath, 'utf-8'); | ||
if (!content.includes(`'main'`)) { | ||
return; | ||
} | ||
tree.write(mailFilePath, content.replace(`'main'`, `'${className}'`)); | ||
} catch { | ||
logger.error( | ||
stripIndents`Unable to update ${mailFilePath} for project ${name}.` | ||
); | ||
} | ||
} | ||
|
||
await formatFiles(tree); | ||
} |