Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/cached-deps/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ runs:
- name: Cache dependencies
if: inputs.caching == 'true'
id: cache
uses: actions/cache@v4
uses: actions/cache@v6
with:
path: node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

steps:
- name: 🗜️ Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7

- name: 🗜️ Setup node version
uses: actions/setup-node@v6
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

steps:
- name: 🗜️ Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7

- name: 🗜️ Setup node version
uses: actions/setup-node@v6
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
package-lock.json
yarn.lock

# Visual Studio Code
.vscode/*
Expand All @@ -34,4 +33,4 @@ Thumbs.db
version-info.ts
dist
tmp
src/package.json
src/package.json
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2025 Celtian
Copyright (c) 2025 - 2026 Celtian

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ npx package-version-info
# Show version
npx package-version-info --version

# Custom paths
npx package-version-info --input package.json --output src/version-info.ts
# Custom paths (.git is the default Git directory)
npx package-version-info --input package.json --output src/version-info.ts --git ../.git

# Short options
npx package-version-info -i package.json -o src/version-info.ts -g ../.git
```

### Output Example
Expand Down Expand Up @@ -142,7 +145,7 @@ zig build
zig build run

# Run with arguments
zig build run -- --input package.json --output version-info.ts
zig build run -- --input package.json --output version-info.ts --git .git

# Run tests
zig build test
Expand All @@ -168,6 +171,6 @@ Contributions are welcome! Please feel free to submit a Pull Request.

## 🪪 License

Copyright © 2025 [Dominik Hladik](https://github.com/Celtian)
Copyright © 2025 - 2026 [Dominik Hladik](https://github.com/Celtian)

All contents are licensed under the [MIT license](LICENSE).
19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
"fix-vulnerabilities": "npx yarn-audit-fix && npx browserslist --update-db"
},
"devDependencies": {
"@commitlint/cli": "^20.2.0",
"@commitlint/config-conventional": "^20.2.0",
"@commitlint/cli": "^21.2.1",
"@commitlint/config-conventional": "^21.2.0",
"@types/fs-extra": "^11.0.4",
"auto-changelog": "^2.5.0",
"auto-changelog": "^2.6.0",
"copyfiles": "^2.4.1",
"fs-extra": "^11.3.3",
"fs-extra": "^11.3.6",
"husky": "^9.1.7",
"rimraf": "^6.1.2",
"tsx": "^4.21.0"
"rimraf": "^6.1.3",
"tsx": "^4.23.0"
},
"bin": {
"package-version-info": "zig-out/bin/version_info"
Expand All @@ -56,6 +56,13 @@
"bugs": {
"url": "https://github.com/Celtian/package-version-info/issues"
},
"auto-changelog": {
"remote": "none",
"commitUrl": "https://github.com/Celtian/package-version-info/commit/{id}",
"issueUrl": "https://github.com/Celtian/package-version-info/issues/{id}",
"mergeUrl": "https://github.com/Celtian/package-version-info/pull/{id}",
"compareUrl": "https://github.com/Celtian/package-version-info/compare/{from}...{to}"
},
"keywords": [
"version",
"info",
Expand Down
7 changes: 6 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub fn main() !void {

var input_path: []const u8 = "package.json";
var output_path: []const u8 = "version-info.ts";
var git_path: []const u8 = ".git";

// Parse arguments
while (args.next()) |arg| {
Expand All @@ -29,8 +30,12 @@ pub fn main() !void {
if (args.next()) |value| {
output_path = value;
}
} else if (std.mem.eql(u8, arg, "--git") or std.mem.eql(u8, arg, "-g")) {
if (args.next()) |value| {
git_path = value;
}
}
}

try version_info.generateVersionInfo(allocator, input_path, output_path);
try version_info.generateVersionInfo(allocator, input_path, output_path, git_path);
}
23 changes: 13 additions & 10 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ pub const GitInfo = struct {
commit: []const u8,
};

/// Reads the current git branch and commit hash
/// Returns null if not in a git repository
pub fn getGitInfo(allocator: std.mem.Allocator) !?GitInfo {
// Read .git/HEAD to get the branch reference
const head_file = std.fs.cwd().openFile(".git/HEAD", .{}) catch |err| {
/// Reads the current git branch and commit hash from a Git directory.
/// Returns null if the directory is not a Git repository.
pub fn getGitInfo(allocator: std.mem.Allocator, git_path: []const u8) !?GitInfo {
const head_path = try std.fs.path.join(allocator, &.{ git_path, "HEAD" });
defer allocator.free(head_path);

const head_file = std.fs.cwd().openFile(head_path, .{}) catch |err| {
if (err == error.FileNotFound) {
return null; // Not a git repository
}
Expand All @@ -85,8 +87,7 @@ pub fn getGitInfo(allocator: std.mem.Allocator) !?GitInfo {
branch = ref_path_raw[11..]; // Skip "refs/heads/"
}

// Build the full path to the ref file (.git/refs/heads/branch-name)
const ref_path = try std.fmt.allocPrint(allocator, ".git/{s}", .{ref_path_raw});
const ref_path = try std.fs.path.join(allocator, &.{ git_path, ref_path_raw });
defer allocator.free(ref_path);

// Try to read the commit hash from the ref file
Expand All @@ -101,7 +102,9 @@ pub fn getGitInfo(allocator: std.mem.Allocator) !?GitInfo {
} else |err| {
if (err == error.FileNotFound) {
// Ref file not found, try packed-refs
const packed_refs_file = std.fs.cwd().openFile(".git/packed-refs", .{}) catch |packed_err| {
const packed_refs_path = try std.fs.path.join(allocator, &.{ git_path, "packed-refs" });
defer allocator.free(packed_refs_path);
const packed_refs_file = std.fs.cwd().openFile(packed_refs_path, .{}) catch |packed_err| {
if (packed_err == error.FileNotFound) {
return null; // Neither individual ref nor packed-refs found
}
Expand Down Expand Up @@ -216,7 +219,7 @@ fn formatTimestampISO8601(buffer: []u8, millis: i64) ![]const u8 {
}

/// Generates a version-info.ts file from package.json
pub fn generateVersionInfo(allocator: std.mem.Allocator, package_json_path: []const u8, output_path: []const u8) !void {
pub fn generateVersionInfo(allocator: std.mem.Allocator, package_json_path: []const u8, output_path: []const u8, git_path: []const u8) !void {
const start_time = std.time.milliTimestamp();
logNewLine();
log(Color.YELLOW, "🚀", "Starting version info generation...", .{});
Expand Down Expand Up @@ -275,7 +278,7 @@ pub fn generateVersionInfo(allocator: std.mem.Allocator, package_json_path: []co

logNewLine();
log(Color.BRIGHT_MAGENTA, "🌿", "Reading git info...", .{});
const git_info = try getGitInfo(allocator);
const git_info = try getGitInfo(allocator, git_path);

if (git_info) |info| {
log(Color.BRIGHT_MAGENTA, "📍", "Branch: {s}", .{info.branch});
Expand Down
Loading
Loading