Skip to content

Commit

Permalink
feat: create git repository in init command (#2088)
Browse files Browse the repository at this point in the history
* feat: create git repository in `init` command

* feat: handle creating project in git repository

* feat: get react-native version from `node_modules`
  • Loading branch information
szymonrybczak authored Oct 31, 2023
1 parent 1167de7 commit 5fcce56
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions __e2e__/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function createCustomTemplateFiles() {
}

const customTemplateCopiedFiles = [
'.git',
'dir',
'file',
'node_modules',
Expand Down
66 changes: 66 additions & 0 deletions packages/cli/src/commands/init/createGitRepository.ts
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
});

0 comments on commit 5fcce56

Please sign in to comment.