Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: resolveGitDir uses execGit
- Loading branch information
Showing
3 changed files
with
34 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
|
||
const debug = require('debug')('lint-staged:git') | ||
const execa = require('execa') | ||
const path = require('path') | ||
|
||
function getAbsolutePath(dir) { | ||
return path.isAbsolute(dir) ? dir : path.resolve(dir) | ||
} | ||
|
||
module.exports = async function execGit(cmd, options = {}) { | ||
const cwd = options.cwd || process.cwd() | ||
debug('Running git command', cmd) | ||
try { | ||
const { stdout } = await execa('git', [].concat(cmd), { | ||
...options, | ||
cwd: getAbsolutePath(cwd) | ||
}) | ||
return stdout | ||
} catch (err) { | ||
throw new Error(err) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,14 @@ | ||
'use strict' | ||
|
||
const execa = require('execa') | ||
const execGit = require('./execGit') | ||
const printErrors = require('./printErrors') | ||
|
||
module.exports = async function resolveGitDir() { | ||
return (await execa('git', ['rev-parse', '--show-toplevel'])).stdout | ||
try { | ||
const gitDir = await execGit(['rev-parse', '--show-toplevel']) | ||
return gitDir | ||
} catch (error) { | ||
printErrors(error) | ||
return null | ||
} | ||
} |