Skip to content

Commit 2b471f3

Browse files
committed
✨ Add feature that save json sample.
1 parent 236d8bf commit 2b471f3

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ node_modules
33
vendor
44
.gitignore
55
*.go
6+
*.json
67
tmp

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ yarn setup
1515
cd [your project dir]
1616
api-to-go https://api.github.com/users
1717
# > ...
18+
# > saved: api.github.com/users_sample.json
1819
# > generated: api.github.com/users.go
1920
```
2021

2122
```sh
2223
tree .
2324
# > .
2425
# > └── api.github.com
25-
# > └── users.go
26+
# > ├── users.go
27+
# > └── users_sample.json
2628
```
2729

2830
```go

bin/api-to-go.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ function run(url) {
88
fetch(apiUrl)
99
.then(res => res.json())
1010
.then(json => {
11-
const path = _parseUrl(apiUrl)
12-
const res = jsonToGo(JSON.stringify(json), path.struct);
13-
const content = _buildContent(res.go, path.pkg)
14-
fs.mkdirSync(path.dir, {recursive: true})
15-
fs.writeFile(path.filePath, content, (err) => {
16-
if (err) throw err;
17-
console.log(json)
18-
console.log()
19-
console.log(`generated: ${path.filePath}`)
20-
});
11+
const path = _parseUrl(apiUrl)
12+
const res = jsonToGo(JSON.stringify(json), path.struct);
13+
const content = _buildContent(res.go, path.pkg)
14+
fs.mkdirSync(path.dir, {recursive: true})
15+
console.log(json)
16+
console.log()
17+
fs.writeFile(path.jsonFilePath, JSON.stringify(json,null,"\t"), (err) => {
18+
if (err) throw err;
19+
console.log(`saved: ${path.jsonFilePath}`)
20+
});
21+
fs.writeFile(path.goFilePath, content, (err) => {
22+
if (err) throw err;
23+
console.log(`generated: ${path.goFilePath}`)
24+
});
2125
}
2226
);
2327
}
@@ -28,16 +32,15 @@ function _parseUrl(apiUrl) {
2832
const pathArr = path.split("/")
2933
const pkg = pathArr[pathArr.length - 2].replace(/\./g, '')
3034
const last = pathArr[pathArr.length - 1] || "index"
31-
const file = last + ".go"
3235
const struct = _capitalize(last)
3336
pathArr.pop()
3437
const dir = pathArr.join("/")
3538
return {
3639
struct,
37-
file,
3840
pkg,
3941
dir,
40-
filePath: `${dir}/${file}`
42+
jsonFilePath: `${dir}/${last}_sample.json`,
43+
goFilePath: `${dir}/${last}.go`
4144
}
4245
}
4346

0 commit comments

Comments
 (0)