Skip to content

Commit 647c3f2

Browse files
committed
feat: print relative date of when script was built
Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent f220be0 commit 647c3f2

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

git-stacked-rebase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ git-stacked-rebase [...] -V|--version [...]
10031003
git-stacked-rebase [...] -h|--help [...]
10041004
10051005
1006-
git-stacked-rebase ${gitStackedRebaseVersionStr}
1006+
git-stacked-rebase ${gitStackedRebaseVersionStr} __BUILD_DATE_REPLACEMENT_STR__
10071007
`.replace(/\t/g, " ".repeat(4));
10081008

10091009
if (process.argv.some((arg) => ["-h", "--help"].includes(arg))) {

script/postbuild.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ const executablePath = "./dist/git-stacked-rebase.js";
66
fs.chmodSync(executablePath, "755");
77
execSyncP(`ls -la ${executablePath}`);
88

9-
updateShebang(executablePath);
10-
execSyncP(`cat ${executablePath} | head -n 2`);
9+
modifyLines(executablePath);
1110

12-
function updateShebang(path) {
11+
function modifyLines(path) {
1312
const file = fs.readFileSync(path, { encoding: "utf-8" });
1413
const lines = file.split("\n");
1514

15+
const afterUpdateShebang = updateShebang(lines);
16+
const afterInjectRelativeBuildDatePrinter = injectRelativeBuildDatePrinter(lines);
17+
18+
const newFile = lines.join("\n");
19+
fs.writeFileSync(path, newFile);
20+
21+
afterUpdateShebang();
22+
afterInjectRelativeBuildDatePrinter();
23+
}
24+
25+
function updateShebang(lines) {
1626
const oldShebang = "#!/usr/bin/env ts-node-dev";
1727
const newShebang = "#!/usr/bin/env node";
1828

@@ -22,8 +32,23 @@ function updateShebang(path) {
2232
lines.splice(0, 0, newShebang);
2333
}
2434

25-
const newFile = lines.join("\n");
26-
fs.writeFileSync(path, newFile);
35+
return () => execSyncP(`cat ${executablePath} | head -n 2`);
36+
}
2737

28-
return;
38+
function injectRelativeBuildDatePrinter(lines) {
39+
const BUILD_DATE_REPLACEMENT_STR = "__BUILD_DATE_REPLACEMENT_STR__";
40+
const targetLineIdx = lines.findIndex((line) => line.includes(BUILD_DATE_REPLACEMENT_STR));
41+
const buildDate = new Date().getTime();
42+
const printRelativeDate =
43+
"(" + //
44+
"built " +
45+
"${" +
46+
`Math.round((new Date() - ${buildDate}) / 1000 / 60)` +
47+
"}" +
48+
" mins ago" +
49+
")";
50+
51+
lines[targetLineIdx] = lines[targetLineIdx].replace(BUILD_DATE_REPLACEMENT_STR, printRelativeDate);
52+
53+
return () => execSyncP(`cat ${executablePath} | grep " mins ago"`);
2954
}

0 commit comments

Comments
 (0)