Skip to content

Commit b7144e4

Browse files
committed
build: add version stamp to release output
* Adds a version stamp to the `package.json` in the release output. The version stamp consists of the current `HEAD` commit sha, and the branch name. Closes #12407
1 parent 877de56 commit b7144e4

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

tools/gulp/tasks/publish.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ task(':publish', async () => {
5252
if (!version.match(validVersionRegex)) {
5353
console.log(red(`Error: Cannot publish due to an invalid version name. Version "${version}" ` +
5454
`is not following our semver format.`));
55-
console.log(yellow(`A version should follow this format: d.d.d, d.d.d-beta.x, d.d.d-alpha.x, ` +
56-
`d.d.d-rc.x`));
55+
console.log(yellow(`A version should follow this format: X.X.X, X.X.X-beta.X, X.X.X-alpha.X, ` +
56+
`X.X.X-rc.X`));
5757
return;
5858
}
5959

tools/package-tools/build-release.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {copyFiles} from './copy-files';
77
import {createEntryPointPackageJson} from './entry-point-package-json';
88
import {inlinePackageMetadataFiles} from './metadata-inlining';
99
import {createMetadataReexportFile} from './metadata-reexport';
10+
import {insertPackageJsonVersionStamp} from './package-version-stamp';
1011
import {createTypingsReexportFile} from './typings-reexport';
1112
import {replaceVersionPlaceholders} from './version-placeholders';
1213

@@ -48,6 +49,8 @@ export function composeRelease(buildPackage: BuildPackage) {
4849
copyFiles(sourceDir, 'package.json', releasePath);
4950

5051
replaceVersionPlaceholders(releasePath);
52+
insertPackageJsonVersionStamp(join(releasePath, 'package.json'));
53+
5154
createTypingsReexportFile(releasePath, './typings/index', name);
5255
createMetadataReexportFile(releasePath, './typings/index', name, importAsName);
5356

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {writeFileSync} from 'fs';
2+
import {spawnSync} from 'child_process';
3+
import {buildConfig} from './build-config';
4+
5+
/**
6+
* Inserts the a version stamp, which consists of the current commit SHA and the current
7+
* branch name, into the specified package.json file.
8+
*
9+
* This makes it easy to quickly verify the exact snapshot from which the release originated.
10+
*/
11+
export function insertPackageJsonVersionStamp(packageJsonPath: string) {
12+
const packageJson = require(packageJsonPath);
13+
14+
packageJson['_gitCommitStamp'] = getCurrentCommitSha();
15+
packageJson['_gitBranchStamp'] = getCurrentBranchName();
16+
17+
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
18+
}
19+
20+
/** Returns the commit SHA for the current git HEAD of the project. */
21+
function getCurrentCommitSha(): string {
22+
return spawnSync('git', ['rev-parse', 'HEAD'], {cwd: buildConfig.projectDir})
23+
.stdout.toString().trim();
24+
}
25+
26+
/** Returns the name of the currently checked out branch of the project. */
27+
function getCurrentBranchName(): string {
28+
return spawnSync('git', ['symbolic-ref', '--short', 'HEAD'], {cwd: buildConfig.projectDir})
29+
.stdout.toString().trim();
30+
}

0 commit comments

Comments
 (0)