Skip to content

Commit 05dc6b5

Browse files
committed
feat: support --format=json options
1 parent c7534d3 commit 05dc6b5

7 files changed

Lines changed: 779 additions & 975 deletions

File tree

.mocharc.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": [
3+
"ts-node-test-register"
4+
]
5+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Install with [npm](https://www.npmjs.com/):
1616
$ github-actions-badge
1717

1818
Options
19-
--format "markdown"
19+
--format "markdown", "json"
2020

2121
Examples
2222
# Copy GitHub Action as Markdown format

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@
5555
},
5656
"devDependencies": {
5757
"@types/glob": "^7.1.1",
58-
"@types/hosted-git-info": "^2.7.0",
58+
"@types/hosted-git-info": "^3.0.0",
5959
"@types/js-yaml": "^3.12.1",
6060
"@types/meow": "^5.0.0",
61-
"@types/mocha": "^5.2.7",
62-
"@types/node": "^12.12.12",
63-
"cross-env": "^6.0.3",
64-
"husky": "^3.1.0",
65-
"lint-staged": "^9.4.3",
66-
"mocha": "^6.2.2",
67-
"prettier": "^1.19.1",
61+
"@types/mocha": "^7.0.2",
62+
"@types/node": "^14.0.13",
63+
"cross-env": "^7.0.2",
64+
"husky": "^4.2.5",
65+
"lint-staged": "^10.2.11",
66+
"mocha": "^8.0.1",
67+
"prettier": "^2.0.5",
6868
"rimraf": "^3.0.0",
6969
"ts-node": "^8.5.2",
7070
"ts-node-test-register": "^8.0.1",
@@ -76,6 +76,6 @@
7676
"hosted-git-info": "^3.0.2",
7777
"js-yaml": "^3.13.1",
7878
"markdown-escape": "^1.1.0",
79-
"meow": "^5.0.0"
79+
"meow": "^7.0.1"
8080
}
8181
}

src/cli.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const meow = require("meow");
1+
import meow from "meow";
22
import { fetchRepositoryInfo, generate } from "./github-actions-badge";
33

44
const cli = meow(`
55
Usage
66
$ github-actions-badge
77
88
Options
9-
--format "markdown"
9+
--format "markdown", "json"
1010
1111
Examples
1212
# Copy GitHub Action as Markdown format
@@ -24,6 +24,9 @@ const cli = meow(`
2424
export const run = async () => {
2525
const { repo, owner } = await fetchRepositoryInfo();
2626
const format = cli.flags.format || "markdown";
27+
if (format !== "json" && format !== "markdown") {
28+
throw new Error(`${format} is unknown format`);
29+
}
2730
const cwd = process.cwd();
2831
return generate({ cwd, repo, owner, format });
2932
};

src/github-actions-badge.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface generateOptions {
1111
owner: string;
1212
repo: string;
1313
cwd: string;
14-
format: "markdown"
14+
format: "markdown" | "json"
1515
}
1616

1717
export const fetchRepositoryInfo = (): Promise<{ owner: string; repo: string }> => {
@@ -39,15 +39,31 @@ export const generate = (options: generateOptions): string => {
3939
const ymlList = glob.sync(path.join(options.cwd, ".github/workflows/*"), {
4040
dot: true
4141
});
42-
return ymlList.map(filePath => {
42+
const items = ymlList.map(filePath => {
4343
const content = yaml.safeLoad(fs.readFileSync(filePath, "utf-8"));
4444
if (!content.name) {
4545
throw new Error(`${filePath} does not define name`);
4646
}
4747
return content.name;
4848
}).map(workflowName => {
49-
// --format "markdown"
50-
// https://github.com/<OWNER>/<REPOSITORY>/workflows/<WORKFLOW_NAME>/badge.svg
51-
return `[![Actions Status: ${encodeMarkdownTitle(workflowName)}](https://github.com/${options.owner}/${options.repo}/workflows/${encodeURIComponent(workflowName)}/badge.svg)](https://github.com/${options.owner}/${options.repo}/actions?query=workflow%3A"${encodeWorkflowNameQuery(workflowName)}")`;
52-
}).join("\n");
49+
// --format "json"
50+
if (options.format === "json") {
51+
return {
52+
name: workflowName,
53+
actionBadgeUrl: `https://github.com/${options.owner}/${options.repo}/workflows/${encodeURIComponent(workflowName)}/badge.svg`,
54+
workflowUrl: `https://github.com/${options.owner}/${options.repo}/actions?query=workflow%3A"${encodeWorkflowNameQuery(workflowName)}`
55+
};
56+
} else if (options.format === "markdown") {
57+
// --format "markdown"
58+
// https://github.com/<OWNER>/<REPOSITORY>/workflows/<WORKFLOW_NAME>/badge.svg
59+
return `[![Actions Status: ${encodeMarkdownTitle(workflowName)}](https://github.com/${options.owner}/${options.repo}/workflows/${encodeURIComponent(workflowName)}/badge.svg)](https://github.com/${options.owner}/${options.repo}/actions?query=workflow%3A"${encodeWorkflowNameQuery(workflowName)}")`;
60+
}
61+
return;
62+
});
63+
if (options.format === "json") {
64+
return JSON.stringify(items);
65+
} else if (options.format === "markdown") {
66+
return items.join("\n");
67+
}
68+
return "";
5369
};

test/mocha.opts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)