import execa from 'execa' ; export class GitRepository { constructor ( private readonly cwd ) { execa . sync ( 'git' , [ '--version' ] , { stdio : 'ignore' , } ) ; } public async isInGitRepository ( ) { try { await execa ( 'git' , [ 'rev-parse' , '--is-inside-work-tree' ] , { cwd : this . cwd , stdio : 'ignore' , } ) ; return true ; } catch ( e ) { return false ; } } public async initializeGitRepository ( ) { await execa ( 'git' , [ 'init' ] , { cwd : this . cwd , stdio : 'ignore' , } ) ; await execa ( 'git' , [ 'add' , '.' ] , { cwd : this . cwd , stdio : 'ignore' , } ) ; await execa ( 'git' , [ 'commit' , '-m="Initial commit"' ] , { cwd : this . cwd , stdio : 'ignore' , } ) ; } public async getCurrentBranch ( ) { const { stdout : currentBranch } = await execa ( 'git' , [ 'rev-parse' , '--abbrev-ref' , 'HEAD' ] , { cwd : this . cwd , } ) ; return currentBranch ; } public async getAuthor ( ) { const { stdout : author } = await execa ( 'git' , [ 'config' , '--global' , 'user.name' ] , { cwd : this . cwd , } ) ; return author . trim ( ) ; } }