-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathversion.ts
53 lines (48 loc) · 1.66 KB
/
version.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import gitmeta from './.git-meta.json' with { type: 'json' }
import denojson from '../deno.json' with { type: 'json' }
import { dirname } from '@std/path'
/**
* 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. Fall back to the static metadata in `deno.json`, which should correspond
* to the most recent dev tag.
*
* @returns The version of the script.
*/
export async function getVersion(): Promise<string> {
// Hard-coded JSON wins
let version = getArchiveVersion()
if (version) return version
// Local git repository
const url = new URL(Deno.mainModule)
if (url.protocol === 'file:') {
version = await getLocalVersion(dirname(url.pathname))
if (version) return version
}
// Fall back to static metadata
return denojson.version
}
async function getLocalVersion(path: string): Promise<string> {
// safe.directory setting so we could still operate from another user
try {
const command = new Deno.Command('git', {
args: ['-C', path, '-c', 'safe.directory=*', 'describe', '--tags', '--always'],
})
const { success, stdout } = await command.output()
const description = new TextDecoder().decode(stdout).trim()
return description
} catch (err) {
return ''
}
}
function getArchiveVersion(): string | undefined {
if (!gitmeta.description.startsWith('$Format:')) {
return gitmeta.description
}
return undefined
}