Skip to content

Commit 03b6d3d

Browse files
authored
feat: create VERSION file on publish and expose via version command (#20)
1 parent d9a975c commit 03b6d3d

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

.github/workflows/release-dev.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ jobs:
7070
run: |
7171
pnpm run build
7272
73+
- name: Get short sha
74+
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV
75+
env:
76+
GITHUB_SHA: ${{ github.sha }}
77+
78+
- name: Generate SDK VERSION file
79+
working-directory: ./packages/sdk
80+
env:
81+
PACKAGE_VERSION: ${{ env.PACKAGE_VERSION }}
82+
GITHUB_SHA: ${{ env.SHORT_SHA }}
83+
run: |
84+
node scripts/generate-version.js
85+
86+
- name: Generate CLI VERSION file
87+
working-directory: ./packages/cli
88+
env:
89+
PACKAGE_VERSION: ${{ env.PACKAGE_VERSION }}
90+
GITHUB_SHA: ${{ env.SHORT_SHA }}
91+
run: |
92+
node scripts/generate-version.js
93+
7394
- name: Update SDK package.json for publishing
7495
working-directory: ./packages/sdk
7596
run: |

.github/workflows/release-prod.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ jobs:
112112
run: |
113113
pnpm run build
114114
115+
- name: Get short sha
116+
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV
117+
env:
118+
GITHUB_SHA: ${{ github.sha }}
119+
120+
- name: Generate SDK VERSION file
121+
working-directory: ./packages/sdk
122+
env:
123+
PACKAGE_VERSION: ${{ env.PACKAGE_VERSION }}
124+
GITHUB_SHA: ${{ env.SHORT_SHA }}
125+
run: |
126+
node scripts/generate-version.js
127+
128+
- name: Generate CLI VERSION file
129+
working-directory: ./packages/cli
130+
env:
131+
PACKAGE_VERSION: ${{ env.PACKAGE_VERSION }}
132+
GITHUB_SHA: ${{ env.SHORT_SHA }}
133+
run: |
134+
node scripts/generate-version.js
135+
115136
- name: Update SDK package.json for publishing
116137
working-directory: ./packages/sdk
117138
run: |

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"files": [
66
"dist",
77
"bin",
8+
"VERSION",
89
"README.md"
910
],
1011
"bin": {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
import { fileURLToPath } from "url";
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8+
const packageRoot = path.resolve(__dirname, "..");
9+
10+
// Get version and commit from environment variables
11+
const version = process.env.PACKAGE_VERSION || process.env.VERSION?.replace(/^v/, "") || "unknown";
12+
const commit = process.env.GITHUB_SHA || process.env.COMMIT_SHA || "unknown";
13+
14+
// Create VERSION file content
15+
const versionContent = `version=${version}
16+
commit=${commit}
17+
`;
18+
19+
// Write VERSION file to package root
20+
const versionFilePath = path.join(packageRoot, "VERSION");
21+
fs.writeFileSync(versionFilePath, versionContent, "utf8");
22+
23+
console.log(`Generated VERSION file: ${versionFilePath}`);
24+
console.log(` version: ${version}`);
25+
console.log(` commit: ${commit}`);
26+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+

packages/sdk/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"types": "dist/index.d.ts",
88
"files": [
99
"dist",
10+
"VERSION",
1011
"README.md"
1112
],
1213
"exports": {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env node
2+
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
import { fileURLToPath } from "url";
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8+
const packageRoot = path.resolve(__dirname, "..");
9+
10+
// Get version and commit from environment variables
11+
const version = process.env.PACKAGE_VERSION || process.env.VERSION?.replace(/^v/, "") || "unknown";
12+
const commit = process.env.GITHUB_SHA || process.env.COMMIT_SHA || "unknown";
13+
14+
// Create VERSION file content
15+
const versionContent = `version=${version}
16+
commit=${commit}
17+
`;
18+
19+
// Write VERSION file to package root
20+
const versionFilePath = path.join(packageRoot, "VERSION");
21+
fs.writeFileSync(versionFilePath, versionContent, "utf8");
22+
23+
console.log(`Generated VERSION file: ${versionFilePath}`);
24+
console.log(` version: ${version}`);
25+
console.log(` commit: ${commit}`);
26+

0 commit comments

Comments
 (0)