version-info: don't throw on import if git does not exist

This commit is contained in:
dumbmoron 2024-08-03 16:28:43 +00:00
parent 985f6e385d
commit a7ab4a9706
No known key found for this signature in database

View file

@ -17,13 +17,14 @@ const findFile = (file) => {
const root = findFile('.git'); const root = findFile('.git');
const pack = findFile('package.json'); const pack = findFile('package.json');
const readGit = (filename) => {
if (!root) { if (!root) {
throw 'no git repository root found'; throw 'no git repository root found';
} else if (!pack) {
throw 'no package root found';
} }
const readGit = (filename) => readFile(join(root, filename), 'utf8'); return readFile(join(root, filename), 'utf8');
}
export const getCommit = async () => { export const getCommit = async () => {
return (await readGit('.git/logs/HEAD')) return (await readGit('.git/logs/HEAD'))
@ -65,6 +66,10 @@ export const getRemote = async () => {
} }
export const getVersion = async () => { export const getVersion = async () => {
if (!pack) {
throw 'no package root found';
}
const { version } = JSON.parse( const { version } = JSON.parse(
await readFile(join(pack, 'package.json'), 'utf8') await readFile(join(pack, 'package.json'), 'utf8')
); );