-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
38 lines (31 loc) · 1.07 KB
/
index.mjs
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
/**
* Exports version of running NodeJs application extracted out of `version`
* property of appʼs package.json
*
* @module node-pkgver
*/
import { existsSync, readFileSync } from 'fs'
import { dirname, join } from 'path'
import { fileURLToPath } from 'url'
let version = null
let path = dirname(fileURLToPath(import.meta.url))
while (path !== dirname(path)) {
path = dirname(path)
const packageJson = join(path, 'package.json')
if (existsSync(packageJson)) {
try {
version = JSON.parse(readFileSync(packageJson, 'utf8')).version
} finally {
typeof version === 'string' && version.length || (version = null)
}
break
}
}
/**
* @export {string|null} String containing version of running NodeJs application
* extracted out of `version` property of appʼs package.json
* or Null if `version` property is empty or missing from
* appʼs package.json, or appʼs package.json is missing per
* se
*/
export default version