This repository was archived by the owner on Apr 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathversion
More file actions
executable file
·87 lines (75 loc) · 2.74 KB
/
Copy pathversion
File metadata and controls
executable file
·87 lines (75 loc) · 2.74 KB
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
#!/usr/bin/env node
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var execSync = require('child_process').execSync;
var fs = require('fs');
// Regex for the string returned by getNpmVersion().
// The subexpression match is the npm version.
var npmRegex = /^(\d+(?:\.\d+)+)$/;
// Regex for the string returned by getGitVersion().
// The 1st subexpression match is the most recent tagged version.
// The 2nd subexpression match exists if either commits were made since the most
// recent tagged version, or there are uncommitted or untracked changes.
var gitRegex = /^v(\d+(?:\.\d+)+)((?:-\d+-g([\da-f]+))?(?:-dirty)?)?$/;
// Return npm package version declared by package.json.
function getNpmVersion() {
return JSON.parse(fs.readFileSync('package.json', 'utf-8')).version;
}
// Return the output of `git describe --tags --dirty`, modified by the result
// of `git status --porcelain`. The latter is necessary because `git describe`
// ignores untracked files, but we want their presence to be considered dirty.
function getGitVersion() {
var versionStr =
execSync("git describe --tags --dirty", { encoding: 'utf-8' }).trim();
if (!/-dirty$/.test(versionStr) &&
execSync("git status --porcelain", { encoding: 'utf-8' })) {
versionStr += '-dirty';
}
return versionStr;
}
function getVersion() {
// Check that the npm version has the expected format.
var npmVersion = getNpmVersion();
var npmMatch = npmRegex.exec(npmVersion);
if (!npmMatch) {
throw new Error("Invalid npm version: " + npmVersion);
}
// Check that the git version has the expected format.
var gitVersion = getGitVersion();
var gitMatch = gitRegex.exec(gitVersion);
if (!gitMatch) {
throw new Error("Invalid git version: " + gitVersion);
}
// Check that npm and git agree on the version.
if (npmMatch[1] !== gitMatch[1]) {
throw new Error("Version mismatch: npm says " + npmMatch[1] + " but git says " + gitMatch[1]);
}
if (gitMatch[2]) {
// Development version.
return npmMatch[1] + '-dev';
} else {
// Release version.
return npmMatch[1];
}
}
try {
console.log(getVersion());
process.exit(0);
} catch (err) {
console.error(err);
process.exit(1);
}