Skip to content

Commit 93e4a5d

Browse files
vplasenciacedoor
authored andcommitted
refactor(cli): organize get group ids logic
1 parent 150a4c2 commit 93e4a5d

File tree

2 files changed

+51
-119
lines changed

2 files changed

+51
-119
lines changed

packages/cli/src/getGroupIds.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { SemaphoreSubgraph, SemaphoreEthers } from "@semaphore-protocol/data"
2+
import logSymbols from "log-symbols"
3+
import Spinner from "./spinner.js"
4+
5+
/**
6+
* Gets all group ids on the specified network
7+
* @param network The specified network
8+
*/
9+
export default async function getGroupIds(network): Promise<string[]> {
10+
let groupIds: string[]
11+
12+
const spinner = new Spinner("Fetching groups")
13+
14+
spinner.start()
15+
16+
try {
17+
const semaphoreSubgraph = new SemaphoreSubgraph(network)
18+
19+
groupIds = await semaphoreSubgraph.getGroupIds()
20+
21+
spinner.stop()
22+
} catch {
23+
try {
24+
const semaphoreEthers = new SemaphoreEthers(network)
25+
26+
groupIds = await semaphoreEthers.getGroupIds()
27+
28+
spinner.stop()
29+
} catch {
30+
spinner.stop()
31+
32+
console.info(`\n ${logSymbols.error}`, "error: unexpected error with the SemaphoreEthers package")
33+
34+
return null
35+
}
36+
}
37+
if (groupIds.length === 0) {
38+
console.info(`\n ${logSymbols.info}`, "info: there are no groups in this network\n")
39+
return null
40+
}
41+
return groupIds
42+
}

packages/cli/src/index.ts

Lines changed: 9 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import pacote from "pacote"
88
import { dirname } from "path"
99
import { fileURLToPath } from "url"
1010
import checkLatestVersion from "./checkLatestVersion.js"
11+
import getGroupIds from "./getGroupIds.js"
1112
import { getGroupId, getProjectName, getSupportedNetwork } from "./inquirerPrompts.js"
1213
import Spinner from "./spinner.js"
1314

@@ -95,37 +96,9 @@ program
9596
return
9697
}
9798

98-
let groupIds: string[]
99+
const groupIds = await getGroupIds(network)
99100

100-
const spinner = new Spinner("Fetching groups")
101-
102-
spinner.start()
103-
104-
try {
105-
const semaphoreSubgraph = new SemaphoreSubgraph(network)
106-
107-
groupIds = await semaphoreSubgraph.getGroupIds()
108-
109-
spinner.stop()
110-
} catch {
111-
try {
112-
const semaphoreEthers = new SemaphoreEthers(network)
113-
114-
groupIds = await semaphoreEthers.getGroupIds()
115-
116-
spinner.stop()
117-
} catch {
118-
spinner.stop()
119-
120-
console.info(`\n ${logSymbols.error}`, "error: unexpected error with the SemaphoreEthers package")
121-
122-
return
123-
}
124-
}
125-
if (groupIds.length === 0) {
126-
console.info(`\n ${logSymbols.info}`, "info: there are no groups in this network\n")
127-
return
128-
}
101+
if (groupIds === null) return
129102

130103
const content = `\n${groupIds.map((id: any) => ` - ${id}`).join("\n")}`
131104

@@ -149,38 +122,9 @@ program
149122
}
150123

151124
if (!groupId) {
152-
let groupIds: string[]
153-
154-
const spinnerGroups = new Spinner("Fetching groups")
155-
156-
spinnerGroups.start()
157-
158-
try {
159-
const semaphoreSubgraphGroups = new SemaphoreSubgraph(network)
160-
161-
groupIds = await semaphoreSubgraphGroups.getGroupIds()
162-
163-
spinnerGroups.stop()
164-
} catch {
165-
try {
166-
const semaphoreEthersGroups = new SemaphoreEthers(network)
167-
168-
groupIds = await semaphoreEthersGroups.getGroupIds()
125+
const groupIds = await getGroupIds(network)
169126

170-
spinnerGroups.stop()
171-
} catch {
172-
spinnerGroups.stop()
173-
174-
console.info(`\n ${logSymbols.error}`, "error: unexpected error with the SemaphoreEthers package")
175-
176-
return
177-
}
178-
}
179-
180-
if (groupIds.length === 0) {
181-
console.info(`\n ${logSymbols.info}`, "info: there are no groups in this network\n")
182-
return
183-
}
127+
if (groupIds === null) return
184128

185129
groupId = await getGroupId(groupIds)
186130
}
@@ -241,36 +185,9 @@ program
241185
}
242186

243187
if (!groupId) {
244-
let groupIds: string[]
245-
246-
const spinnerGroups = new Spinner("Fetching groups")
247-
248-
spinnerGroups.start()
249-
250-
try {
251-
const semaphoreSubgraphGroups = new SemaphoreSubgraph(network)
252-
253-
groupIds = await semaphoreSubgraphGroups.getGroupIds()
254-
255-
spinnerGroups.stop()
256-
} catch {
257-
try {
258-
const semaphoreEthersGroups = new SemaphoreEthers(network)
259-
260-
groupIds = await semaphoreEthersGroups.getGroupIds()
261-
262-
spinnerGroups.stop()
263-
} catch {
264-
spinnerGroups.stop()
265-
console.info(`\n ${logSymbols.error}`, "error: unexpected error with the SemaphoreEthers package")
266-
return
267-
}
268-
}
188+
const groupIds = await getGroupIds(network)
269189

270-
if (groupIds.length === 0) {
271-
console.info(`\n ${logSymbols.info}`, "info: there are no groups in this network\n")
272-
return
273-
}
190+
if (groupIds === null) return
274191

275192
groupId = await getGroupId(groupIds)
276193
}
@@ -332,36 +249,9 @@ program
332249
}
333250

334251
if (!groupId) {
335-
let groupIds: string[]
336-
337-
const spinnerGroups = new Spinner("Fetching groups")
338-
339-
spinnerGroups.start()
340-
341-
try {
342-
const semaphoreSubgraphGroups = new SemaphoreSubgraph(network)
343-
344-
groupIds = await semaphoreSubgraphGroups.getGroupIds()
252+
const groupIds = await getGroupIds(network)
345253

346-
spinnerGroups.stop()
347-
} catch {
348-
try {
349-
const semaphoreEthersGroups = new SemaphoreEthers(network)
350-
351-
groupIds = await semaphoreEthersGroups.getGroupIds()
352-
353-
spinnerGroups.stop()
354-
} catch {
355-
spinnerGroups.stop()
356-
console.info(`\n ${logSymbols.error}`, "error: unexpected error with the SemaphoreEthers package")
357-
return
358-
}
359-
}
360-
361-
if (groupIds.length === 0) {
362-
console.info(`\n ${logSymbols.info}`, "info: there are no groups in this network\n")
363-
return
364-
}
254+
if (groupIds === null) return
365255

366256
groupId = await getGroupId(groupIds)
367257
}

0 commit comments

Comments
 (0)