-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreact-native-lab.ts
More file actions
212 lines (185 loc) · 5.73 KB
/
react-native-lab.ts
File metadata and controls
212 lines (185 loc) · 5.73 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* eslint-disable import/no-extraneous-dependencies */
import colors from "picocolors";
import { execSync } from "node:child_process";
import { basename, dirname, join, resolve } from "node:path";
import { copyFileSync, existsSync, mkdirSync, readdirSync } from "node:fs";
import { rmSync } from "node:fs";
import { getTemplateFile, installTemplate } from "./template";
import { tryGitInit } from "./helpers/git";
import getOnline from "./helpers/is-online";
import isWriteable from "./helpers/is-writable";
import isFolderEmpty from "./helpers/is-folder-empty";
import { PackageManager } from "./helpers/get-pkg-manager";
import { EnvPackages, TemplateType } from "./types";
import { setupCI as initCI } from "./helpers/ci";
function fixAndroidPackageStructure(root: string, packageName: string) {
const androidJavaPath = join(root, "android", "app", "src", "main", "java");
if (!existsSync(androidJavaPath)) {
return;
}
try {
const comDir = join(androidJavaPath, "com");
if (!existsSync(comDir)) {
return;
}
// Check if we have the incorrect structure (com/com.package.name/)
const packageDirs = readdirSync(comDir);
const incorrectDir = packageDirs.find((dir) => dir.startsWith("com."));
if (incorrectDir) {
// Parse the package name to create correct folder structure
const packageParts = packageName.split(".");
// Create the correct folder structure
let currentPath = androidJavaPath;
for (const part of packageParts) {
currentPath = join(currentPath, part);
if (!existsSync(currentPath)) {
mkdirSync(currentPath, { recursive: true });
}
}
// Move files from incorrect structure to correct structure
const incorrectPath = join(comDir, incorrectDir);
const correctPath = currentPath;
if (existsSync(incorrectPath)) {
// Copy all files from incorrect path to correct path
const files = readdirSync(incorrectPath);
for (const file of files) {
const srcFile = join(incorrectPath, file);
const destFile = join(correctPath, file);
if (existsSync(srcFile)) {
copyFileSync(srcFile, destFile);
}
}
// Remove the incorrect directory structure
rmSync(join(comDir, incorrectDir), { recursive: true, force: true });
console.log(
colors.green(`✓ Fixed Android package structure for ${packageName}`),
);
}
}
} catch (error) {
console.warn(
colors.yellow(
`Warning: Could not fix Android package structure: ${error}`,
),
);
}
}
export default async function createReactNative({
appPath,
packageManager,
reactNativeVersion,
packageName,
nativeWind,
srcDir,
envEnabled,
envPackage,
includeCustomHooks,
customHooks,
includeConsoleRemover,
disableGit,
skipInstall,
template,
setupCI,
}: {
appPath: string;
packageManager: PackageManager;
reactNativeVersion: string;
packageName?: string;
srcDir: boolean;
nativeWind: boolean;
envEnabled: boolean;
envPackage: EnvPackages;
includeCustomHooks: boolean;
customHooks: string[];
includeConsoleRemover: boolean;
disableGit?: boolean;
skipInstall: boolean;
template: TemplateType;
setupCI?: boolean;
}) {
const root = resolve(appPath);
if (!(await isWriteable(dirname(root)))) {
console.error(
"The application path is not writable, please check folder permissions and try again.",
);
console.error(
"It is likely you do not have write permissions for this folder.",
);
process.exit(1);
}
const appName = basename(root);
mkdirSync(root, { recursive: true });
if (!isFolderEmpty(root, appName)) {
process.exit(1);
}
const useYarn = packageManager === "yarn";
const isOnline = !useYarn || (await getOnline());
if (!isOnline) {
console.error(
"You appear to be offline. Please check your network connection.",
);
process.exit(1);
}
console.log("");
console.log(`Creating a new React Native app in ${colors.green(root)}.`);
// Build the CLI command with optional package name
let cliCommand = `npx @react-native-community/cli init ${appName} --pm ${packageManager} --version ${reactNativeVersion} --skip-git-init --skip-install`;
if (packageName) {
cliCommand += ` --package-name ${packageName}`;
}
try {
execSync(cliCommand, { stdio: "inherit" });
} catch (error) {
console.error("Failed to create the project.");
console.error(error);
process.exit(1);
}
process.chdir(root);
// Fix Android package structure if a custom package name was provided
if (packageName) {
fixAndroidPackageStructure(root, packageName);
}
// Copy `.gitignore` if the application did not provide one
const ignorePath = join(root, ".gitignore");
if (!existsSync(ignorePath)) {
copyFileSync(
getTemplateFile({
template,
mode: nativeWind ? "nativewind" : "default",
file: "gitignore",
}),
ignorePath,
);
}
const formattedAppName = appName
.trim()
.replace(/([a-z])([A-Z])/g, "$1-$2") // Insert hyphen between lowercase and uppercase letters
.toLowerCase()
.replace(/[^a-z]+/g, "-");
await installTemplate({
appName: formattedAppName,
packageName,
root,
packageManager,
envEnabled,
envPackage,
includeCustomHooks,
customHooks,
includeConsoleRemover,
template,
srcDir,
nativeWind,
skipInstall,
});
if (disableGit) {
console.log("Skipping git initialization.");
} else if (tryGitInit(root)) {
console.log("Initialized a git repository.");
}
if (setupCI) {
initCI(root, packageManager);
}
console.log(
colors.green("\n🎉 Enjoy your new React Native app! Have fun coding! 🎉"),
);
}