@semaphore-protocol/cli 0.1.0 → 0.2.0

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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  <h1 align="center">
3
3
  Semaphore CLI
4
4
  </h1>
5
- <p align="center">A command line tool to set up a Semaphore project.</p>
5
+ <p align="center">A command line tool to set up your Semaphore project and query groups.</p>
6
6
  </p>
7
7
 
8
8
  <p align="center">
package/dist/index.js CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  /**
4
4
  * @module @semaphore-protocol/cli
5
- * @version 0.1.0
6
- * @file A command line tool to set up a Semaphore project and query groups.
5
+ * @version 0.2.0
6
+ * @file A command line tool to set up your Semaphore project and query groups.
7
7
  * @copyright Ethereum Foundation 2022
8
8
  * @license MIT
9
9
  * @see [Github]{@link https://github.com/semaphore-protocol/semaphore/tree/main/packages/cli}
@@ -12,69 +12,128 @@
12
12
  import { Subgraph } from '@semaphore-protocol/subgraph';
13
13
  import chalk from 'chalk';
14
14
  import { program } from 'commander';
15
+ import download from 'download';
15
16
  import figlet from 'figlet';
16
- import { readFileSync } from 'fs';
17
- import ora from 'ora';
17
+ import { readFileSync, existsSync, renameSync } from 'fs';
18
+ import logSymbols from 'log-symbols';
18
19
  import { dirname } from 'path';
19
20
  import { fileURLToPath } from 'url';
21
+ import ora from 'ora';
22
+
23
+ class Spinner {
24
+ constructor(text) {
25
+ this.text = text;
26
+ }
27
+ start() {
28
+ console.info("");
29
+ this.ora = ora({
30
+ text: this.text,
31
+ indent: 1
32
+ }).start();
33
+ }
34
+ stop() {
35
+ this.ora.stop();
36
+ process.stdout.cursorTo(0);
37
+ }
38
+ }
20
39
 
21
- /* eslint-disable no-console */
22
40
  const packagePath = `${dirname(fileURLToPath(import.meta.url))}/..`;
23
41
  const { version } = JSON.parse(readFileSync(`${packagePath}/package.json`, "utf8"));
24
42
  program
25
43
  .name("semaphore")
26
- .description("A command line tool to set up a Semaphore project and query groups.")
44
+ .description("A command line tool to set up your Semaphore project and query groups.")
27
45
  .version(version, "-v, --version", "Show Semaphore CLI version.")
28
- .addHelpText("before", `${figlet.textSync("Semaphore CLI")}\n`)
46
+ .addHelpText("before", `${figlet.textSync("Semaphore")}\n`)
47
+ .addHelpText("after", "\r")
29
48
  .helpOption(undefined, "Display this help.")
30
- .addHelpCommand("help [command]", "Display help for a specific command.");
49
+ .addHelpCommand("help [command]", "Display help for a specific command.")
50
+ .configureOutput({
51
+ outputError: (message) => {
52
+ console.info(`\n ${logSymbols.error}`, message);
53
+ }
54
+ });
55
+ program
56
+ .command("init")
57
+ .description("Initialize a Semaphore project with a supported template.")
58
+ .argument("<project-directory>", "Directory of the project.")
59
+ // .option("-t, --template <template-name>", "Supported Semaphore template.", "hardhat")
60
+ .action(async (projectDirectory) => {
61
+ // const originalDirectory = process.cwd()
62
+ const spinner = new Spinner(`Creating '${projectDirectory}' project`);
63
+ const templateURL = `https://registry.npmjs.org/@semaphore-protocol/cli-template-hardhat/-/cli-template-hardhat-${version}.tgz`;
64
+ if (existsSync(projectDirectory)) {
65
+ console.info(`\n ${logSymbols.error}`, `error: the '${projectDirectory}' folder already exists\n`);
66
+ return;
67
+ }
68
+ spinner.start();
69
+ await download(templateURL, "./", { extract: true });
70
+ renameSync("package", projectDirectory);
71
+ spinner.stop();
72
+ console.info(` ${logSymbols.success}`, `Your '${projectDirectory}' project is ready!\n`);
73
+ });
74
+ program
75
+ .command("get-groups")
76
+ .description("Get the list of groups from a supported network (Goerli or Arbitrum).")
77
+ .option("-n, --network <network-name>", "Supported Ethereum network.", "goerli")
78
+ .action(async ({ network }) => {
79
+ const subgraph = new Subgraph(network);
80
+ const spinner = new Spinner("Fetching groups");
81
+ spinner.start();
82
+ try {
83
+ const groups = await subgraph.getGroups();
84
+ spinner.stop();
85
+ if (groups.length === 0) {
86
+ console.info(` ${logSymbols.error}`, "error: there are no groups in this network\n");
87
+ return;
88
+ }
89
+ const content = `${groups.map(({ id }) => ` - ${id}`).join("\n")}`;
90
+ console.info(`${content}\n`);
91
+ }
92
+ catch (error) {
93
+ spinner.stop();
94
+ console.info(` ${logSymbols.error}`, "error: unexpected error with the Semaphore subgraph");
95
+ }
96
+ });
31
97
  program
32
98
  .command("get-group")
33
- .description("Get the data of a group from a supported network.")
99
+ .description("Get the data of a group from a supported network (Goerli or Arbitrum).")
34
100
  .argument("<group-id>", "Identifier of the group.")
35
101
  .option("-n, --network <network-name>", "Supported Ethereum network.", "goerli")
36
102
  .option("--members", "Show group members.")
37
103
  .option("--signals", "Show group signals.")
38
104
  .action(async (groupId, { network, members, signals }) => {
39
105
  const subgraph = new Subgraph(network);
40
- const spinner = ora({
41
- text: `Fetching group ${groupId}`,
42
- indent: 1
43
- }).start();
44
- const group = await subgraph.getGroup(groupId, { members, verifiedProofs: signals });
45
- let content = `\n ${chalk.bold("Id")}: ${group.id}\n`;
46
- content += ` ${chalk.bold("Admin")}: ${group.admin}\n`;
47
- content += ` ${chalk.bold("Merkle tree")}:\n`;
48
- content += ` Root: ${group.merkleTree.root}\n`;
49
- content += ` Depth: ${group.merkleTree.depth}\n`;
50
- content += ` Zero value: ${group.merkleTree.zeroValue}\n`;
51
- content += ` Number of leaves: ${group.merkleTree.numberOfLeaves}`;
52
- if (members) {
53
- content += `\n\n ${chalk.bold("Members")}: \n${group.members
54
- .map((member, i) => ` ${i}. ${member}`)
55
- .join("\n")}`;
106
+ const spinner = new Spinner(`Fetching group ${groupId}`);
107
+ spinner.start();
108
+ try {
109
+ const group = await subgraph.getGroup(groupId, { members, verifiedProofs: signals });
110
+ spinner.stop();
111
+ if (!group) {
112
+ console.info(` ${logSymbols.error}`, "error: the group does not exist\n");
113
+ return;
114
+ }
115
+ let content = ` ${chalk.bold("Id")}: ${group.id}\n`;
116
+ content += ` ${chalk.bold("Admin")}: ${group.admin}\n`;
117
+ content += ` ${chalk.bold("Merkle tree")}:\n`;
118
+ content += ` Root: ${group.merkleTree.root}\n`;
119
+ content += ` Depth: ${group.merkleTree.depth}\n`;
120
+ content += ` Zero value: ${group.merkleTree.zeroValue}\n`;
121
+ content += ` Number of leaves: ${group.merkleTree.numberOfLeaves}`;
122
+ if (members) {
123
+ content += `\n\n ${chalk.bold("Members")}: \n${group.members
124
+ .map((member, i) => ` ${i}. ${member}`)
125
+ .join("\n")}`;
126
+ }
127
+ if (signals) {
128
+ content += `\n\n ${chalk.bold("Signals")}: \n${group.verifiedProofs
129
+ .map(({ signal }) => ` - ${signal}`)
130
+ .join("\n")}`;
131
+ }
132
+ console.info(`${content}\n`);
56
133
  }
57
- if (signals) {
58
- content += `\n\n ${chalk.bold("Signals")}: \n${group.verifiedProofs
59
- .map(({ signal }) => ` - ${signal}`)
60
- .join("\n")}`;
134
+ catch (error) {
135
+ spinner.stop();
136
+ console.info(` ${logSymbols.error}`, "error: unexpected error with the Semaphore subgraph");
61
137
  }
62
- spinner.stop();
63
- console.log(`${content}\n`);
64
- });
65
- program
66
- .command("get-groups")
67
- .description("Get the list of groups from a supported network.")
68
- .option("-n, --network <network-name>", "Supported Ethereum network.", "goerli")
69
- .action(async ({ network }) => {
70
- const subgraph = new Subgraph(network);
71
- const spinner = ora({
72
- text: `Fetching groups`,
73
- indent: 1
74
- }).start();
75
- const groups = await subgraph.getGroups();
76
- const content = `\n${groups.map(({ id }) => ` - ${id}`).join("\n")}`;
77
- spinner.stop();
78
- console.log(`${content}\n`);
79
138
  });
80
139
  program.parse();
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/cli",
3
3
  "type": "module",
4
- "version": "0.1.0",
5
- "description": "A command line tool to set up a Semaphore project and query groups.",
4
+ "version": "0.2.0",
5
+ "description": "A command line tool to set up your Semaphore project and query groups.",
6
6
  "license": "MIT",
7
7
  "bin": {
8
8
  "semaphore": "dist/index.js"
@@ -40,9 +40,12 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@semaphore-protocol/subgraph": "2.6.1",
43
+ "@types/clear": "^0.1.2",
43
44
  "chalk": "^5.1.2",
44
45
  "commander": "^9.4.1",
46
+ "download": "^8.0.0",
45
47
  "figlet": "^1.5.2",
48
+ "log-symbols": "^5.1.0",
46
49
  "ora": "^6.1.2"
47
50
  }
48
51
  }
package/src/index.ts CHANGED
@@ -1,85 +1,137 @@
1
- /* eslint-disable no-console */
2
1
  import { Subgraph } from "@semaphore-protocol/subgraph"
3
2
  import chalk from "chalk"
4
3
  import { program } from "commander"
4
+ import download from "download"
5
5
  import figlet from "figlet"
6
- import { readFileSync } from "fs"
7
- import ora from "ora"
6
+ import { existsSync, readFileSync, renameSync } from "fs"
7
+ import logSymbols from "log-symbols"
8
8
  import { dirname } from "path"
9
9
  import { fileURLToPath } from "url"
10
+ import Spinner from "./spinner.js"
10
11
 
11
12
  const packagePath = `${dirname(fileURLToPath(import.meta.url))}/..`
12
13
  const { version } = JSON.parse(readFileSync(`${packagePath}/package.json`, "utf8"))
13
14
 
14
15
  program
15
16
  .name("semaphore")
16
- .description("A command line tool to set up a Semaphore project and query groups.")
17
+ .description("A command line tool to set up your Semaphore project and query groups.")
17
18
  .version(version, "-v, --version", "Show Semaphore CLI version.")
18
- .addHelpText("before", `${figlet.textSync("Semaphore CLI")}\n`)
19
+ .addHelpText("before", `${figlet.textSync("Semaphore")}\n`)
20
+ .addHelpText("after", "\r")
19
21
  .helpOption(undefined, "Display this help.")
20
22
  .addHelpCommand("help [command]", "Display help for a specific command.")
23
+ .configureOutput({
24
+ outputError: (message) => {
25
+ console.info(`\n ${logSymbols.error}`, message)
26
+ }
27
+ })
21
28
 
22
29
  program
23
- .command("get-group")
24
- .description("Get the data of a group from a supported network.")
25
- .argument("<group-id>", "Identifier of the group.")
26
- .option("-n, --network <network-name>", "Supported Ethereum network.", "goerli")
27
- .option("--members", "Show group members.")
28
- .option("--signals", "Show group signals.")
29
- .action(async (groupId, { network, members, signals }) => {
30
- const subgraph = new Subgraph(network)
31
-
32
- const spinner = ora({
33
- text: `Fetching group ${groupId}`,
34
- indent: 1
35
- }).start()
36
-
37
- const group = await subgraph.getGroup(groupId, { members, verifiedProofs: signals })
38
-
39
- let content = `\n ${chalk.bold("Id")}: ${group.id}\n`
40
- content += ` ${chalk.bold("Admin")}: ${group.admin}\n`
41
- content += ` ${chalk.bold("Merkle tree")}:\n`
42
- content += ` Root: ${group.merkleTree.root}\n`
43
- content += ` Depth: ${group.merkleTree.depth}\n`
44
- content += ` Zero value: ${group.merkleTree.zeroValue}\n`
45
- content += ` Number of leaves: ${group.merkleTree.numberOfLeaves}`
46
-
47
- if (members) {
48
- content += `\n\n ${chalk.bold("Members")}: \n${group.members
49
- .map((member: string, i: number) => ` ${i}. ${member}`)
50
- .join("\n")}`
30
+ .command("init")
31
+ .description("Initialize a Semaphore project with a supported template.")
32
+ .argument("<project-directory>", "Directory of the project.")
33
+ // .option("-t, --template <template-name>", "Supported Semaphore template.", "hardhat")
34
+ .action(async (projectDirectory) => {
35
+ // const originalDirectory = process.cwd()
36
+ const spinner = new Spinner(`Creating '${projectDirectory}' project`)
37
+ const templateURL = `https://registry.npmjs.org/@semaphore-protocol/cli-template-hardhat/-/cli-template-hardhat-${version}.tgz`
38
+
39
+ if (existsSync(projectDirectory)) {
40
+ console.info(`\n ${logSymbols.error}`, `error: the '${projectDirectory}' folder already exists\n`)
41
+ return
51
42
  }
52
43
 
53
- if (signals) {
54
- content += `\n\n ${chalk.bold("Signals")}: \n${group.verifiedProofs
55
- .map(({ signal }: any) => ` - ${signal}`)
56
- .join("\n")}`
57
- }
44
+ spinner.start()
45
+
46
+ await download(templateURL, "./", { extract: true })
47
+
48
+ renameSync("package", projectDirectory)
58
49
 
59
50
  spinner.stop()
60
51
 
61
- console.log(`${content}\n`)
52
+ console.info(` ${logSymbols.success}`, `Your '${projectDirectory}' project is ready!\n`)
62
53
  })
63
54
 
64
55
  program
65
56
  .command("get-groups")
66
- .description("Get the list of groups from a supported network.")
57
+ .description("Get the list of groups from a supported network (Goerli or Arbitrum).")
67
58
  .option("-n, --network <network-name>", "Supported Ethereum network.", "goerli")
68
59
  .action(async ({ network }) => {
69
60
  const subgraph = new Subgraph(network)
61
+ const spinner = new Spinner("Fetching groups")
70
62
 
71
- const spinner = ora({
72
- text: `Fetching groups`,
73
- indent: 1
74
- }).start()
63
+ spinner.start()
75
64
 
76
- const groups = await subgraph.getGroups()
65
+ try {
66
+ const groups = await subgraph.getGroups()
77
67
 
78
- const content = `\n${groups.map(({ id }: any) => ` - ${id}`).join("\n")}`
68
+ spinner.stop()
79
69
 
80
- spinner.stop()
70
+ if (groups.length === 0) {
71
+ console.info(` ${logSymbols.error}`, "error: there are no groups in this network\n")
72
+ return
73
+ }
74
+
75
+ const content = `${groups.map(({ id }: any) => ` - ${id}`).join("\n")}`
76
+
77
+ console.info(`${content}\n`)
78
+ } catch (error) {
79
+ spinner.stop()
80
+
81
+ console.info(` ${logSymbols.error}`, "error: unexpected error with the Semaphore subgraph")
82
+ }
83
+ })
81
84
 
82
- console.log(`${content}\n`)
85
+ program
86
+ .command("get-group")
87
+ .description("Get the data of a group from a supported network (Goerli or Arbitrum).")
88
+ .argument("<group-id>", "Identifier of the group.")
89
+ .option("-n, --network <network-name>", "Supported Ethereum network.", "goerli")
90
+ .option("--members", "Show group members.")
91
+ .option("--signals", "Show group signals.")
92
+ .action(async (groupId, { network, members, signals }) => {
93
+ const subgraph = new Subgraph(network)
94
+ const spinner = new Spinner(`Fetching group ${groupId}`)
95
+
96
+ spinner.start()
97
+
98
+ try {
99
+ const group = await subgraph.getGroup(groupId, { members, verifiedProofs: signals })
100
+
101
+ spinner.stop()
102
+
103
+ if (!group) {
104
+ console.info(` ${logSymbols.error}`, "error: the group does not exist\n")
105
+
106
+ return
107
+ }
108
+
109
+ let content = ` ${chalk.bold("Id")}: ${group.id}\n`
110
+ content += ` ${chalk.bold("Admin")}: ${group.admin}\n`
111
+ content += ` ${chalk.bold("Merkle tree")}:\n`
112
+ content += ` Root: ${group.merkleTree.root}\n`
113
+ content += ` Depth: ${group.merkleTree.depth}\n`
114
+ content += ` Zero value: ${group.merkleTree.zeroValue}\n`
115
+ content += ` Number of leaves: ${group.merkleTree.numberOfLeaves}`
116
+
117
+ if (members) {
118
+ content += `\n\n ${chalk.bold("Members")}: \n${group.members
119
+ .map((member: string, i: number) => ` ${i}. ${member}`)
120
+ .join("\n")}`
121
+ }
122
+
123
+ if (signals) {
124
+ content += `\n\n ${chalk.bold("Signals")}: \n${group.verifiedProofs
125
+ .map(({ signal }: any) => ` - ${signal}`)
126
+ .join("\n")}`
127
+ }
128
+
129
+ console.info(`${content}\n`)
130
+ } catch (error) {
131
+ spinner.stop()
132
+
133
+ console.info(` ${logSymbols.error}`, "error: unexpected error with the Semaphore subgraph")
134
+ }
83
135
  })
84
136
 
85
137
  program.parse()
package/src/spinner.ts ADDED
@@ -0,0 +1,25 @@
1
+ import ora, { Ora } from "ora"
2
+
3
+ export default class Spinner {
4
+ private text: string
5
+ private ora: Ora
6
+
7
+ constructor(text: string) {
8
+ this.text = text
9
+ }
10
+
11
+ start() {
12
+ console.info("")
13
+
14
+ this.ora = ora({
15
+ text: this.text,
16
+ indent: 1
17
+ }).start()
18
+ }
19
+
20
+ stop() {
21
+ this.ora.stop()
22
+
23
+ process.stdout.cursorTo(0)
24
+ }
25
+ }