Skip to content

Commit

Permalink
refactor(version): Prefer JSON, split out getRemoteVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jun 8, 2024
1 parent b55736b commit 961dc35
Showing 1 changed file with 43 additions and 19 deletions.
62 changes: 43 additions & 19 deletions bids-validator/src/version.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,60 @@
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)
} else if (url.startsWith('https://deno.land/x/')) {
// Retrieve version X from https://deno.land/x/bids-validator@X/version.ts
return url.split('@')[1].split('/')[0]
} else if (url.startsWith('https://raw.githubusercontent.com')) {
// Retrieve version X from https://raw.githubusercontent.com/bids-standard/bids-validator/X/bids-validator/src/version.ts
return url.split('/bids-validator/')[1]
/**
* Determine the version of the currently running script.
*
* The version is determined by the following rules:
*
* 1. Search for a hard-coded version populated by git-archive or the build.
* 2. If the script is running from a local file, the version is determined by
* the output of `git describe --tags --always` in the script's directory.
* 3. If the script is running from a remote URL, the version is determined by
* the path of the URL. In case of GitHub, the version can be any git ref.
* In the case of deno.land, the tag name should be available and will be parsed.
*
* If no version can be determined, the URL of the script is returned.
*
* @returns The version of the script.
*
*/
export async function getVersion(): Promise<string> {
// Hard-coded JSON wins
let version = getArchiveVersion()
if (version) { return version }

const url = new URL(Deno.mainModule)
if (url.protocol === 'file:') {
version = await getLocalVersion(url.pathname.split('/').slice(0, -1).join('/'))
if (version) { return version }
} else if (url.protocol === 'https:' || url.protocol === 'http:') {
version = getRemoteVersion(url)
if (version) { return version }
}
return url
return url.href

Check warning on line 33 in bids-validator/src/version.ts

View check run for this annotation

Codecov / codecov/patch

bids-validator/src/version.ts#L30-L33

Added lines #L30 - L33 were not covered by tests
}

async function getLocalVersion(path: string): string {
async function getLocalVersion(path: string): Promise<string> {
const p = Deno.run({
cmd: ['git', 'describe', '--tags', '--always'],
stdout: 'piped',
stderr: 'piped',
cwd: path,
})
const description = new TextDecoder().decode(await p.output()).trim()
p.close()
return description
}

function getRemoteVersion(url: URL): string | undefined {
if (url.hostname === 'deno.land') {
// https://deno.land/x/bids-validator@<ver>/bids-validator.ts
return url.pathname.split('@')[1].split('/')[0]
} else if (url.hostname === 'raw.githubusercontent.com') {
// https://raw.githubusercontent.com/<org>/bids-validator/<ver>/bids-validator/src/bids-validator.ts
return url.pathname.split('/bids-validator/')[1]
}
return undefined
}

Check warning on line 56 in bids-validator/src/version.ts

View check run for this annotation

Codecov / codecov/patch

bids-validator/src/version.ts#L47-L56

Added lines #L47 - L56 were not covered by tests

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

0 comments on commit 961dc35

Please sign in to comment.