|
| 1 | +/** |
| 2 | + * Version Command |
| 3 | + * |
| 4 | + * Display the CLI version and commit SHA |
| 5 | + */ |
| 6 | + |
| 7 | +import { Command } from "@oclif/core"; |
| 8 | +import * as fs from "fs"; |
| 9 | +import * as path from "path"; |
| 10 | +import { execSync } from "child_process"; |
| 11 | +import { fileURLToPath } from "url"; |
| 12 | + |
| 13 | +interface VersionInfo { |
| 14 | + version: string; |
| 15 | + commit: string; |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +function readVersionFile(): VersionInfo | null { |
| 20 | + try { |
| 21 | + // Get the directory of the current module |
| 22 | + const currentFile = fileURLToPath(import.meta.url); |
| 23 | + const currentDir = path.dirname(currentFile); |
| 24 | + |
| 25 | + // Navigate to package root |
| 26 | + const packageRoot = path.resolve(currentDir, "../.."); |
| 27 | + const versionFilePath = path.join(packageRoot, "VERSION"); |
| 28 | + |
| 29 | + if (!fs.existsSync(versionFilePath)) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + const content = fs.readFileSync(versionFilePath, "utf8"); |
| 34 | + const lines = content.trim().split("\n"); |
| 35 | + |
| 36 | + const versionInfo: VersionInfo = { |
| 37 | + version: "unknown", |
| 38 | + commit: "unknown", |
| 39 | + }; |
| 40 | + |
| 41 | + for (const line of lines) { |
| 42 | + const [key, ...valueParts] = line.split("="); |
| 43 | + const value = valueParts.join("=").trim(); |
| 44 | + |
| 45 | + if (key === "version") { |
| 46 | + versionInfo.version = value; |
| 47 | + } else if (key === "commit") { |
| 48 | + versionInfo.commit = value; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return versionInfo; |
| 53 | + } catch (error) { |
| 54 | + return null; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export default class Version extends Command { |
| 59 | + static description = "Display the CLI version and commit SHA"; |
| 60 | + |
| 61 | + static examples = ["<%= config.bin %> <%= command.id %>"]; |
| 62 | + |
| 63 | + async run(): Promise<void> { |
| 64 | + const versionInfo = readVersionFile(); |
| 65 | + |
| 66 | + // Version will always be present when published, for unpublished pull from current env |
| 67 | + if (!versionInfo) { |
| 68 | + this.log(`Version: ${this.config.version} (unpublished)`); |
| 69 | + |
| 70 | + // Attempt to get version from package root |
| 71 | + try { |
| 72 | + // Pull current working dir to pull commit hash |
| 73 | + const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 74 | + const packageRoot = path.resolve(__dirname, ".."); |
| 75 | + |
| 76 | + // Print the short sha from the projects root .git dir |
| 77 | + this.log(`Commit: ${execSync(`cd ${packageRoot} && git rev-parse --short HEAD`, { encoding: "utf8" }).trim()}`); |
| 78 | + } catch { |
| 79 | + // If we can't get the commit then print unknown |
| 80 | + this.log(`Commit: unknown`); |
| 81 | + } |
| 82 | + |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + this.log(`Version: ${versionInfo.version}`); |
| 87 | + this.log(`Commit: ${versionInfo.commit}`); |
| 88 | + } |
| 89 | +} |
| 90 | + |
0 commit comments