Skip to content

Commit 3ba7d3a

Browse files
committed
🎨 Move buildPath to src and add jest.
1 parent 2b471f3 commit 3ba7d3a

File tree

5 files changed

+2204
-0
lines changed

5 files changed

+2204
-0
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"api-to-go": "bin/api-to-go.js"
1818
},
1919
"dependencies": {
20+
"js-yaml": "^4.1.0",
2021
"node-fetch": "2"
22+
},
23+
"devDependencies": {
24+
"jest": "^28.1.1"
2125
}
2226
}

src/.api-to-go.test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
api.github.com:
2+
- /users/{user}
3+
- /users/{user}/repos

src/buildPath.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const yaml = require("js-yaml");
2+
const fs = require("fs");
3+
4+
function buildPath(apiUrl, configFile = "./.api-to-go.yaml") {
5+
// TODO: configのパターンを検知してpathを変換
6+
7+
const url = new URL(apiUrl);
8+
_buildPath(url, configFile)
9+
const path = `${url.hostname}${url.pathname}`
10+
11+
const pathArr = path.split("/")
12+
const pkg = pathArr[pathArr.length - 2].replace(/\./g, '')
13+
const last = pathArr[pathArr.length - 1] || "index"
14+
const struct = _capitalize(last)
15+
pathArr.pop()
16+
const dir = pathArr.join("/")
17+
return {
18+
struct,
19+
pkg,
20+
dir,
21+
jsonFilePath: `${dir}/${last}_sample.json`,
22+
goFilePath: `${dir}/${last}.go`
23+
}
24+
}
25+
26+
function _capitalize(str) {
27+
const lower = str.toLowerCase();
28+
return str.charAt(0).toUpperCase() + lower.slice(1);
29+
}
30+
31+
function _loadConfig(configFile) {
32+
try {
33+
return yaml.load(fs.readFileSync(configFile, 'utf8'));
34+
} catch (e) {
35+
console.log(e);
36+
}
37+
}
38+
39+
function _buildPath(url, configFile) {
40+
const cfg = _loadConfig(configFile)
41+
const hostCfg = cfg?.[url.hostname]
42+
if (hostCfg !== undefined) {
43+
for (let i = 0; i < hostCfg.length; i++) {
44+
console.log(hostCfg[i])
45+
}
46+
}
47+
}
48+
49+
module.exports = buildPath;

src/buildPath.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const buildPath = require('./buildPath');
2+
3+
test('build path', () => {
4+
const expected = {
5+
"dir": "api.github.com/users/github",
6+
"goFilePath": "api.github.com/users/github/repos.go",
7+
"jsonFilePath": "api.github.com/users/github/repos_sample.json",
8+
"pkg": "github",
9+
"struct": "Repos"
10+
}
11+
const received = buildPath(
12+
"https://api.github.com/users/github/repos",
13+
"./.api-to-go.test.yaml"
14+
)
15+
expect(received).toEqual(expected);
16+
});

0 commit comments

Comments
 (0)