-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance
createGitReader()
to automatically attempt adding the `.git…
…` folder, add `isGitDir()` and `resolveGitDir()`
- Loading branch information
Showing
4 changed files
with
123 additions
and
5 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,44 @@ | ||
import { stat } from 'fs/promises'; | ||
import { join, resolve } from 'path'; | ||
|
||
const test = { | ||
objects: 'dir', | ||
refs: 'dir', | ||
HEAD: 'file', | ||
config: 'file' | ||
}; | ||
|
||
export async function isGitDir(dir: string) { | ||
try { | ||
const stats = await Promise.all(Object.keys(test).map((name) => stat(join(dir, name)))); | ||
|
||
return Object.values(test).every((type, idx) => | ||
type === 'dir' ? stats[idx].isDirectory() : stats[idx].isFile() | ||
); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
export async function resolveGitDir(dir: string) { | ||
const absdir = resolve(process.cwd(), dir); | ||
let absdirStat; | ||
|
||
try { | ||
absdirStat = await stat(absdir); | ||
} catch { | ||
throw new Error(`No such directory "${absdir}"`); | ||
} | ||
|
||
if (!absdirStat.isDirectory()) { | ||
throw new Error(`Not a directory "${absdir}"`); | ||
} | ||
|
||
try { | ||
const dotGitDir = join(absdir, '.git'); | ||
await stat(dotGitDir); | ||
return dotGitDir; | ||
} catch {} | ||
|
||
return absdir; | ||
} |
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,64 @@ | ||
import assert from 'assert'; | ||
import path from 'path'; | ||
import { isGitDir, resolveGitDir } from '@discoveryjs/scan-git'; | ||
|
||
const cwd = process.cwd(); | ||
const gitdir = path.join(cwd, '.git'); | ||
|
||
describe('Git Directory Utilities', () => { | ||
describe('isGitDir()', () => { | ||
it('should return false for non-Git directory', async () => { | ||
const actual = await isGitDir(cwd); | ||
assert.strictEqual(actual, false); | ||
}); | ||
|
||
it('should return true for valid Git directory', async () => { | ||
const actual = await isGitDir(gitdir); | ||
assert.strictEqual(actual, true); | ||
}); | ||
}); | ||
|
||
describe('resolveGitDir()', () => { | ||
describe('absolute paths', () => { | ||
it('should resolve current working directory to Git directory', async () => { | ||
const actual = await resolveGitDir(cwd); | ||
assert.strictEqual(actual, gitdir); | ||
}); | ||
|
||
it('should resolve .git directory path to itself', async () => { | ||
const actual = await resolveGitDir(gitdir); | ||
assert.strictEqual(actual, gitdir); | ||
}); | ||
}); | ||
|
||
describe('relative paths', () => { | ||
it('should resolve empty path to Git directory', async () => { | ||
const actual = await resolveGitDir(''); | ||
assert.strictEqual(actual, gitdir); | ||
}); | ||
|
||
it('should resolve .git relative path to Git directory', async () => { | ||
const actual = await resolveGitDir('.git'); | ||
assert.strictEqual(actual, gitdir); | ||
}); | ||
}); | ||
|
||
it('should throws for non existing paths', () => { | ||
const testdir = path.resolve(cwd, 'non-exists'); | ||
|
||
return assert.rejects( | ||
() => resolveGitDir(testdir), | ||
(e: Error) => e.message.endsWith(`No such directory "${testdir}"`) | ||
); | ||
}); | ||
|
||
it('should throws for non directory paths', () => { | ||
const testpath = path.resolve(cwd, 'package.json'); | ||
|
||
return assert.rejects( | ||
() => resolveGitDir(testpath), | ||
(e: Error) => e.message.endsWith(`Not a directory "${testpath}"`) | ||
); | ||
}); | ||
}); | ||
}); |