Skip to content

Commit

Permalink
feat(ts): update the example
Browse files Browse the repository at this point in the history
  • Loading branch information
pplmx committed Aug 28, 2024
1 parent 20381e7 commit c614959
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 113 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Visit our [website](https://x-pt.github.io) for more details.

### Prerequisites

- Python 3.6+
- Python 3.8+
- [CookieCutter](https://cookiecutter.readthedocs.io/en/latest/installation.html)

### Usage
Expand All @@ -35,10 +35,10 @@ cookiecutter gh:x-pt/template

Check out an example project generated with this template:

- [Example C++ with CMake](https://github.com/x-pt/example-cxx-cmake)
- [Example C++ with XMake](https://github.com/x-pt/example-cxx-xmake)
- [Example Golang](https://github.com/x-pt/example-go)
- [Example Python](https://github.com/x-pt/example-py)
- [Example for C++ with CMake](https://github.com/x-pt/example-cxx-cmake)
- [Example for C++ with XMake](https://github.com/x-pt/example-cxx-xmake)
- [Example for Golang](https://github.com/x-pt/example-go)
- [Example for Python](https://github.com/x-pt/example-py)

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion template/ts/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"github_username": "your-org-or-username",


"node_version": "18",
"node_version": "20",


"__gh_slug": "{{ cookiecutter.github_username }}/{{ cookiecutter.project_slug }}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ on:
{% raw %}
jobs:
build-and-test:
name: Node.js-${{ matrix.node }}
name: Node.js-${{ matrix.node }} on ${{ matrix.os }}

strategy:
matrix:
node: [ 18, 20, 22 ]
node: [ 20, 22 ]
os:
- ubuntu-latest
- windows-latest
Expand All @@ -31,6 +31,7 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: "npm"

- name: Init dependencies
run: make init
Expand Down
3 changes: 1 addition & 2 deletions template/ts/{{cookiecutter.project_slug}}/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"enabled": true,
"rules": {
"recommended": true
},
"ignore": ["**/*.test.ts"]
}
},
"organizeImports": {
"enabled": true
Expand Down
6 changes: 4 additions & 2 deletions template/ts/{{cookiecutter.project_slug}}/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
},
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^6.0.0"
"@actions/github": "^6.0.0",
"axios": "^1.7.5",
"toml": "^3.0.0"
},
"devDependencies": {
"@vercel/ncc": "^0.38.1",
"@biomejs/biome": "1.8.3",
"@types/jest": "^29.5.12",
"@vercel/ncc": "^0.38.1",
"husky": "^9.1.5",
"jest": "^29.7.0",
"lint-staged": "^15.2.9",
Expand Down
95 changes: 80 additions & 15 deletions template/ts/{{cookiecutter.project_slug}}/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,90 @@
import { getInput, setFailed } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import * as fs from "node:fs/promises";
import * as core from "@actions/core";
import axios from "axios";
import * as toml from "toml";

interface Config {
input_text?: string;
find_word?: string;
replace_word?: string;
number_list?: number[];
input_file?: string;
output_file?: string;
append_text?: string;
api_url?: string;
}

// Function to check API reachability
async function checkAPIReachability(apiUrl: string): Promise<void> {
try {
const response = await axios.get(apiUrl, { timeout: 10000 });
if (response.status < 200 || response.status >= 300) {
core.warning(`API is not reachable, status code: ${response.status}`);
}
} catch (error) {
core.warning(`Failed to make API request: ${error instanceof Error ? error.message : String(error)}`);
}
}

// Function to read and append text to a file
async function readAndAppendToFile(inputFile: string, outputFile: string, appendText: string): Promise<void> {
try {
const content = await fs.readFile(inputFile, "utf-8");
const modifiedContent = `${content}\n${appendText}`;
await fs.writeFile(outputFile, modifiedContent, { encoding: "utf-8" });
} catch (error) {
throw new Error(`File operation failed: ${error instanceof Error ? error.message : String(error)}`);
}
}

export async function run(): Promise<void> {
try {
const token = getInput("gh-token", { required: true });
const label = getInput("label", { required: true });
const configPath = core.getInput("config_path") || ".github/configs/setup-custom-action-by-ts.toml";

const configContent = await fs.readFile(configPath, "utf-8");
const config: Config = toml.parse(configContent);

const {
input_text = "",
find_word = "",
replace_word = "",
number_list = [],
input_file = "",
output_file = "",
append_text = "",
api_url = "",
} = config;

const pullRequest = context.payload.pull_request;
if (!pullRequest) {
setFailed("This action can only be run on Pull Requests");
return;
if (api_url) {
try {
await checkAPIReachability(api_url);
core.info(`API ${api_url} is reachable.`);
} catch (error) {
core.warning(error instanceof Error ? error.message : String(error));
}
}

const octokit = getOctokit(token);
await octokit.rest.issues.addLabels({
...context.repo,
issue_number: pullRequest.number,
labels: [label],
});
if (input_file && output_file && append_text) {
try {
await readAndAppendToFile(input_file, output_file, append_text);
core.info(`Appended text to file: ${output_file}`);
} catch (error) {
core.warning(error instanceof Error ? error.message : String(error));
}
}

const processedText = input_text.replace(new RegExp(find_word, "g"), replace_word);
const wordCount = processedText.trim() === "" ? 0 : processedText.trim().split(/\s+/).length;

const sum = number_list.reduce((acc: number, num: number) => acc + num, 0);
const average = number_list.length > 0 ? sum / number_list.length : 0;

core.setOutput("processed_text", processedText);
core.setOutput("word_count", wordCount);
core.setOutput("sum", sum);
core.setOutput("average", average);
} catch (error) {
setFailed(error instanceof Error ? error.message : "An unexpected error occurred");
core.setFailed(`Action failed with error: ${error instanceof Error ? error.message : String(error)}`);
}
}

Expand Down
Loading

0 comments on commit c614959

Please sign in to comment.