Skip to content

Commit

Permalink
feat(version): Find version from git archive
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jun 10, 2024
1 parent a019092 commit ec36ec5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bids-validator/src/.git-meta.json export-subst
6 changes: 6 additions & 0 deletions bids-validator/src/.git-meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"node": "$Format:%H$",
"date": "$Format:%cI$",
"description": "$Format:%(describe:tags=true,match=*[0-9]*)$",
"refnames": "$Format:%D$"
}
15 changes: 14 additions & 1 deletion bids-validator/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import gitmeta from './.git-meta.json' with { type: 'json' }

export async function getVersion(): string {
const url = import.meta.url
if (url.startsWith('file://')) {
const archiveVersion = getArchiveVersion()
if (archiveVersion) {
return archiveVersion
}
// Get parent directory of current file, without file:/
const parent = url.slice(7).split('/').slice(0, -1).join('/')
return await getLocalVersion(parent)
Expand All @@ -14,7 +20,7 @@ export async function getVersion(): string {
return url
}

export async function getLocalVersion(path: string): string {
async function getLocalVersion(path: string): string {
const p = Deno.run({
cmd: ['git', 'describe', '--tags', '--always'],
stdout: 'piped',
Expand All @@ -24,3 +30,10 @@ export async function getLocalVersion(path: string): string {
const description = new TextDecoder().decode(await p.output()).trim()
return description
}

function getArchiveVersion(): string | undefined {
if (!gitmeta.description.startsWith('$Format:')) {
return gitmeta.description
}
return undefined
}

0 comments on commit ec36ec5

Please sign in to comment.