@kokiito0926/superhacker 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +2 -2
  2. package/index.js +33 -44
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -11,7 +11,7 @@ $ npm install --global @kokiito0926/superhacker
11
11
 
12
12
  ## 使用方法
13
13
 
14
- ストーリーの一覧を表示します。
14
+ ストーリーの一覧を表示します。
15
15
  --limitのオプションで表示件数を制限することもできます。
16
16
 
17
17
  ```bash
@@ -25,7 +25,7 @@ $ superhacker list --limit 10
25
25
  $ superhacker comment --id 46726480
26
26
  ```
27
27
 
28
- コメントのIDを指定して、すべての返信を再帰的に取得します。
28
+ コメントのIDを指定して、すべての返信を再帰的に取得します。
29
29
  デフォルトでは、ツリー構造で取得されますが、フラットな形式で取得することもできます。
30
30
 
31
31
  ```bash
package/index.js CHANGED
@@ -5,7 +5,7 @@
5
5
  // >> $ node ./index.js comment --id 2921983
6
6
  // >> $ node ./index.js comments --id 8863 --format flat
7
7
 
8
- import { minimist, argv } from "zx";
8
+ import { argv } from "zx";
9
9
 
10
10
  const BASE_URL = "https://hacker-news.firebaseio.com/v0";
11
11
 
@@ -47,28 +47,22 @@ if (!command) {
47
47
  }
48
48
  // console.log(command);
49
49
 
50
- const args = minimist(process.argv.slice(2));
51
- const id = args.id;
52
- const limit = args.limit ? parseInt(args.limit) : null;
53
- const flat = args.format ? args.format === "flat" : false;
50
+ const id = argv?.id;
51
+ const limit = argv?.limit ? parseInt(argv?.limit) : null;
52
+ const flat = argv?.format ? argv?.format === "flat" : false;
54
53
 
55
54
  if (command === "list") {
56
- try {
57
- const idsResponse = await fetch(`${BASE_URL}/topstories.json`);
58
- let ids = await idsResponse.json();
55
+ const idsResponse = await fetch(`${BASE_URL}/topstories.json`);
56
+ let ids = await idsResponse.json();
59
57
 
60
- if (limit > 0) {
61
- ids = ids.slice(0, limit);
62
- }
58
+ if (limit > 0) {
59
+ ids = ids.slice(0, limit);
60
+ }
63
61
 
64
- const stories = await Promise.all(ids.map((id) => getItem(id)));
65
- const sortedStories = stories.sort((a, b) => b.score - a.score);
62
+ const stories = await Promise.all(ids.map((id) => getItem(id)));
63
+ const sortedStories = stories.sort((a, b) => b.score - a.score);
66
64
 
67
- console.log(JSON.stringify(sortedStories, null, 2));
68
- } catch (e) {
69
- console.error(`Error fetching list: ${e.message}`);
70
- process.exit(1);
71
- }
65
+ console.log(JSON.stringify(sortedStories, null, 2));
72
66
  } else if (command === "comment") {
73
67
  if (!id) {
74
68
  console.error("Error: ID is required via --id or stdin.");
@@ -82,32 +76,27 @@ if (command === "list") {
82
76
  process.exit(1);
83
77
  }
84
78
 
85
- try {
86
- const rootItem = await getItem(id);
87
- if (!rootItem) {
88
- console.log(JSON.stringify([]));
89
- process.exit(0);
90
- }
79
+ const rootItem = await getItem(id);
80
+ if (!rootItem) {
81
+ console.log(JSON.stringify([]));
82
+ process.exit(0);
83
+ }
91
84
 
92
- const flatComments = rootItem.kids ? await getCommentsRecursive(rootItem.kids) : [];
93
- // console.log(flatComments);
94
-
95
- if (flat) {
96
- console.log(JSON.stringify([rootItem, ...flatComments], null, 2));
97
- } else {
98
- console.log(
99
- JSON.stringify(
100
- {
101
- ...rootItem,
102
- replies: buildTree(flatComments, parseInt(id)),
103
- },
104
- null,
105
- 2,
106
- ),
107
- );
108
- }
109
- } catch (e) {
110
- console.error(`Error fetching comments: ${e.message}`);
111
- process.exit(1);
85
+ const flatComments = rootItem.kids ? await getCommentsRecursive(rootItem.kids) : [];
86
+ // console.log(flatComments);
87
+
88
+ if (flat) {
89
+ console.log(JSON.stringify([rootItem, ...flatComments], null, 2));
90
+ } else {
91
+ console.log(
92
+ JSON.stringify(
93
+ {
94
+ ...rootItem,
95
+ replies: buildTree(flatComments, parseInt(id)),
96
+ },
97
+ null,
98
+ 2,
99
+ ),
100
+ );
112
101
  }
113
102
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kokiito0926/superhacker",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "private": false,
5
5
  "description": "Hacker NewsのAPIを利用して、ストーリーやコメントをJSON形式で取得することができるコマンドラインのツールです。",
6
6
  "keywords": [