From 5fcce566a4d2188f27114cf56ea70d0a9755ace3 Mon Sep 17 00:00:00 2001 From: Szymon Rybczak Date: Tue, 31 Oct 2023 12:33:32 +0100 Subject: [PATCH] feat: create git repository in `init` command (#2088) * feat: create git repository in `init` command * feat: handle creating project in git repository * feat: get react-native version from `node_modules` --- __e2e__/init.test.ts | 1 + .../src/commands/init/createGitRepository.ts | 66 +++++++++++++++++++ packages/cli/src/commands/init/init.ts | 3 + 3 files changed, 70 insertions(+) create mode 100644 packages/cli/src/commands/init/createGitRepository.ts diff --git a/__e2e__/init.test.ts b/__e2e__/init.test.ts index cd52d7b7f..579444602 100644 --- a/__e2e__/init.test.ts +++ b/__e2e__/init.test.ts @@ -21,6 +21,7 @@ function createCustomTemplateFiles() { } const customTemplateCopiedFiles = [ + '.git', 'dir', 'file', 'node_modules', diff --git a/packages/cli/src/commands/init/createGitRepository.ts b/packages/cli/src/commands/init/createGitRepository.ts new file mode 100644 index 000000000..f3b193b34 --- /dev/null +++ b/packages/cli/src/commands/init/createGitRepository.ts @@ -0,0 +1,66 @@ +import {getLoader, logger} from '@react-native-community/cli-tools'; +import execa from 'execa'; +import fs from 'fs'; +import path from 'path'; + +const createGitRepository = async (folder: string) => { + const loader = getLoader(); + + try { + await execa('git', ['--version'], {stdio: 'ignore'}); + } catch { + loader.fail('Unable to initialize Git repo. `git` not in $PATH.'); + return; + } + + try { + await execa('git', ['rev-parse', '--is-inside-work-tree'], { + stdio: 'ignore', + cwd: folder, + }); + loader.succeed( + 'New project is already inside of a Git repo, skipping git init.', + ); + return; + } catch {} + + loader.start('Initializing Git repository'); + + let version; + + try { + version = JSON.parse( + fs.readFileSync( + path.join('node_modules/react-native/package.json'), + 'utf8', + ), + ).version; + } catch {} + + try { + await execa('git', ['init'], {cwd: folder}); + await execa('git', ['branch', '-M', 'main'], {cwd: folder}); + await execa('git', ['add', '.'], {cwd: folder}); + await execa( + 'git', + [ + 'commit', + '-m', + `Initial commit\n\n${ + version ? 'Generated by react-native@' + version : '' + }`, + ], + { + cwd: folder, + }, + ); + loader.succeed(); + } catch (e) { + loader.fail( + 'Could not create an empty Git repository, see debug logs with --verbose', + ); + logger.debug(e as string); + } +}; + +export default createGitRepository; diff --git a/packages/cli/src/commands/init/init.ts b/packages/cli/src/commands/init/init.ts index cea1c0a8c..08033c4d2 100644 --- a/packages/cli/src/commands/init/init.ts +++ b/packages/cli/src/commands/init/init.ts @@ -28,6 +28,7 @@ import {getBunVersionIfAvailable} from '../../tools/bun'; import {getNpmVersionIfAvailable} from '../../tools/npm'; import {getYarnVersionIfAvailable} from '../../tools/yarn'; import {createHash} from 'crypto'; +import createGitRepository from './createGitRepository'; const DEFAULT_VERSION = 'latest'; @@ -340,5 +341,7 @@ export default (async function initialize( await createProject(projectName, directoryName, version, options); const projectFolder = path.join(root, directoryName); + + await createGitRepository(projectFolder); printRunInstructions(projectFolder, projectName); });