-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathbuild-info.js
174 lines (160 loc) · 3.85 KB
/
build-info.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
'use strict';
const fs = require('fs');
const path = require('path');
const gitRepoInfo = require('git-repo-info');
const semver = require('semver');
const NON_SEMVER_IDENTIFIER = /[^0-9A-Za-z-]/g;
/** @type {BuildInfo} */
let cached;
/**
* @param {Options=} options
* @returns {BuildInfo}
*/
function buildInfo(options) {
if (!options && cached) {
return cached;
}
let root = (options && options.root) || path.resolve(__dirname, '..');
let packageVersion = (options && options.packageVersion) || readPackageVersion(root);
let gitInfo = (options && options.gitInfo) || buildGitInfo(root);
let buildInfo = buildFromParts(packageVersion, gitInfo);
if (!options) {
cached = buildInfo;
}
return buildInfo;
}
/**
* @param {string} root
* @returns {GitInfo}
*/
function buildGitInfo(root) {
let info = gitRepoInfo(root);
return {
sha: info.sha,
branch: info.branch,
tag: info.tag,
};
}
/**
* @typedef {Object} GitInfo
* @property {string} sha
* @property {string=} branch
* @property {string=} tag
*/
/**
* @typedef {Object} Options
* @property {string=} root
* @property {string=} packageVersion
* @property {GitInfo=} gitInfo
*/
/**
* @typedef {Object} BuildInfo
* @property {string=} tag
* @property {string=} branch
* @property {string} sha
* @property {string} shortSha
* @property {string=} channel
* @property {string} packageVersion
* @property {string=} tagVersion
* @property {string} version
*/
/**
* Build info object from parts.
* @param {string} packageVersion
* @param {GitInfo} gitInfo
* @returns {BuildInfo}
*/
function buildFromParts(packageVersion, gitInfo) {
let { tag, branch, sha } = gitInfo;
let shortSha = sha.slice(0, 8);
if (tag) {
let tagVersion = parseTagVersion(tag);
return {
tag,
branch: null,
sha,
shortSha,
channel: 'tag',
packageVersion,
tagVersion,
version: tagVersion,
isBuildForTag: true,
};
} else {
let channel =
branch === 'main'
? process.env.BUILD_TYPE === 'alpha'
? 'alpha'
: 'canary'
: branch && escapeSemVerIdentifier(branch);
let version = buildVersion(packageVersion, shortSha, channel);
return {
tag: null,
branch,
sha,
shortSha,
channel,
packageVersion,
tagVersion: null,
version,
isBuildForTag: false,
};
}
}
/**
* Read package version.
* @param {string} root
* @returns {string}
*/
function readPackageVersion(root) {
let pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'));
// use _originalVersion if present if we've already mutated it
return pkg._originalVersion || pkg.version;
}
/**
* @param {string} tag
*/
function parseTagVersion(tag) {
if (tag) {
return semver.parse(tag.replace(/-ember-source$/, '')).version;
}
}
/**
* @param {string} txt
*/
function escapeSemVerIdentifier(txt) {
return txt.replace(NON_SEMVER_IDENTIFIER, '-');
}
/**
* @param {string} packageVersion
* @param {string} sha
* @param {string=} channel
*/
function buildVersion(packageVersion, sha, channel) {
let base = semver.parse(packageVersion);
let major = base.major;
let minor = base.minor;
let patch = base.patch;
let suffix = '';
suffix += toSuffix('-', base.prerelease, channel);
suffix += toSuffix('+', base.build, sha);
return `${major}.${minor}.${patch}${suffix}`;
}
/**
* @param {string} delim
* @param {string[]} identifiers
* @param {string=} identifier
*/
function toSuffix(delim, identifiers, identifier) {
if (identifier) {
identifiers = identifiers.concat([identifier]);
}
if (identifiers.length > 0) {
return delim + identifiers.join('.');
}
return '';
}
module.exports.buildInfo = buildInfo;
module.exports.buildFromParts = buildFromParts;
module.exports.buildVersion = buildVersion;
module.exports.parseTagVersion = parseTagVersion;