-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart.ts
More file actions
79 lines (67 loc) · 2.68 KB
/
start.ts
File metadata and controls
79 lines (67 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { PKCLogger } from "../../../util.js";
import { BaseCommand } from "../../base-command.js";
import { Args, Flags } from "@oclif/core";
import pLimit from "p-limit";
export default class Start extends BaseCommand {
static override description = "Start a community";
static override strict = false; // To allow for variable length arguments
static override args = {
addresses: Args.string({
name: "addresses", // name of arg to show in help and reference with args[name]
required: true,
description: "Addresses of communities to start. Separated by space"
})
};
static override flags = {
concurrency: Flags.integer({
description: "Number of communities to start in parallel",
default: 5,
min: 0
})
};
static override examples = [
"bitsocial community start plebbit.bso",
"bitsocial community start 12D3KooWG3XbzoVyAE6Y9vHZKF64Yuuu4TjdgQKedk14iYmTEPWu",
{
description: "Start all communities in your data path",
command: "bitsocial community start $(bitsocial community list -q)"
},
{
description: "Start communities sequentially (no concurrency)",
command: "bitsocial community start $(bitsocial community list -q) --concurrency 1"
}
];
async run() {
const { argv, flags } = await this.parse(Start);
const addresses = <string[]>argv;
const log = PKCLogger("bitsocial-cli:commands:community:start");
log(`addresses: `, addresses);
log(`flags: `, flags);
const concurrency = Math.max(flags.concurrency, 1);
const limit = pLimit(concurrency);
const pkc = await this._connectToPkcRpc(flags.pkcRpcUrl.toString());
const errors: { address: string; error: Error }[] = [];
const tasks = addresses.map((address) =>
limit(async () => {
try {
const community = await pkc.createCommunity({ address });
await community.start();
this.log(address);
} catch (e) {
const error = e instanceof Error ? e : new Error(typeof e === "string" ? e : JSON.stringify(e));
//@ts-expect-error
error.details = { ...error.details, address };
errors.push({ address, error });
}
})
);
await Promise.all(tasks);
await pkc.destroy();
if (errors.length > 0) {
for (const { error } of errors) {
console.error(error);
}
this.exit(1);
}
}
}