Skip to content

Commit bfb65a4

Browse files
Lissy93tuunitjamesgeorge007
committed
feat: add option to specify the target file (#79)
Co-authored-by: Jan Larwig <jan@larwig.com> Co-authored-by: James George <jamesgeorge998001@gmail.com>
1 parent 40a5902 commit bfb65a4

File tree

5 files changed

+165
-451
lines changed

5 files changed

+165
-451
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ You can find an example [here](https://github.com/jamesgeorge007/jamesgeorge007/
5151

5252
Use the following `input params` to customize it for your use case:-
5353

54-
| Input Param | Default Value | Description |
55-
| ------------ | -------------------------------------------- | --------------------------------------------------------- |
56-
| `COMMIT_MSG` | :zap: Update README with the recent activity | Commit message used while committing to the repo |
57-
| `MAX_LINES` | 5 | The maximum number of lines populated in your readme file |
54+
| Input Param | Default Value | Description |
55+
| ------------- | -------------------------------------------- | --------------------------------------------------------- |
56+
| `COMMIT_MSG` | :zap: Update README with the recent activity | Commit message used while committing to the repo |
57+
| `MAX_LINES` | 5 | The maximum number of lines populated in your readme file |
58+
| `TARGET_FILE` | README.md | The file to insert recent activity into |
5859

5960
```yml
6061
name: Update README

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ inputs:
1515
description: "The maximum number of lines populated in your readme file"
1616
default: 5
1717
required: false
18-
18+
TARGET_FILE:
19+
description: "The file location to write changes to"
20+
default: "README.md"
21+
required: false
1922
branding:
2023
color: yellow
2124
icon: activity

dist/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,8 @@ const { Toolkit } = __webpack_require__(461);
17911791
const GH_USERNAME = core.getInput("GH_USERNAME");
17921792
const COMMIT_MSG = core.getInput("COMMIT_MSG");
17931793
const MAX_LINES = core.getInput("MAX_LINES");
1794+
const TARGET_FILE = core.getInput("TARGET_FILE") || "README.md";
1795+
17941796
/**
17951797
* Returns the sentence case representation
17961798
* @param {String} str - the string
@@ -1858,7 +1860,7 @@ const commitFile = async () => {
18581860
"41898282+github-actions[bot]@users.noreply.github.com",
18591861
]);
18601862
await exec("git", ["config", "--global", "user.name", "readme-bot"]);
1861-
await exec("git", ["add", "README.md"]);
1863+
await exec("git", ["add", TARGET_FILE]);
18621864
await exec("git", ["commit", "-m", COMMIT_MSG]);
18631865
await exec("git", ["push"]);
18641866
};
@@ -1911,7 +1913,9 @@ Toolkit.run(
19111913
// Call the serializer to construct a string
19121914
.map((item) => serializers[item.type](item));
19131915

1914-
const readmeContent = fs.readFileSync("./README.md", "utf-8").split("\n");
1916+
const readmeContent = fs
1917+
.readFileSync(`./${TARGET_FILE}`, "utf-8")
1918+
.split("\n");
19151919

19161920
// Find the index corresponding to <!--START_SECTION:activity--> comment
19171921
let startIdx = readmeContent.findIndex(
@@ -1955,7 +1959,7 @@ Toolkit.run(
19551959
);
19561960

19571961
// Update README
1958-
fs.writeFileSync("./README.md", readmeContent.join("\n"));
1962+
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));
19591963

19601964
// Commit to the remote repository
19611965
try {
@@ -1987,7 +1991,7 @@ Toolkit.run(
19871991
}
19881992
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`);
19891993
});
1990-
tools.log.success("Wrote to README");
1994+
tools.log.success(`Wrote to ${TARGET_FILE}`);
19911995
} else {
19921996
// It is likely that a newline is inserted after the <!--START_SECTION:activity--> comment (code formatter)
19931997
let count = 0;
@@ -2002,11 +2006,11 @@ Toolkit.run(
20022006
count++;
20032007
}
20042008
});
2005-
tools.log.success("Updated README with the recent activity");
2009+
tools.log.success(`Updated ${TARGET_FILE} with the recent activity`);
20062010
}
20072011

20082012
// Update README
2009-
fs.writeFileSync("./README.md", readmeContent.join("\n"));
2013+
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));
20102014

20112015
// Commit to the remote repository
20122016
try {

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const { Toolkit } = require("actions-toolkit");
88
const GH_USERNAME = core.getInput("GH_USERNAME");
99
const COMMIT_MSG = core.getInput("COMMIT_MSG");
1010
const MAX_LINES = core.getInput("MAX_LINES");
11+
const TARGET_FILE = core.getInput("TARGET_FILE");
12+
1113
/**
1214
* Returns the sentence case representation
1315
* @param {String} str - the string
@@ -75,7 +77,7 @@ const commitFile = async () => {
7577
"41898282+github-actions[bot]@users.noreply.github.com",
7678
]);
7779
await exec("git", ["config", "--global", "user.name", "readme-bot"]);
78-
await exec("git", ["add", "README.md"]);
80+
await exec("git", ["add", TARGET_FILE]);
7981
await exec("git", ["commit", "-m", COMMIT_MSG]);
8082
await exec("git", ["push"]);
8183
};
@@ -128,7 +130,9 @@ Toolkit.run(
128130
// Call the serializer to construct a string
129131
.map((item) => serializers[item.type](item));
130132

131-
const readmeContent = fs.readFileSync("./README.md", "utf-8").split("\n");
133+
const readmeContent = fs
134+
.readFileSync(`./${TARGET_FILE}`, "utf-8")
135+
.split("\n");
132136

133137
// Find the index corresponding to <!--START_SECTION:activity--> comment
134138
let startIdx = readmeContent.findIndex(
@@ -172,7 +176,7 @@ Toolkit.run(
172176
);
173177

174178
// Update README
175-
fs.writeFileSync("./README.md", readmeContent.join("\n"));
179+
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));
176180

177181
// Commit to the remote repository
178182
try {
@@ -204,7 +208,7 @@ Toolkit.run(
204208
}
205209
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`);
206210
});
207-
tools.log.success("Wrote to README");
211+
tools.log.success(`Wrote to ${TARGET_FILE}`);
208212
} else {
209213
// It is likely that a newline is inserted after the <!--START_SECTION:activity--> comment (code formatter)
210214
let count = 0;
@@ -219,11 +223,11 @@ Toolkit.run(
219223
count++;
220224
}
221225
});
222-
tools.log.success("Updated README with the recent activity");
226+
tools.log.success(`Updated ${TARGET_FILE} with the recent activity`);
223227
}
224228

225229
// Update README
226-
fs.writeFileSync("./README.md", readmeContent.join("\n"));
230+
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));
227231

228232
// Commit to the remote repository
229233
try {

0 commit comments

Comments
 (0)