Skip to content

Commit

Permalink
feat: add build commit message and timestamp to footer
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjinp committed Jan 12, 2024
1 parent 0a16991 commit 0437db1
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
24 changes: 22 additions & 2 deletions apps/astro/src/components/Footer.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { SITE } from "@config";
import Hr from "./Hr.astro";
import Socials from "./Socials.astro";
import { commitInfo } from "@utils/commits";
import LinkButton from "./LinkButton.astro";
const currentYear = new Date().getFullYear();
Expand All @@ -10,16 +12,34 @@ export interface Props {
}
const { noMarginTop = false } = Astro.props;
const buildTime = new Date().toLocaleDateString();
---

<footer class={`${noMarginTop ? "" : "mt-auto"}`}>
<Hr noPadding />
<div class="footer-wrapper">
<Socials centered />
<div class="copyright-wrapper">
<span>Copyright &#169; {SITE.author} {currentYear}</span>
<div class="flex flex-col">
<div>
<small>
version{" "}
<LinkButton
title="commit hash"
href={`https://github.com/kenjinp/kenny.wtf/commit/${commitInfo.hash}`}
>
#{commitInfo.shortHash}
</LinkButton><strong>&nbsp|&nbsp</strong>
<span title="build date">
last updated: {buildTime}
</span>
</small>
</div>
<div>
<span>Copyright &#169; {SITE.author} {currentYear}</span>
</div>
</div>
</div>

</footer>

<style>
Expand Down
84 changes: 84 additions & 0 deletions apps/astro/src/utils/commits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// from https://github.com/seymen/git-last-commit
import { execSync } from "child_process";
import path from "path";
import { fileURLToPath } from "url";

const __filenameNew = fileURLToPath(import.meta.url);

const __dirnameNew = path.dirname(__filenameNew);

const splitCharacter = "<##>";

const executeCommand = (command: any, options: any) => {
let dst = __dirnameNew;

if (!!options && options.dst) {
dst = options.dst;
}

const stdout = execSync(command, { cwd: dst }).toString();
if (stdout === "") {
return;
}

return stdout;
};

const prettyFormat = [
"%h",
"%H",
"%s",
"%f",
"%b",
"%at",
"%ct",
"%an",
"%ae",
"%cn",
"%ce",
"%N",
"",
];

const getCommandString = (splitCharacter: string) =>
'git log -1 --pretty=format:"' +
prettyFormat.join(splitCharacter) +
'"' +
" && git rev-parse --abbrev-ref HEAD" +
" && git tag --contains HEAD";

export const getLastCommit = (options: any) => {
const command = getCommandString(splitCharacter);

const res = executeCommand(command, options) as string;

const a = res.split(splitCharacter);

// e.g. master\n or master\nv1.1\n or master\nv1.1\nv1.2\n
const branchAndTags = a[a.length - 1].split("\n").filter(n => n);
const branch = branchAndTags[0];
const tags = branchAndTags.slice(1);

return {
shortHash: a[0],
hash: a[1],
subject: a[2],
sanitizedSubject: a[3],
body: a[4],
authoredOn: a[5],
committedOn: a[6],
author: {
name: a[7],
email: a[8],
},
committer: {
name: a[9],
email: a[10],
},
notes: a[11],
branch,
tags,
};
};

export const commitInfo = getLastCommit({});

0 comments on commit 0437db1

Please sign in to comment.