Skip to content

Commit 07a2acf

Browse files
committed
chore: refactor (#21)
1 parent 7653019 commit 07a2acf

File tree

12 files changed

+8521
-155
lines changed

12 files changed

+8521
-155
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,29 @@ jobs:
2323
node-version: ${{ matrix.node-version }}
2424

2525
- name: Install dependencies
26-
run: npm install
26+
run: yarn install
2727

2828
- name: Check format and lint
29-
run: npm run format-check && npm run lint
29+
run: yarn run format-check && yarn run lint
3030

3131
- name: Run tests
32-
run: npm test
32+
run: yarn test
3333

3434
- name: Build and package
35-
run: npm run build
35+
run: yarn run build
3636

37+
38+
- name: Get the version
39+
if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.node-version == 14 }}
40+
id: get_version
41+
run: echo ::set-output name=version::${GITHUB_REF/refs\/tags\//}
42+
3743
- name: Publish
3844
if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.node-version == 14 }}
3945
env:
4046
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
47+
VERSION: ${{ steps.get_version.outputs.version }}
4148
run: |
4249
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
43-
npm publish
50+
yarn version --new-version ${VERSION} --no-git-tag-version
51+
yarn publish --access public

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ Pull requests are the best way to propose changes to the codebase (we use [Githu
6363

6464
1. Ensure you have the Node.js version we use, check [.npmrc](.npmrc). You can easily run multiple Node version with [nvm](https://github.com/nvm-sh/nvm).
6565

66-
2. Install dependencies `npm install`
66+
2. Install dependencies `yarn install`
6767

6868
3. See the `package.json` how to run the test.
6969

7070
4. Run the CLI locally
7171

7272
```bash
73-
npm run dev src/cli.ts <options>
73+
yarn run dev src/cli.ts <options>
7474
```
7575

7676
### Adding tests
@@ -94,7 +94,7 @@ For creating a README.md fixture, first create the readme input file for the tes
9494
### Use a Consistent Coding Style
9595

9696
- we use spaces.
97-
- You can try running `npm run lint && npm run format` for style unification
97+
- You can try running `yarn run lint && yarn run format` for style unification
9898

9999

100100
## License

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,13 @@ The following options are available via the CLI
4141

4242
```
4343
Options:
44-
--help Show help [boolean]
45-
--version Show version number [boolean]
46-
-t, --toc-level TOC level used for markdown
47-
[number] [required] [default: 2]
48-
-a, --action GitHub action file [string] [default: "action.yml"]
49-
--no-banner Print no banner
50-
-u, --update-readme Update readme file. [string]
51-
--line-breaks, --lb Used line breaks in the generated docs.
44+
--help Show help [boolean]
45+
--version Show version number [boolean]
46+
-t, --toc-level TOC level used for markdown [number] [default: 2]
47+
-a, --action GitHub action file [string] [default: "action.yml"]
48+
--no-banner Print no banner
49+
-u, --update-readme Update readme file. [string]
50+
-l, --line-breaks Used line breaks in the generated docs.
5251
[string] [choices: "CR", "LF", "CRLF"] [default: "LF"]
5352
```
5453

__tests__/action-docs.test.ts

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,105 @@
11
import { generateActionMarkdownDocs, Options } from "../src";
2-
import { readFileSync, writeFileSync } from "fs";
2+
import { readFileSync, writeFileSync, copyFileSync, rmSync, unlink } from "fs";
3+
import * as path from "path";
34
import { option } from "yargs";
45

5-
test("With defaults.", async () => {
6-
const markdown = await generateActionMarkdownDocs();
7-
const expected = <string>(
8-
readFileSync("__tests__/fixtures/default.output", "utf-8")
9-
);
6+
const fixtureDir = path.join("__tests__", "fixtures");
107

11-
expect(markdown).toEqual(expected);
8+
// By default an 'action.yml' is expected at the runtime location. Therefore we copy one during th test.
9+
beforeAll(() => {
10+
copyFileSync(path.join(fixtureDir, "action.yml"), "action.yml");
1211
});
1312

14-
test("A minimal action definition.", async () => {
15-
const markdown = await generateActionMarkdownDocs({
16-
actionFile: "__tests__/fixtures/minimal_action.yml",
13+
afterAll(() => {
14+
return unlink("action.yml", (err) => {
15+
if (err) throw err;
1716
});
18-
const expected = <string>(
19-
readFileSync("__tests__/fixtures/minimal_action.output", "utf-8")
20-
);
21-
22-
expect(markdown).toEqual(expected);
2317
});
2418

25-
test("All fields action definition.", async () => {
26-
const markdown = await generateActionMarkdownDocs({
27-
actionFile: "__tests__/fixtures/all_fields_action.yml",
19+
describe("Test output", () => {
20+
test("With defaults.", async () => {
21+
const markdown = await generateActionMarkdownDocs();
22+
const expected = <string>(
23+
readFileSync(path.join(fixtureDir, "default.output"), "utf-8")
24+
);
25+
26+
expect(markdown).toEqual(expected);
2827
});
29-
const expected = <string>(
30-
readFileSync("__tests__/fixtures/all_fields_action.output", "utf-8")
31-
);
3228

33-
expect(markdown).toEqual(expected);
34-
});
29+
test("A minimal action definition.", async () => {
30+
const markdown = await generateActionMarkdownDocs({
31+
actionFile: path.join(fixtureDir, "minimal_action.yml"),
32+
});
33+
const expected = <string>(
34+
readFileSync(path.join(fixtureDir, "minimal_action.output"), "utf-8")
35+
);
3536

36-
test("Update empty readme (all fields)", async () => {
37-
await testReadme(
38-
"__tests__/fixtures/all_fields_action.yml",
39-
"__tests__/fixtures/all_fields_readme.input",
40-
"__tests__/fixtures/all_fields_readme.output"
41-
);
42-
});
37+
expect(markdown).toEqual(expected);
38+
});
39+
40+
test("All fields action definition.", async () => {
41+
const markdown = await generateActionMarkdownDocs({
42+
actionFile: path.join(fixtureDir, "all_fields_action.yml"),
43+
});
44+
const expected = <string>(
45+
readFileSync(path.join(fixtureDir, "all_fields_action.output"), "utf-8")
46+
);
4347

44-
test("Update filled readme (all fields)", async () => {
45-
await testReadme(
46-
"__tests__/fixtures/all_fields_action.yml",
47-
"__tests__/fixtures/all_fields_readme_filled.input",
48-
"__tests__/fixtures/all_fields_readme_filled.output"
49-
);
48+
expect(markdown).toEqual(expected);
49+
});
5050
});
5151

52-
test("Update readme (all fields) CRLF", async () => {
53-
await testReadme(
54-
"__tests__/fixtures/all_fields_action.yml.crlf",
55-
"__tests__/fixtures/all_fields_readme.input.crlf",
56-
"__tests__/fixtures/all_fields_readme.output.crlf",
57-
{ lineBreaks: "CRLF" }
58-
);
52+
describe("Test update readme ", () => {
53+
test("Empty readme (all fields)", async () => {
54+
await testReadme({
55+
actionFile: path.join(fixtureDir, "all_fields_action.yml"),
56+
originalReadme: path.join(fixtureDir, "all_fields_readme.input"),
57+
fixtureReadme: path.join(fixtureDir, "all_fields_readme.output"),
58+
});
59+
});
60+
61+
test("Filled readme (all fields)", async () => {
62+
await testReadme({
63+
actionFile: path.join(fixtureDir, "all_fields_action.yml"),
64+
originalReadme: path.join(fixtureDir, "all_fields_readme_filled.input"),
65+
fixtureReadme: path.join(fixtureDir, "all_fields_readme_filled.output"),
66+
});
67+
});
68+
69+
test("Readme (all fields) with CRLF line breaks", async () => {
70+
await testReadme(
71+
{
72+
actionFile: path.join(fixtureDir, "all_fields_action.yml.crlf"),
73+
originalReadme: path.join(fixtureDir, "all_fields_readme.input.crlf"),
74+
fixtureReadme: path.join(fixtureDir, "all_fields_readme.output.crlf"),
75+
},
76+
{ lineBreaks: "CRLF" }
77+
);
78+
});
5979
});
6080

81+
interface ReadmeTestFixtures {
82+
actionFile: string;
83+
originalReadme: string;
84+
fixtureReadme: string;
85+
}
86+
6187
async function testReadme(
62-
actionFile: string,
63-
originalReadme: string,
64-
fixtureReadme: string,
88+
files: ReadmeTestFixtures,
6589
overwriteOptions?: Options
6690
) {
67-
const expected = <string>readFileSync(fixtureReadme, "utf-8");
68-
const original = <string>readFileSync(originalReadme, "utf-8");
91+
const expected = <string>readFileSync(files.fixtureReadme, "utf-8");
92+
const original = <string>readFileSync(files.originalReadme, "utf-8");
6993

7094
await generateActionMarkdownDocs({
71-
actionFile: actionFile,
95+
actionFile: files.actionFile,
7296
updateReadme: true,
73-
readmeFile: originalReadme,
97+
readmeFile: files.originalReadme,
7498
...overwriteOptions,
7599
});
76100

77-
const updated = <string>readFileSync(originalReadme, "utf-8");
101+
const updated = <string>readFileSync(files.originalReadme, "utf-8");
78102

79-
writeFileSync(originalReadme, original);
103+
writeFileSync(files.originalReadme, original);
80104
expect(updated).toEqual(expected);
81105
}

__tests__/cli.test.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,41 @@ import * as path from "path";
33

44
import { readFileSync, writeFileSync } from "fs";
55

6-
test("CLI: update readme default", async () => {
7-
await testReadme(
8-
"__tests__/fixtures/all_fields_action.yml",
9-
"__tests__/fixtures/all_fields_readme.input",
10-
"__tests__/fixtures/all_fields_readme.output"
11-
);
12-
});
6+
const fixtureDir = path.join("__tests__", "fixtures");
137

14-
test("CLI: update readme CRLF", async () => {
15-
await testReadme(
16-
"__tests__/fixtures/all_fields_action.yml.crlf",
17-
"__tests__/fixtures/all_fields_readme.input.crlf",
18-
"__tests__/fixtures/all_fields_readme.output.crlf",
19-
"--lb CRLF"
20-
);
8+
describe("CLI tests", () => {
9+
test("Update readme default", async () => {
10+
await testReadme(
11+
path.join(fixtureDir, "all_fields_action.yml"),
12+
path.join(fixtureDir, "all_fields_readme.input"),
13+
path.join(fixtureDir, "all_fields_readme.output")
14+
);
15+
});
16+
17+
test("Update readme with CRLF line breaks.", async () => {
18+
await testReadme(
19+
path.join(fixtureDir, "all_fields_action.yml.crlf"),
20+
path.join(fixtureDir, "all_fields_readme.input.crlf"),
21+
path.join(fixtureDir, "all_fields_readme.output.crlf"),
22+
"-l CRLF"
23+
);
24+
});
25+
26+
test("Console output with TOC 3 and no banner.", async () => {
27+
const result = await cli(
28+
`-a __tests__/fixtures/all_fields_action.yml -t 3 --no-banner`
29+
);
30+
31+
const expected = <string>(
32+
readFileSync(
33+
path.join(fixtureDir, "all_fields_action_toc3_cli.output"),
34+
"utf-8"
35+
)
36+
);
37+
38+
expect(result.code).toBe(0);
39+
expect(result.stdout).toEqual(`${expected}\n`);
40+
});
2141
});
2242

2343
interface CliRespone {
File renamed without changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### Description
2+
3+
Default test
4+
5+
### Inputs
6+
7+
| parameter | description | required | default |
8+
| - | - | - | - |
9+
| inputA | A description A | `false` | |
10+
| inputB | A description B | `true` | |
11+
| inputC | A description C | `true` | C |
12+
| inputD | A description D | `false` | D |
13+
14+
15+
### Outputs
16+
17+
| parameter | description |
18+
| - | - |
19+
| outputA | A description A |
20+
| outputB | A description B |
21+
22+
23+
### Runs
24+
25+
This action is an `node12` action.
26+

__tests__/linebreak.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import * as lb from "../src/linebreak";
22

3-
test("Line break types", () => {
4-
expect(lb.getLineBreakType("CR")).toEqual("CR");
5-
expect(lb.getLineBreakType("CRLF")).toEqual("CRLF");
6-
expect(lb.getLineBreakType("LF")).toEqual("LF");
7-
expect(lb.getLineBreakType("unknown")).toEqual("LF");
8-
});
3+
describe("Verify line break utils.", () => {
4+
test("Test line break string to type conversion.", () => {
5+
expect(lb.getLineBreakType("CR")).toEqual("CR");
6+
expect(lb.getLineBreakType("CRLF")).toEqual("CRLF");
7+
expect(lb.getLineBreakType("LF")).toEqual("LF");
8+
expect(lb.getLineBreakType("unknown")).toEqual("LF");
9+
});
910

10-
test("Line break types", () => {
11-
expect(lb.getLineBreak("CR")).toEqual("\r");
12-
expect(lb.getLineBreak("CRLF")).toEqual("\r\n");
13-
expect(lb.getLineBreak("LF")).toEqual("\n");
11+
test("Verify line break character.", () => {
12+
expect(lb.getLineBreak("CR")).toEqual("\r");
13+
expect(lb.getLineBreak("CRLF")).toEqual("\r\n");
14+
expect(lb.getLineBreak("LF")).toEqual("\n");
15+
});
1416
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "action-docs",
3-
"version": "0.3.0-beta.1",
3+
"version": "0.3.0-beta.3",
44
"description": "Generate GitHub action docs based on action.yml",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

0 commit comments

Comments
 (0)