/
init.ts
118 lines (107 loc) · 3.97 KB
/
init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { setDefaultCollection } from '@nrwl/workspace/src/utilities/set-default-collection';
import {
addDependenciesToPackageJson,
convertNxGenerator,
detectPackageManager,
formatFiles,
logger,
readJson,
removeDependenciesFromPackageJson,
Tree,
} from '@nrwl/devkit';
import { Schema } from './schema';
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial';
import { jestInitGenerator } from '@nrwl/jest';
import { detoxInitGenerator } from '@nrwl/detox';
import { typesReactVersion } from '@nrwl/react/src/utils/versions';
import {
babelRuntimeVersion,
jestReactNativeVersion,
metroReactNativeBabelPresetVersion,
metroVersion,
nxVersion,
reactNativeAsyncStorageAsyncStorageVersion,
reactNativeCommunityCli,
reactNativeCommunityCliAndroid,
reactNativeCommunityCliIos,
reactNativeConfigVersion,
reactNativeSvgTransformerVersion,
reactNativeSvgVersion,
reactNativeVersion,
reactTestRendererVersion,
reactVersion,
testingLibraryJestNativeVersion,
testingLibraryReactNativeVersion,
typesReactNativeVersion,
} from '../../utils/versions';
import { addGitIgnoreEntry } from './lib/add-git-ignore-entry';
import { initRootBabelConfig } from './lib/init-root-babel-config';
export async function reactNativeInitGenerator(host: Tree, schema: Schema) {
setDefaultCollection(host, '@nrwl/react-native');
addGitIgnoreEntry(host);
initRootBabelConfig(host);
const tasks = [moveDependency(host), updateDependencies(host)];
if (!schema.unitTestRunner || schema.unitTestRunner === 'jest') {
const jestTask = jestInitGenerator(host, {});
tasks.push(jestTask);
}
if (!schema.e2eTestRunner || schema.e2eTestRunner === 'detox') {
const detoxTask = await detoxInitGenerator(host, {});
tasks.push(detoxTask);
}
if (!schema.skipFormat) {
await formatFiles(host);
}
return runTasksInSerial(...tasks);
}
export function updateDependencies(host: Tree) {
const isPnpm = detectPackageManager(host.root) === 'pnpm';
const { dependencies = {} } = readJson(host, 'package.json');
// TODO(jack): Remove this once React Native 0.68.0 is out.
if (dependencies['react']?.match(/[\^~]?18/)) {
logger.warn(
`React version ${dependencies['react']} is incompatible with React Native version ${reactNativeVersion}. Nx will downgrade the version to ${reactVersion}.`
);
}
return addDependenciesToPackageJson(
host,
{
react: reactVersion,
'react-native': reactNativeVersion,
},
{
'@nrwl/react-native': nxVersion,
'@types/react': typesReactVersion,
'@types/react-native': typesReactNativeVersion,
'@react-native-community/cli': reactNativeCommunityCli,
'@react-native-community/cli-platform-android':
reactNativeCommunityCliAndroid,
'@react-native-community/cli-platform-ios': reactNativeCommunityCliIos,
'metro-react-native-babel-preset': metroReactNativeBabelPresetVersion,
'@testing-library/react-native': testingLibraryReactNativeVersion,
'@testing-library/jest-native': testingLibraryJestNativeVersion,
'jest-react-native': jestReactNativeVersion,
metro: metroVersion,
'metro-resolver': metroVersion,
'react-test-renderer': reactTestRendererVersion,
'react-native-svg-transformer': reactNativeSvgTransformerVersion,
'react-native-svg': reactNativeSvgVersion,
'react-native-config': reactNativeConfigVersion,
'@react-native-async-storage/async-storage':
reactNativeAsyncStorageAsyncStorageVersion,
...(isPnpm
? {
'metro-config': metroVersion, // metro-config is used by metro.config.js
'@babel/runtime': babelRuntimeVersion, // @babel/runtime is used by react-native-svg
}
: {}),
}
);
}
function moveDependency(host: Tree) {
return removeDependenciesFromPackageJson(host, ['@nrwl/react-native'], []);
}
export default reactNativeInitGenerator;
export const reactNativeInitSchematic = convertNxGenerator(
reactNativeInitGenerator
);