-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
176 lines (155 loc) · 5.26 KB
/
index.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict'
// eagerly load libs that are always used
const fs = require('fs')
const path = require('path')
// lazily load libs that might not be used (depending on async/sync and opts.describe)
let _cp, _util
function cp () {
if (!_cp) _cp = require('child_process')
return _cp
}
function util () {
if (!_util) _util = require('util')
return _util
}
// lazily load promisified functions that might not be used
let _access, _execFile, _readFile
function access () {
if (!_access) _access = util().promisify(fs.access)
return _access
}
function execFile () {
if (!_execFile) _execFile = util().promisify(cp().execFile)
return _execFile
}
function readFile () {
if (!_readFile) _readFile = util().promisify(fs.readFile)
return _readFile
}
// functions to execute a git command
function gitArgs (dir, args) {
return [`--git-dir=${path.join(dir, '.git')}`, `--work-tree=${dir}`].concat(args)
}
async function git (dir, args) {
const { stdout, stderr } = await execFile()('git', gitArgs(dir, args))
if (stderr) throw new Error(String(stderr).trim())
return String(stdout).trim()
}
function gitSync (dir, args) {
const stdout = cp().execFileSync('git', gitArgs(dir, args))
return String(stdout).trim()
}
// functions to read a file
function pathToGitFile (dir, filename) {
return path.join(dir, '.git', filename)
}
async function readGitFile (dir, filename) {
const data = await readFile()(pathToGitFile(dir, filename), 'utf8')
return String(data).trim()
}
function readGitFileSync (dir, filename) {
const data = fs.readFileSync(pathToGitFile(dir, filename), 'utf8')
return String(data).trim()
}
function getOpts (opts) {
return { fallbackToSha: true, ...opts }
}
// valid opts:
// - dir (string): in case `process.cwd()` isn't suitable
// - describe (boolean): use `git describe --tags` instead of `git rev-parse HEAD`
// - fallbackToSha (boolean): if opts.describe and no tags found, fallback to latest commit sha
const nextBuildId = async opts => {
opts = getOpts(opts)
const inputDir = path.resolve(process.cwd(), opts.dir || '.')
let dir = inputDir
// dir may not be the project root so look for .git dir in parent dirs too
const root = path.parse(dir).root
let attempts = 0 // protect against infinite tight loop if libs misbehave
while (dir !== root && attempts < 999) {
attempts++
try {
await access()(path.join(dir, '.git'), fs.constants.R_OK)
break
} catch (_) {
dir = path.dirname(dir)
}
}
if (dir === root || attempts >= 999) dir = inputDir
// if opts.describe, use `git describe --tags`
let id
if (opts.describe) {
try {
id = await git(dir, ['describe', '--tags'])
if (!id) throw new Error('Output of `git describe --tags` was empty!')
return id
} catch (err) {
if (!opts.fallbackToSha) throw err
}
}
// try file system, suggestion by @sheerun here: https://github.com/nexdrew/next-build-id/issues/17#issuecomment-482799872
// 1. read .git/HEAD to find out ref, the result is something like `ref: refs/heads/master`
// 2. read this file to find our current commit (e.g. .git/refs/heads/master)
try {
const head = await readGitFile(dir, 'HEAD')
let refi = head.indexOf('ref:')
if (refi === -1) refi = 0
const endi = head.indexOf('\n', refi + 4) + 1
const ref = head.slice(refi + 4, endi || undefined).trim()
if (ref) {
id = await readGitFile(dir, ref)
if (id) return id
}
} catch (_) {}
// fallback to `git rev-parse HEAD`
id = await git(dir, ['rev-parse', 'HEAD'])
if (!id) throw new Error('Output of `git rev-parse HEAD` was empty!')
return id
}
nextBuildId.sync = opts => {
opts = getOpts(opts)
const inputDir = path.resolve(process.cwd(), opts.dir || '.')
let dir = inputDir
// dir may not be the project root so look for .git dir in parent dirs too
const root = path.parse(dir).root
let attempts = 0 // protect against infinite tight loop if libs misbehave
while (dir !== root && attempts < 999) {
attempts++
try {
fs.accessSync(path.join(dir, '.git'), fs.constants.R_OK)
break
} catch (_) {
dir = path.dirname(dir)
}
}
if (dir === root || attempts >= 999) dir = inputDir
// if opts.describe, use `git describe --tags`
let id
if (opts.describe) {
try {
id = gitSync(dir, ['describe', '--tags'])
if (!id) throw new Error('Output of `git describe --tags` was empty!')
return id
} catch (err) {
if (!opts.fallbackToSha) throw err
}
}
// try file system, suggestion by @sheerun here: https://github.com/nexdrew/next-build-id/issues/17#issuecomment-482799872
// 1. read .git/HEAD to find out ref, the result is something like `ref: refs/heads/master`
// 2. read this file to find our current commit (e.g. .git/refs/heads/master)
try {
const head = readGitFileSync(dir, 'HEAD')
let refi = head.indexOf('ref:')
if (refi === -1) refi = 0
const endi = head.indexOf('\n', refi + 4) + 1
const ref = head.slice(refi + 4, endi || undefined).trim()
if (ref) {
id = readGitFileSync(dir, ref)
if (id) return id
}
} catch (_) {}
// fallback to `git rev-parse HEAD`
id = gitSync(dir, ['rev-parse', 'HEAD'])
if (!id) throw new Error('Output of `git rev-parse HEAD` was empty!')
return id
}
module.exports = nextBuildId