|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import {fileURLToPath} from "node:url"; |
| 4 | +import {findUpSync} from "find-up"; |
| 5 | +import {readAndGetGitData} from "./gitData/index.js"; |
| 6 | + |
| 7 | +// Global variable __dirname no longer available in ES6 modules. |
| 8 | +// Solutions: https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules |
| 9 | +// eslint-disable-next-line @typescript-eslint/naming-convention |
| 10 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 11 | + |
| 12 | +type VersionJson = { |
| 13 | + /** "0.28.2" */ |
| 14 | + version: string; |
| 15 | +}; |
| 16 | + |
| 17 | +const BRANCH_IGNORE = /^(HEAD|master|unstable|main)$/; |
| 18 | + |
| 19 | +/** |
| 20 | + * Gathers all information on package version including Git data. |
| 21 | + * @returns a version string, e.g. |
| 22 | + * - Stable release: `v0.36.0/80c248bb` |
| 23 | + * - Dev release: `v0.36.0-dev.80c248bb/80c248bb` |
| 24 | + * - Test branch: `v0.36.0/developer-feature/80c248bb` |
| 25 | + */ |
| 26 | +export function getVersionData(): { |
| 27 | + version: string; |
| 28 | + commit: string; |
| 29 | +} { |
| 30 | + const parts: string[] = []; |
| 31 | + |
| 32 | + /** Returns local version from `lerna.json` or `package.json` as `"0.28.2"` */ |
| 33 | + const localVersion = readCliPackageJson() || readVersionFromLernaJson(); |
| 34 | + if (localVersion) { |
| 35 | + parts.push(`v${localVersion}`); |
| 36 | + } |
| 37 | + |
| 38 | + const {branch, commit} = readAndGetGitData(); |
| 39 | + |
| 40 | + // Add branch only if not present and not an ignore value |
| 41 | + if (branch && !BRANCH_IGNORE.test(branch)) parts.push(branch); |
| 42 | + |
| 43 | + // Add commit only if present. 7 characters to be consistent with Github |
| 44 | + if (commit) { |
| 45 | + const commitShort = commit.slice(0, 7); |
| 46 | + // Don't add commit if it's already in the version string (dev versions) |
| 47 | + if (!localVersion || !localVersion.includes(commitShort)) { |
| 48 | + parts.push(commitShort); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return { |
| 53 | + // Guard against empty parts array |
| 54 | + version: parts.length > 0 ? parts.join("/") : "unknown", |
| 55 | + commit, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +/** Read version information from lerna.json */ |
| 60 | +function readVersionFromLernaJson(): string | undefined { |
| 61 | + const filePath = findUpSync("lerna.json", {cwd: __dirname}); |
| 62 | + if (!filePath) return undefined; |
| 63 | + |
| 64 | + const lernaJson = JSON.parse(fs.readFileSync(filePath, "utf8")) as VersionJson; |
| 65 | + return lernaJson.version; |
| 66 | +} |
| 67 | + |
| 68 | +/** Read version information from package.json */ |
| 69 | +function readCliPackageJson(): string | undefined { |
| 70 | + const filePath = findUpSync("package.json", {cwd: __dirname}); |
| 71 | + if (!filePath) return undefined; |
| 72 | + |
| 73 | + const packageJson = JSON.parse(fs.readFileSync(filePath, "utf8")) as VersionJson; |
| 74 | + return packageJson.version; |
| 75 | +} |
0 commit comments