-
Notifications
You must be signed in to change notification settings - Fork 907
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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`
- Loading branch information
1 parent
1167de7
commit 5fcce56
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters