1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9
10const {exec} = require('shelljs');
11
12const VERSION_REGEX = /^v?((\d+)\.(\d+)\.(\d+)(?:-(.+))?)$/;
13
14function parseVersion(versionStr) {
15  const match = versionStr.match(VERSION_REGEX);
16  if (!match) {
17    throw new Error(
18      `You must pass a correctly formatted version; couldn't parse ${versionStr}`,
19    );
20  }
21  const [, version, major, minor, patch, prerelease] = match;
22  return {
23    version,
24    major,
25    minor,
26    patch,
27    prerelease,
28  };
29}
30
31function isReleaseBranch(branch) {
32  return branch.endsWith('-stable');
33}
34
35function getBranchName() {
36  return exec('git rev-parse --abbrev-ref HEAD', {
37    silent: true,
38  }).stdout.trim();
39}
40
41function isTaggedLatest(commitSha) {
42  return (
43    exec(`git rev-list -1 latest | grep ${commitSha}`, {
44      silent: true,
45    }).stdout.trim() === commitSha
46  );
47}
48
49module.exports = {
50  getBranchName,
51  isTaggedLatest,
52  parseVersion,
53  isReleaseBranch,
54};
55