Skip to content

Commit

Permalink
feat: add option to specify the target file (#79)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Larwig <jan@larwig.com>
Co-authored-by: James George <jamesgeorge998001@gmail.com>
  • Loading branch information
3 people committed May 31, 2023
1 parent 40a5902 commit bfb65a4
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 451 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ You can find an example [here](https://github.com/jamesgeorge007/jamesgeorge007/

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

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

```yml
name: Update README
Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ inputs:
description: "The maximum number of lines populated in your readme file"
default: 5
required: false

TARGET_FILE:
description: "The file location to write changes to"
default: "README.md"
required: false
branding:
color: yellow
icon: activity
Expand Down
16 changes: 10 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1791,6 +1791,8 @@ const { Toolkit } = __webpack_require__(461);
const GH_USERNAME = core.getInput("GH_USERNAME");
const COMMIT_MSG = core.getInput("COMMIT_MSG");
const MAX_LINES = core.getInput("MAX_LINES");
const TARGET_FILE = core.getInput("TARGET_FILE") || "README.md";

/**
* Returns the sentence case representation
* @param {String} str - the string
Expand Down Expand Up @@ -1858,7 +1860,7 @@ const commitFile = async () => {
"41898282+github-actions[bot]@users.noreply.github.com",
]);
await exec("git", ["config", "--global", "user.name", "readme-bot"]);
await exec("git", ["add", "README.md"]);
await exec("git", ["add", TARGET_FILE]);
await exec("git", ["commit", "-m", COMMIT_MSG]);
await exec("git", ["push"]);
};
Expand Down Expand Up @@ -1911,7 +1913,9 @@ Toolkit.run(
// Call the serializer to construct a string
.map((item) => serializers[item.type](item));

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

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

// Update README
fs.writeFileSync("./README.md", readmeContent.join("\n"));
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));

// Commit to the remote repository
try {
Expand Down Expand Up @@ -1987,7 +1991,7 @@ Toolkit.run(
}
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`);
});
tools.log.success("Wrote to README");
tools.log.success(`Wrote to ${TARGET_FILE}`);
} else {
// It is likely that a newline is inserted after the <!--START_SECTION:activity--> comment (code formatter)
let count = 0;
Expand All @@ -2002,11 +2006,11 @@ Toolkit.run(
count++;
}
});
tools.log.success("Updated README with the recent activity");
tools.log.success(`Updated ${TARGET_FILE} with the recent activity`);
}

// Update README
fs.writeFileSync("./README.md", readmeContent.join("\n"));
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));

// Commit to the remote repository
try {
Expand Down
16 changes: 10 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { Toolkit } = require("actions-toolkit");
const GH_USERNAME = core.getInput("GH_USERNAME");
const COMMIT_MSG = core.getInput("COMMIT_MSG");
const MAX_LINES = core.getInput("MAX_LINES");
const TARGET_FILE = core.getInput("TARGET_FILE");

/**
* Returns the sentence case representation
* @param {String} str - the string
Expand Down Expand Up @@ -75,7 +77,7 @@ const commitFile = async () => {
"41898282+github-actions[bot]@users.noreply.github.com",
]);
await exec("git", ["config", "--global", "user.name", "readme-bot"]);
await exec("git", ["add", "README.md"]);
await exec("git", ["add", TARGET_FILE]);
await exec("git", ["commit", "-m", COMMIT_MSG]);
await exec("git", ["push"]);
};
Expand Down Expand Up @@ -128,7 +130,9 @@ Toolkit.run(
// Call the serializer to construct a string
.map((item) => serializers[item.type](item));

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

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

// Update README
fs.writeFileSync("./README.md", readmeContent.join("\n"));
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));

// Commit to the remote repository
try {
Expand Down Expand Up @@ -204,7 +208,7 @@ Toolkit.run(
}
readmeContent.splice(startIdx + idx, 0, `${idx + 1}. ${line}`);
});
tools.log.success("Wrote to README");
tools.log.success(`Wrote to ${TARGET_FILE}`);
} else {
// It is likely that a newline is inserted after the <!--START_SECTION:activity--> comment (code formatter)
let count = 0;
Expand All @@ -219,11 +223,11 @@ Toolkit.run(
count++;
}
});
tools.log.success("Updated README with the recent activity");
tools.log.success(`Updated ${TARGET_FILE} with the recent activity`);
}

// Update README
fs.writeFileSync("./README.md", readmeContent.join("\n"));
fs.writeFileSync(`./${TARGET_FILE}`, readmeContent.join("\n"));

// Commit to the remote repository
try {
Expand Down

0 comments on commit bfb65a4

Please sign in to comment.