Skip to content

Commit 421999a

Browse files
committed
feat: add readme test
1 parent 63d68df commit 421999a

File tree

8 files changed

+3062
-137
lines changed

8 files changed

+3062
-137
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ jobs:
2525
if: steps.yarn-cache.outputs.cache-hit != 'true'
2626
- run: yarn lint
2727
- run: yarn typecheck
28+
- run: yarn test

jest.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Config } from '@jest/types'
2+
3+
const config: Config.InitialOptions = {
4+
preset: 'ts-jest',
5+
testEnvironment: 'node',
6+
roots: ['tests']
7+
}
8+
9+
export default config

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"build": "cd apis/jma/himawari && ts-node ../../../src/index.ts && tsc",
55
"lint": "eslint --ext .ts --ignore-path .gitignore .",
66
"lint:fix": "npm run lint -- --fix",
7-
"typecheck": "tsc --noEmit"
7+
"typecheck": "tsc --noEmit",
8+
"test": "jest"
89
},
910
"eslintConfig": {
1011
"env": {
@@ -15,6 +16,7 @@
1516
"extends": [
1617
"standard",
1718
"plugin:@typescript-eslint/recommended",
19+
"plugin:jest/recommended",
1820
"plugin:prettier/recommended",
1921
"prettier"
2022
],
@@ -43,6 +45,7 @@
4345
"trailingComma": "none"
4446
},
4547
"devDependencies": {
48+
"@types/jest": "^26.0.20",
4649
"@types/node": "^14.14.31",
4750
"@typescript-eslint/eslint-plugin": "^4.15.2",
4851
"@typescript-eslint/parser": "^4.15.2",
@@ -51,15 +54,17 @@
5154
"eslint-config-prettier": "^8.1.0",
5255
"eslint-config-standard": "^16.0.2",
5356
"eslint-plugin-import": "^2.22.1",
54-
"eslint-plugin-jest": "^24.1.5",
57+
"eslint-plugin-jest": "^24.1.7",
5558
"eslint-plugin-node": "^11.1.0",
5659
"eslint-plugin-prettier": "^3.3.1",
5760
"eslint-plugin-promise": "^4.3.1",
5861
"eslint-plugin-react": "^7.22.0",
5962
"eslint-plugin-standard": "^5.0.0",
6063
"front-matter": "^4.0.2",
64+
"jest": "^26.6.3",
6165
"prettier": "^2.2.1",
6266
"standard-version": "^9.1.1",
67+
"ts-jest": "^26.5.3",
6368
"ts-node": "^9.1.1",
6469
"typescript": "^4.2.2"
6570
}

src/createApiDocs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const createApiDocs = (input: string, trailingSlash: boolean) => {
88
if (c.isDir) {
99
walkDir(c.tree)
1010
} else {
11-
const endpoint = `${tree.path.replace('api', '')}/${c.name.replace(/(^index)?\.ts$/, '')}${
11+
const endpoint = `${tree.path.replace(input, '')}/${c.name.replace(/(^index)?\.ts$/, '')}${
1212
trailingSlash ? '/' : ''
1313
}`
1414

src/createReadme.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
export type Attributes = {
2+
title: string
3+
description: string
4+
homepage: string
5+
image: string
6+
baseURL: string
7+
trailingSlash: boolean
8+
}
9+
10+
export const createReadme = (
11+
packageName: string,
12+
org: string,
13+
attributes: Attributes,
14+
body: string,
15+
apiDocs: string
16+
) => {
17+
return `# ${packageName} ${attributes.title}
18+
<br />
19+
<div align="center">
20+
<img src="https://aspida.github.io/api-types/assets/${org}/${attributes.image}" alt="${packageName} ${attributes.title}" />
21+
</div>
22+
<br />
23+
24+
> [${attributes.title}](${attributes.homepage}) - ${attributes.description}
25+
<div align="center">
26+
<a href="https://www.npmjs.com/package/${packageName}">
27+
<img src="https://img.shields.io/npm/v/${packageName}" alt="npm version" />
28+
</a>
29+
<a href="https://www.npmjs.com/package/${packageName}">
30+
<img src="https://img.shields.io/npm/dm/${packageName}" alt="npm download" />
31+
</a>
32+
<a href="https://github.com/aspida/api-types/actions?query=workflow%3A%22Node.js+CI%22">
33+
<img src="https://github.com/aspida/api-types/workflows/Node.js%20CI/badge.svg?branch=master" alt="Node.js CI" />
34+
</a>
35+
</div>
36+
<br />
37+
<br />
38+
<br />
39+
40+
${body}
41+
42+
## Getting Started
43+
44+
- Using [npm](https://www.npmjs.com/):
45+
46+
\`\`\`sh
47+
$ npm install ${packageName} @aspida/axios axios
48+
\`\`\`
49+
50+
- Using [Yarn](https://yarnpkg.com/):
51+
52+
\`\`\`sh
53+
$ yarn add ${packageName} @aspida/axios axios
54+
\`\`\`
55+
56+
\`index.ts\`
57+
\`\`\`ts
58+
import api from "${packageName}"
59+
import aspida from '@aspida/axios'
60+
61+
;(async () => {
62+
const client = api(aspida())
63+
const resBody = await client.foo.baz.$get()
64+
console.log(resBody)
65+
})()
66+
\`\`\`
67+
68+
## API Documents
69+
70+
baseURL: ${attributes.baseURL}
71+
<br />
72+
73+
${apiDocs}
74+
75+
## License
76+
77+
${packageName} is licensed under a [MIT License](https://github.com/aspida/api-types/blob/master/LICENSE).
78+
`
79+
}

src/index.ts

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@ import path from 'path'
33
import fm from 'front-matter'
44
import build from 'aspida/dist/buildTemplate'
55
import { createApiDocs } from './createApiDocs'
6+
import { Attributes, createReadme } from './createReadme'
67

78
const name = path.basename(process.cwd())
89
const org = path.basename(path.join(process.cwd(), '../'))
910
const packageName = `@api-types/${org}-${name}`
1011
const configText = fs.readFileSync('config.md', 'utf8')
11-
const { attributes, body } = fm<{
12-
title: string
13-
description: string
14-
homepage: string
15-
image: string
16-
baseURL: string
17-
trailingSlash: boolean
18-
}>(configText)
19-
12+
const { attributes, body } = fm<Attributes>(configText)
2013
const packageJson = {
2114
name: packageName,
2215
version: '0.0.0',
@@ -63,68 +56,5 @@ fs.writeFileSync(
6356

6457
fs.writeFileSync(
6558
'README.md',
66-
`# ${packageName} ${attributes.title}
67-
<br />
68-
<div align="center">
69-
<img src="https://aspida.github.io/api-types/assets/${org}/${
70-
attributes.image
71-
}" alt="${packageName} ${attributes.title}" />
72-
</div>
73-
<br />
74-
75-
> [${attributes.title}](${attributes.homepage}) - ${attributes.description}
76-
<div align="center">
77-
<a href="https://www.npmjs.com/package/${packageName}">
78-
<img src="https://img.shields.io/npm/v/${packageName}" alt="npm version" />
79-
</a>
80-
<a href="https://www.npmjs.com/package/${packageName}">
81-
<img src="https://img.shields.io/npm/dm/${packageName}" alt="npm download" />
82-
</a>
83-
<a href="https://github.com/aspida/api-types/actions?query=workflow%3A%22Node.js+CI%22">
84-
<img src="https://github.com/aspida/api-types/workflows/Node.js%20CI/badge.svg?branch=master" alt="Node.js CI" />
85-
</a>
86-
</div>
87-
<br />
88-
<br />
89-
<br />
90-
91-
${body}
92-
93-
## Getting Started
94-
95-
- Using [npm](https://www.npmjs.com/):
96-
97-
\`\`\`sh
98-
$ npm install ${packageName} @aspida/axios axios
99-
\`\`\`
100-
101-
- Using [Yarn](https://yarnpkg.com/):
102-
103-
\`\`\`sh
104-
$ yarn add ${packageName} @aspida/axios axios
105-
\`\`\`
106-
107-
\`index.ts\`
108-
\`\`\`ts
109-
import api from "${packageName}"
110-
import aspida from '@aspida/axios'
111-
112-
;(async () => {
113-
const client = api(aspida())
114-
const resBody = await client.foo.baz.$get()
115-
console.log(resBody)
116-
})()
117-
\`\`\`
118-
119-
## API Documents
120-
121-
baseURL: ${attributes.baseURL}
122-
<br />
123-
124-
${createApiDocs('api', attributes.trailingSlash)}
125-
126-
## License
127-
128-
${packageName} is licensed under a [MIT License](https://github.com/aspida/api-types/blob/master/LICENSE).
129-
`
59+
createReadme(packageName, org, attributes, body, createApiDocs('api', attributes.trailingSlash))
13060
)

tests/config.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import fm from 'front-matter'
4+
import { createApiDocs } from '../src/createApiDocs'
5+
import { Attributes, createReadme } from '../src/createReadme'
6+
7+
test('create README', () => {
8+
fs.readdirSync(path.join(__dirname, '../apis')).forEach(org => {
9+
fs.readdirSync(path.join(__dirname, '../apis', org)).forEach(name => {
10+
const { attributes, body } = fm<Attributes>(
11+
fs.readFileSync(path.join(__dirname, '../apis', org, name, 'config.md'), 'utf8')
12+
)
13+
expect(fs.readFileSync(path.join(__dirname, '../apis', org, name, 'README.md'), 'utf8')).toBe(
14+
createReadme(
15+
`@api-types/${org}-${name}`,
16+
org,
17+
attributes,
18+
body,
19+
createApiDocs(path.join(__dirname, '../apis', org, name, 'api'), attributes.trailingSlash)
20+
)
21+
)
22+
})
23+
})
24+
})

0 commit comments

Comments
 (0)