Skip to content

Commit 5d0e5cf

Browse files
authored
fix: abort git repository creation if it takes too long (#861)
1 parent 24a38b6 commit 5d0e5cf

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

packages/create-react-native-library/src/exampleApp/generateExampleApp.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default async function generateExampleApp({
6868
'--version',
6969
reactNativeVersion,
7070
'--skip-install',
71+
'--skip-git-init',
7172
'--pm',
7273
'npm',
7374
];

packages/create-react-native-library/src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,28 @@ async function create(_argv: Args) {
160160
folder,
161161
});
162162
} else {
163-
await createInitialGitCommit(folder);
163+
spinner.text = 'Initializing git repository';
164+
165+
try {
166+
const abortController = new AbortController();
167+
168+
// Creating git repository can get stuck in some cases,
169+
// e.g. if git asks for ssh passphrase.
170+
// We abort it after a timeout so that this doesn't hang forever.
171+
await Promise.race([
172+
createInitialGitCommit(folder, abortController.signal),
173+
new Promise<void>((_resolve, reject) => {
174+
setTimeout(() => {
175+
const error = new Error('Creating git repository took too long');
176+
177+
abortController.abort(error.message);
178+
reject(error);
179+
}, 5000);
180+
}),
181+
]);
182+
} catch (error) {
183+
spinner.warn('Failed to create git repository');
184+
}
164185

165186
printSuccessMessage();
166187

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
import { spawn } from './spawn';
22

3-
export async function createInitialGitCommit(folder: string) {
4-
let isInGitRepo = false;
5-
6-
try {
7-
isInGitRepo =
8-
(await spawn('git', ['rev-parse', '--is-inside-work-tree'])) === 'true';
9-
} catch (e) {
10-
// Ignore error
11-
}
3+
export async function createInitialGitCommit(
4+
folder: string,
5+
signal?: AbortSignal
6+
) {
7+
const isInGitRepo =
8+
(await spawn('git', ['rev-parse', '--is-inside-work-tree'], {
9+
cwd: folder,
10+
signal,
11+
})) === 'true';
1212

1313
if (!isInGitRepo) {
14-
try {
15-
await spawn('git', ['init'], { cwd: folder });
16-
await spawn('git', ['branch', '-M', 'main'], { cwd: folder });
17-
await spawn('git', ['add', '.'], { cwd: folder });
18-
await spawn('git', ['commit', '-m', 'chore: initial commit'], {
19-
cwd: folder,
20-
});
21-
} catch (e) {
22-
// Ignore error
23-
}
14+
await spawn('git', ['init'], { cwd: folder, signal });
15+
await spawn('git', ['branch', '-M', 'main'], { cwd: folder, signal });
16+
await spawn('git', ['add', '.'], { cwd: folder, signal });
17+
await spawn('git', ['commit', '-m', 'chore: initial commit'], {
18+
cwd: folder,
19+
signal,
20+
});
2421
}
2522
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"verbatimModuleSyntax": true
3333
},
3434
"exclude": [
35-
"lib",
35+
"**/lib",
3636
"packages/react-native-builder-bob/src/__fixtures__",
3737
"packages/create-react-native-library/templates"
3838
]

0 commit comments

Comments
 (0)