Skip to content

Commit e36233c

Browse files
authored
Add example of actions with inputs (#3)
Add input example of the action. Use Prism.js to enable syntax highlight for YAML files
1 parent 3e68272 commit e36233c

File tree

17 files changed

+189
-48
lines changed

17 files changed

+189
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
major: v0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
major: v0
File renamed without changes.

data/actions/setup-chrome/version.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
major: v1
File renamed without changes.

data/actions/setup-edge/version.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
major: v1
File renamed without changes.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
major: v1

gatsby-node.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,43 @@ import type { ActionType } from "./src/types";
77
export const createPages: GatsbyNode["createPages"] = async ({
88
actions: { createPage },
99
}) => {
10-
const actionFiles = (await fs.promises.readdir("./data/actions")).filter(
11-
(file) => file.endsWith(".yaml") || file.endsWith(".yml"),
12-
);
10+
const actionDirs = await fs.promises.readdir("./data/actions");
11+
12+
for (const dir of actionDirs) {
13+
let actionYml: string;
14+
let versionYml: string;
15+
try {
16+
actionYml = await fs.promises.readFile(
17+
`./data/actions/${dir}/action.yml`,
18+
"utf-8",
19+
);
20+
versionYml = await fs.promises.readFile(
21+
`./data/actions/${dir}/version.yml`,
22+
"utf-8",
23+
);
24+
} catch (error) {
25+
console.warn(`Failed to read action.yaml in ${dir}`, String(error));
26+
continue;
27+
}
1328

14-
for (const file of actionFiles) {
15-
const slug = path.basename(file, path.extname(file));
16-
const content = await fs.promises.readFile(
17-
`./data/actions/${file}`,
18-
"utf-8",
19-
);
2029
const { name, description, inputs, outputs } = yaml.load(
21-
content,
30+
actionYml,
2231
) as ActionType;
32+
const { major } = yaml.load(versionYml);
2333
createPage({
24-
path: `/${slug}/`,
34+
path: `/${dir}/`,
2535
component: path.resolve("./src/templates/action.tsx"),
2636
context: {
27-
name: slug,
37+
name: dir,
2838
action: {
2939
name,
3040
description,
3141
inputs,
3242
outputs,
3343
},
44+
version: {
45+
major,
46+
},
3447
},
3548
});
3649
}

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"clean": "gatsby clean",
1414
"typecheck": "tsc --noEmit",
1515
"lint": "biome ci .",
16-
"lint:fix": "biome check --apply ."
16+
"lint:fix": "biome check --apply .",
17+
"fetch": "node ./scripts/fetch-actions.mjs"
1718
},
1819
"dependencies": {
1920
"babel-plugin-styled-components": "^2.1.4",
@@ -30,15 +31,18 @@
3031
"grommet": "^2.38.0",
3132
"grommet-icons": "^4.12.1",
3233
"js-yaml": "^4.1.0",
34+
"prismjs": "^1.29.0",
3335
"react": "^18.2.0",
3436
"react-dom": "^18.2.0",
3537
"styled-components": "^6.1.11"
3638
},
3739
"devDependencies": {
3840
"@biomejs/biome": "^1.7.3",
3941
"@types/node": "^20.11.19",
42+
"@types/prismjs": "^1.26.4",
4043
"@types/react": "^18.2.55",
4144
"@types/react-dom": "^18.2.19",
45+
"semver": "^7.6.2",
4246
"typescript": "^5.3.3"
4347
}
4448
}

pnpm-lock.yaml

+35-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/fetch-actions.mjs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import fs from "node:fs/promises";
2+
import semver from "semver";
3+
4+
const ACTION_NAMES = [
5+
"release-chrome-extension",
6+
"release-firefox-addon",
7+
"setup-chrome",
8+
"setup-edge",
9+
"setup-firefox",
10+
];
11+
const ACTIONS_ROOT = "./data/actions";
12+
13+
const { GITHUB_TOKEN } = process.env;
14+
15+
const getLatestTagNames = async (owner, repo) => {
16+
const url = `https://api.github.com/repos/${owner}/${repo}/releases/latest`;
17+
const headers = GITHUB_TOKEN
18+
? { Authorization: `Bearer ${GITHUB_TOKEN}` }
19+
: {};
20+
const res = await fetch(url, { headers });
21+
return (await res.json()).tag_name;
22+
};
23+
24+
const run = async () => {
25+
for (const name of ACTION_NAMES) {
26+
const actionDest = `${ACTIONS_ROOT}/${name}/action.yml`;
27+
const url = `https://raw.githubusercontent.com/browser-actions/${name}/latest/action.yml`;
28+
const content = await fetch(url).then((res) => res.text());
29+
await fs.mkdir(`${ACTIONS_ROOT}/${name}`, { recursive: true });
30+
await fs.writeFile(actionDest, content);
31+
32+
console.log(`Fetched ${name} action.yml to ${actionDest}`);
33+
}
34+
35+
for (const name of ACTION_NAMES) {
36+
const versionDest = `${ACTIONS_ROOT}/${name}/version.yml`;
37+
const latestTag = await getLatestTagNames("browser-actions", name);
38+
if (!latestTag) {
39+
console.error(`Failed to get latest tag for ${name}`);
40+
continue;
41+
}
42+
const versionContent = `major: v${semver.major(latestTag)}
43+
`;
44+
await fs.writeFile(versionDest, versionContent);
45+
46+
console.log(`Fetched ${name} version to ${versionDest}`);
47+
}
48+
};
49+
50+
await run();

src/components/code.tsx

+25-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
import { Box } from "grommet";
2-
import type * as React from "react";
2+
import Prism from "prismjs";
3+
import * as React from "react";
4+
import "prismjs/themes/prism.css";
5+
import "prismjs/components/prism-yaml";
36

47
interface Props {
58
children: React.ReactNode;
69
}
710

8-
export const CodeBlock: React.FC<Props> = ({ children }) => (
9-
<Box
10-
background="light-2"
11-
pad={{ horizontal: "medium", vertical: "small" }}
12-
round="small"
13-
>
14-
<pre>
15-
<code>{children}</code>
16-
</pre>
17-
</Box>
18-
);
11+
export const CodeBlock: React.FC<Props> = ({ children }) => {
12+
const ref = React.useRef<HTMLElement>(null);
13+
14+
React.useEffect(() => {
15+
if (!ref.current) {
16+
return;
17+
}
18+
19+
Prism.highlightElement(ref.current, false);
20+
}, []);
21+
22+
return (
23+
<Box background="light-2" round="small">
24+
<pre style={{ background: "none" }}>
25+
<code ref={ref} className="language-yml">
26+
{children}
27+
</code>
28+
</pre>
29+
</Box>
30+
);
31+
};

0 commit comments

Comments
 (0)