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
6 changes: 6 additions & 0 deletions .github/actions/cached-deps/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ runs:
yarn install --frozen-lockfile
echo "cache='${{ inputs.caching }}'" >> $GITHUB_OUTPUT
shell: bash

- name: Postinstall
if: steps.cache.outputs.cache-hit == 'true' && inputs.caching == 'true'
id: postinstall
run: yarn postinstall
shell: bash
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ Thumbs.db
# Temp files
version-info.ts
dist
tmp
tmp
src/package.json
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"clean": "rimraf zig-out dist",
"prepare": "husky",
"copy-files": "copyfiles -u 2 zig-out/bin/version_info dist/bin && copyfiles README.md LICENSE dist",
"postinstall": "yarn script:package-zig",
"script:package-zig": "yarn tsx ./scripts/package-zig.ts",
"script:package-npm": "yarn tsx ./scripts/package-npm.ts",
"script:package-github": "yarn tsx ./scripts/package-github.ts",
"version": "auto-changelog -p && git add CHANGELOG.md",
Expand Down
21 changes: 21 additions & 0 deletions scripts/package-zig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { writeFileSync, ensureDirSync } from 'fs-extra';
import { join, resolve } from 'path';

const pkg = require(resolve(__dirname, '..', 'package.json'));

pkg.scripts = undefined;
pkg.devDependencies = undefined;
pkg.packageManager = undefined;
pkg.engines = undefined;
pkg.files = undefined;
pkg.bin = {
"package-version-info": "bin/version_info"
};

const srcDir = join(__dirname, '..', 'src');
ensureDirSync(srcDir);
writeFileSync(join(srcDir, 'package.json'), JSON.stringify(pkg, null, 2));

console.log('\x1b[34m', 'Package.json in src/ modified.', '\x1b[0m');


2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn main() !void {
// Parse arguments
while (args.next()) |arg| {
if (std.mem.eql(u8, arg, "--version") or std.mem.eql(u8, arg, "-v")) {
try version_info.printVersion(allocator, "package.json");
try version_info.printToolVersion(allocator);
return;
} else if (std.mem.eql(u8, arg, "--input") or std.mem.eql(u8, arg, "-i")) {
if (args.next()) |value| {
Expand Down
19 changes: 9 additions & 10 deletions src/root.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! By convention, root.zig is the root source file when making a library.
const std = @import("std");

const embedded_package_json = @embedFile("package.json");

const Color = struct {
pub const RESET = "\x1b[0m";
pub const BOLD = "\x1b[1m";
Expand All @@ -27,20 +29,17 @@ fn logNewLine() void {
std.debug.print("\n", .{});
}

/// Prints version from package.json in a nicely formatted way
pub fn printVersion(allocator: std.mem.Allocator, package_json_path: []const u8) !void {
const file = try std.fs.cwd().openFile(package_json_path, .{});
defer file.close();

const file_content = try file.readToEndAlloc(allocator, 1024 * 1024);
defer allocator.free(file_content);

const parsed = try std.json.parseFromSlice(std.json.Value, allocator, file_content, .{});
/// Prints the tool's version from embedded package.json
pub fn printToolVersion(allocator: std.mem.Allocator) !void {
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, embedded_package_json, .{});
defer parsed.deinit();

const version = parsed.value.object.get("version").?.string;
const name = parsed.value.object.get("name").?.string;

logNewLine();
log(Color.BRIGHT_CYAN, "📦", "Version: {s}", .{version});
log(Color.BRIGHT_CYAN, "📦", "{s}", .{name});
log(Color.BRIGHT_CYAN, "📌", "Version: {s}", .{version});
logNewLine();
}

Expand Down