Skip to content

Commit 527c560

Browse files
authored
Merge pull request #31 from Yuripetusko/master
Don't use commander for cli tools as it doesn't work through package.json bin
2 parents 6974645 + b9f4095 commit 527c560

File tree

8 files changed

+199
-225
lines changed

8 files changed

+199
-225
lines changed

cli/consolidate.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
#! /usr/bin/env node
2-
import commander from "commander";
3-
import { Options } from "../src/tools/types";
42
import fs from "fs";
53
import JsonAdapter from "../src/tools/consolidator/adapters/json";
64
import { Consolidator } from "../src/tools/consolidator/consolidator";
5+
import arg from "arg";
76

8-
export const addTo = (program: commander.CommanderStatic | typeof commander) =>
9-
program
10-
.command("consolidate")
11-
.option("--json <json>", "The JSON file from which to consolidate")
12-
.action(async (opts: Options) => {
13-
const file = opts.json;
14-
if (!file) {
15-
console.error("File path must be provided");
16-
process.exit(1);
17-
}
18-
// Check the JSON file exists and is reachable
19-
try {
20-
fs.accessSync(file, fs.constants.R_OK);
21-
} catch (e) {
22-
console.error(
23-
"File is not readable. Are you providing the right path?"
24-
);
25-
process.exit(1);
26-
}
27-
const ja = new JsonAdapter(file);
28-
const con = new Consolidator(ja);
29-
con.consolidate();
30-
});
7+
const consolidate = async () => {
8+
const args = arg({
9+
"--json": String, // The JSON file from which to consolidate
10+
});
11+
12+
const file = args["--json"];
13+
if (!file) {
14+
console.error("File path must be provided");
15+
process.exit(1);
16+
}
17+
// Check the JSON file exists and is reachable
18+
try {
19+
fs.accessSync(file, fs.constants.R_OK);
20+
} catch (e) {
21+
console.error("File is not readable. Are you providing the right path?");
22+
process.exit(1);
23+
}
24+
const ja = new JsonAdapter(file);
25+
const con = new Consolidator(ja);
26+
con.consolidate();
27+
};
28+
29+
consolidate();

cli/fetch.ts

Lines changed: 70 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#! /usr/bin/env node
2-
import { Options } from "../src/tools/types";
32
import {
43
deeplog,
54
getApi,
@@ -9,85 +8,80 @@ import {
98
} from "../src/tools/utils";
109
import fs from "fs";
1110
import fetchRemarks from "../src/tools/fetchRemarks";
12-
import commander from "commander";
11+
import arg from "arg";
1312

14-
export const addTo = (program: commander.CommanderStatic | typeof commander) =>
15-
program
16-
.option("--ws <ws>", "The websocket URL", "ws://127.0.0.1:9944")
17-
.option("--from <from>", "The starting block, defaults to 0", "0")
18-
.option(
19-
"--append <path>",
20-
"Path to append new remarks to, will auto-detect last block and use it as FROM. Overrides FROM. If file does not exist, it will be created and FROM will default to 0."
21-
)
22-
.option(
23-
"--prefixes <prefixes>",
24-
"Limit remarks to prefix. No default. Can be hex (0x726d726b,0x524d524b) or string (rmrk,RMRK), or combination (rmrk,0x524d524b), separate with comma for multiple",
25-
""
26-
)
27-
.option(
28-
"--to <to>",
29-
"Ending block, defaults to latest on given network",
30-
"latest"
31-
)
32-
.action(async (opts: Options) => {
33-
const api = await getApi(opts.ws);
34-
console.log("Connecting to " + opts.ws);
35-
const append = opts.append;
36-
let from = parseInt(opts.from);
13+
const fetch = async () => {
14+
const args = arg({
15+
// Types
16+
"--ws": String, // Optional websocket url
17+
"--append": String, // Path to append new remarks to, will auto-detect last block and use it as FROM. Overrides FROM. If file does not exist, it will be created and FROM will default to 0.
18+
"--from": Number, // The starting block
19+
"--to": Number, // The starting block
20+
"--prefixes": String, // Limit remarks to prefix. No default. Can be hex (0x726d726b,0x524d524b) or string (rmrk,RMRK), or combination (rmrk,0x524d524b), separate with comma for multiple
21+
});
3722

38-
// Grab FROM from append file
39-
let appendFile = [];
40-
if (append) {
41-
console.log("Will append to " + append);
42-
// eslint-disable-next-line security/detect-non-literal-fs-filename
43-
fs.appendFileSync(append, "");
44-
try {
45-
// eslint-disable-next-line security/detect-non-literal-fs-filename
46-
const fileContent = fs.readFileSync(append).toString();
47-
if (fileContent) {
48-
appendFile = JSON.parse(fileContent);
49-
if (appendFile.length) {
50-
const lastBlock = appendFile.pop();
51-
from = lastBlock.block;
52-
}
53-
}
54-
} catch (e) {
55-
console.error(e);
56-
process.exit(1);
23+
console.log(args)
24+
const ws = args["--ws"] || "ws://127.0.0.1:9944";
25+
const api = await getApi(ws);
26+
const append = args["--append"];
27+
console.log("Connecting to " + ws);
28+
let from = args["--from"] || 0;
29+
30+
// Grab FROM from append file
31+
let appendFile = [];
32+
if (append) {
33+
console.log("Will append to " + append);
34+
// eslint-disable-next-line security/detect-non-literal-fs-filename
35+
fs.appendFileSync(append, "");
36+
try {
37+
// eslint-disable-next-line security/detect-non-literal-fs-filename
38+
const fileContent = fs.readFileSync(append).toString();
39+
if (fileContent) {
40+
appendFile = JSON.parse(fileContent);
41+
if (appendFile.length) {
42+
const lastBlock = appendFile.pop();
43+
from = lastBlock.block;
5744
}
5845
}
46+
} catch (e) {
47+
console.error(e);
48+
process.exit(1);
49+
}
50+
}
5951

60-
const to =
61-
opts.to !== "latest"
62-
? parseInt(opts.to)
63-
: await getLatestFinalizedBlock(api);
52+
const to =
53+
typeof args["--to"] === "number"
54+
? args["--to"]
55+
: await getLatestFinalizedBlock(api);
6456

65-
if (from > to) {
66-
console.error("Starting block must be less than ending block.");
67-
process.exit(1);
68-
}
57+
if (from > to) {
58+
console.error("Starting block must be less than ending block.");
59+
process.exit(1);
60+
}
6961

70-
console.log(`Processing block range from ${from} to ${to}.`);
71-
let extracted = await fetchRemarks(
72-
api,
73-
from,
74-
to,
75-
prefixToArray(opts.prefixes)
76-
);
77-
console.log(deeplog(extracted));
78-
console.log(getRemarksFromBlocks(extracted));
79-
let outputFileName = `remarks-${from}-${to}-${opts.prefixes}.json`;
80-
if (append) {
81-
extracted = appendFile.concat(extracted);
82-
console.log(`Appending ${appendFile.length} remarks found. Full set:`);
83-
console.log(deeplog(extracted));
84-
outputFileName = append;
85-
}
86-
extracted.push({
87-
block: to,
88-
calls: [],
89-
});
90-
// eslint-disable-next-line security/detect-non-literal-fs-filename
91-
fs.writeFileSync(outputFileName, JSON.stringify(extracted));
92-
process.exit(0);
93-
});
62+
console.log(`Processing block range from ${from} to ${to}.`);
63+
let extracted = await fetchRemarks(
64+
api,
65+
from,
66+
to,
67+
prefixToArray(args["--prefixes"] || "")
68+
);
69+
console.log(deeplog(extracted));
70+
console.log(getRemarksFromBlocks(extracted));
71+
let outputFileName = `remarks-${from}-${to}-${args["--prefixes"] || ""}.json`;
72+
if (append) {
73+
extracted = appendFile.concat(extracted);
74+
console.log(`Appending ${appendFile.length} remarks found. Full set:`);
75+
console.log(deeplog(extracted));
76+
outputFileName = append;
77+
}
78+
extracted.push({
79+
block: to,
80+
calls: [],
81+
});
82+
// eslint-disable-next-line security/detect-non-literal-fs-filename
83+
fs.writeFileSync(outputFileName, JSON.stringify(extracted));
84+
process.exit(0);
85+
};
86+
87+
fetch();

cli/getevents.ts

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
#! /usr/bin/env node
2-
import commander from "commander";
3-
import { Options } from "../src/tools/types";
42
import { deeplog, getApi } from "../src/tools/utils";
3+
import arg from "arg";
54

6-
export const addTo = (program: commander.CommanderStatic | typeof commander) =>
7-
program
8-
.command("getevents")
9-
.option("--ws <ws>", "The websocket URL", "ws://127.0.0.1:9944")
10-
.option(
11-
"--blocks <blocks>",
12-
"Blocks to extract events from, comma separated"
13-
)
14-
.action(async (opts: Options) => {
15-
const api = await getApi(opts.ws);
16-
console.log("Connecting to " + opts.ws);
17-
const blocks = opts.blocks.split(",").map(parseInt);
18-
console.log(`Processing blocks: ` + blocks.toString());
19-
for (const blockNum of blocks) {
20-
console.log(`========== Block ${blockNum} =============`);
21-
const blockHash = await api.rpc.chain.getBlockHash(blockNum);
22-
const events = await api.query.system.events.at(blockHash);
23-
const block = await api.rpc.chain.getBlock(blockHash);
24-
if (block.block === undefined) {
25-
console.error("Block is undefined for block " + blockHash);
26-
continue;
27-
}
28-
console.log(`Found ${events.length} events`);
29-
console.log(`Found ${block.block.extrinsics.length} extrincics`);
30-
for (const e of events) {
31-
console.log(`~~~~ Event ${e.event.method.toString()} ~~~~`);
32-
deeplog(e.toHuman());
33-
deeplog(e.event.meta.toHuman());
34-
console.log(e.event.section.toString());
35-
console.log(e.event.method.toString());
36-
console.log(`~~~~ Event ${e.event.method.toString()} END ~~~~`);
37-
}
38-
let index = 0;
39-
for (const ex of block.block.extrinsics) {
40-
console.log(`=== Extrinsic ${blockNum}-${index} =============`);
41-
deeplog(ex.toHuman());
42-
console.log(`=== Extrinsic ${blockNum}-${index} END =============`);
43-
index++;
44-
}
45-
console.log(`========== Block ${blockNum} END =============`);
46-
}
47-
process.exit(0);
48-
});
5+
const getEvents = async () => {
6+
const args = arg({
7+
// Types
8+
"--ws": String, // The websocket URL
9+
"--blocks": String, // Blocks to extract events from, comma separated
10+
});
11+
12+
const api = await getApi(args["--ws"] || "ws://127.0.0.1:9944");
13+
console.log("Connecting to " + args["--ws"]);
14+
const blocks = (args["--blocks"] || "").split(",").map(parseInt);
15+
console.log(`Processing blocks: ` + blocks.toString());
16+
for (const blockNum of blocks) {
17+
console.log(`========== Block ${blockNum} =============`);
18+
const blockHash = await api.rpc.chain.getBlockHash(blockNum);
19+
const events = await api.query.system.events.at(blockHash);
20+
const block = await api.rpc.chain.getBlock(blockHash);
21+
if (block.block === undefined) {
22+
console.error("Block is undefined for block " + blockHash);
23+
continue;
24+
}
25+
console.log(`Found ${events.length} events`);
26+
console.log(`Found ${block.block.extrinsics.length} extrincics`);
27+
for (const e of events) {
28+
console.log(`~~~~ Event ${e.event.method.toString()} ~~~~`);
29+
deeplog(e.toHuman());
30+
deeplog(e.event.meta.toHuman());
31+
console.log(e.event.section.toString());
32+
console.log(e.event.method.toString());
33+
console.log(`~~~~ Event ${e.event.method.toString()} END ~~~~`);
34+
}
35+
let index = 0;
36+
for (const ex of block.block.extrinsics) {
37+
console.log(`=== Extrinsic ${blockNum}-${index} =============`);
38+
deeplog(ex.toHuman());
39+
console.log(`=== Extrinsic ${blockNum}-${index} END =============`);
40+
index++;
41+
}
42+
console.log(`========== Block ${blockNum} END =============`);
43+
}
44+
process.exit(0);
45+
};
46+
47+
getEvents();

cli/index.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

cli/seed.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
#! /usr/bin/env node
2-
import commander from "commander";
3-
import { Options } from "../src/tools/types";
42
import { getApi } from "../src/tools/utils";
53
import { Seeder } from "../test/seed/seeder";
4+
import arg from "arg";
65

7-
export const addTo = (program: commander.CommanderStatic | typeof commander) =>
8-
program
9-
.command("seed")
10-
.option(
11-
"--folder <folder>",
12-
"The folder from which to read seeds",
13-
"default"
14-
)
15-
.action(async (opts: Options) => {
16-
let folder = opts.folder;
17-
if (!folder.startsWith("test/seed")) folder = "test/seed/" + folder;
18-
console.log("Connecting to local chain...");
19-
const api = await getApi("ws://127.0.0.1:9944");
20-
console.log("Connected.");
21-
console.log("Looking for seed files inside " + folder);
22-
const s = new Seeder(api);
23-
await s.seedFromFolder(folder);
24-
process.exit(0);
25-
});
6+
const seed = async () => {
7+
const args = arg({
8+
// Types
9+
"--folder": String, // The folder from which to read seeds
10+
});
11+
12+
let folder = args["--folder"] || "default";
13+
if (!folder.startsWith("test/seed")) folder = "test/seed/" + folder;
14+
console.log("Connecting to local chain...");
15+
const api = await getApi("ws://127.0.0.1:9944");
16+
console.log("Connected.");
17+
console.log("Looking for seed files inside " + folder);
18+
const s = new Seeder(api);
19+
await s.seedFromFolder(folder);
20+
process.exit(0);
21+
};
22+
23+
seed();

0 commit comments

Comments
 (0)