Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default async function generateExampleApp({
'--version',
reactNativeVersion,
'--skip-install',
'--skip-git-init',
'--pm',
'npm',
];
Expand Down
23 changes: 22 additions & 1 deletion packages/create-react-native-library/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,28 @@ async function create(_argv: Args) {
folder,
});
} else {
await createInitialGitCommit(folder);
spinner.text = 'Initializing git repository';

try {
const abortController = new AbortController();

// Creating git repository can get stuck in some cases,
// e.g. if git asks for ssh passphrase.
// We abort it after a timeout so that this doesn't hang forever.
await Promise.race([
createInitialGitCommit(folder, abortController.signal),
new Promise<void>((_resolve, reject) => {
setTimeout(() => {
const error = new Error('Creating git repository took too long');

abortController.abort(error.message);
reject(error);
}, 5000);
}),
]);
} catch (error) {
spinner.warn('Failed to create git repository');
}

printSuccessMessage();

Expand Down
35 changes: 16 additions & 19 deletions packages/create-react-native-library/src/utils/initialCommit.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import { spawn } from './spawn';

export async function createInitialGitCommit(folder: string) {
let isInGitRepo = false;

try {
isInGitRepo =
(await spawn('git', ['rev-parse', '--is-inside-work-tree'])) === 'true';
} catch (e) {
// Ignore error
}
export async function createInitialGitCommit(
folder: string,
signal?: AbortSignal
) {
const isInGitRepo =
(await spawn('git', ['rev-parse', '--is-inside-work-tree'], {
cwd: folder,
signal,
})) === 'true';

if (!isInGitRepo) {
try {
await spawn('git', ['init'], { cwd: folder });
await spawn('git', ['branch', '-M', 'main'], { cwd: folder });
await spawn('git', ['add', '.'], { cwd: folder });
await spawn('git', ['commit', '-m', 'chore: initial commit'], {
cwd: folder,
});
} catch (e) {
// Ignore error
}
await spawn('git', ['init'], { cwd: folder, signal });
await spawn('git', ['branch', '-M', 'main'], { cwd: folder, signal });
await spawn('git', ['add', '.'], { cwd: folder, signal });
await spawn('git', ['commit', '-m', 'chore: initial commit'], {
cwd: folder,
signal,
});
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"verbatimModuleSyntax": true
},
"exclude": [
"lib",
"**/lib",
"packages/react-native-builder-bob/src/__fixtures__",
"packages/create-react-native-library/templates"
]
Expand Down
Loading