Skip to content

Commit

Permalink
Merge pull request #31 from Yuripetusko/master
Browse files Browse the repository at this point in the history
Don't use commander for cli tools as it doesn't work through package.json bin
  • Loading branch information
Yuripetusko authored Mar 17, 2021
2 parents 6974645 + b9f4095 commit 527c560
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 225 deletions.
49 changes: 24 additions & 25 deletions cli/consolidate.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
#! /usr/bin/env node
import commander from "commander";
import { Options } from "../src/tools/types";
import fs from "fs";
import JsonAdapter from "../src/tools/consolidator/adapters/json";
import { Consolidator } from "../src/tools/consolidator/consolidator";
import arg from "arg";

export const addTo = (program: commander.CommanderStatic | typeof commander) =>
program
.command("consolidate")
.option("--json <json>", "The JSON file from which to consolidate")
.action(async (opts: Options) => {
const file = opts.json;
if (!file) {
console.error("File path must be provided");
process.exit(1);
}
// Check the JSON file exists and is reachable
try {
fs.accessSync(file, fs.constants.R_OK);
} catch (e) {
console.error(
"File is not readable. Are you providing the right path?"
);
process.exit(1);
}
const ja = new JsonAdapter(file);
const con = new Consolidator(ja);
con.consolidate();
});
const consolidate = async () => {
const args = arg({
"--json": String, // The JSON file from which to consolidate
});

const file = args["--json"];
if (!file) {
console.error("File path must be provided");
process.exit(1);
}
// Check the JSON file exists and is reachable
try {
fs.accessSync(file, fs.constants.R_OK);
} catch (e) {
console.error("File is not readable. Are you providing the right path?");
process.exit(1);
}
const ja = new JsonAdapter(file);
const con = new Consolidator(ja);
con.consolidate();
};

consolidate();
146 changes: 70 additions & 76 deletions cli/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#! /usr/bin/env node
import { Options } from "../src/tools/types";
import {
deeplog,
getApi,
Expand All @@ -9,85 +8,80 @@ import {
} from "../src/tools/utils";
import fs from "fs";
import fetchRemarks from "../src/tools/fetchRemarks";
import commander from "commander";
import arg from "arg";

export const addTo = (program: commander.CommanderStatic | typeof commander) =>
program
.option("--ws <ws>", "The websocket URL", "ws://127.0.0.1:9944")
.option("--from <from>", "The starting block, defaults to 0", "0")
.option(
"--append <path>",
"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."
)
.option(
"--prefixes <prefixes>",
"Limit remarks to prefix. No default. Can be hex (0x726d726b,0x524d524b) or string (rmrk,RMRK), or combination (rmrk,0x524d524b), separate with comma for multiple",
""
)
.option(
"--to <to>",
"Ending block, defaults to latest on given network",
"latest"
)
.action(async (opts: Options) => {
const api = await getApi(opts.ws);
console.log("Connecting to " + opts.ws);
const append = opts.append;
let from = parseInt(opts.from);
const fetch = async () => {
const args = arg({
// Types
"--ws": String, // Optional websocket url
"--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.
"--from": Number, // The starting block
"--to": Number, // The starting block
"--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
});

// Grab FROM from append file
let appendFile = [];
if (append) {
console.log("Will append to " + append);
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.appendFileSync(append, "");
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const fileContent = fs.readFileSync(append).toString();
if (fileContent) {
appendFile = JSON.parse(fileContent);
if (appendFile.length) {
const lastBlock = appendFile.pop();
from = lastBlock.block;
}
}
} catch (e) {
console.error(e);
process.exit(1);
console.log(args)
const ws = args["--ws"] || "ws://127.0.0.1:9944";
const api = await getApi(ws);
const append = args["--append"];
console.log("Connecting to " + ws);
let from = args["--from"] || 0;

// Grab FROM from append file
let appendFile = [];
if (append) {
console.log("Will append to " + append);
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.appendFileSync(append, "");
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const fileContent = fs.readFileSync(append).toString();
if (fileContent) {
appendFile = JSON.parse(fileContent);
if (appendFile.length) {
const lastBlock = appendFile.pop();
from = lastBlock.block;
}
}
} catch (e) {
console.error(e);
process.exit(1);
}
}

const to =
opts.to !== "latest"
? parseInt(opts.to)
: await getLatestFinalizedBlock(api);
const to =
typeof args["--to"] === "number"
? args["--to"]
: await getLatestFinalizedBlock(api);

if (from > to) {
console.error("Starting block must be less than ending block.");
process.exit(1);
}
if (from > to) {
console.error("Starting block must be less than ending block.");
process.exit(1);
}

console.log(`Processing block range from ${from} to ${to}.`);
let extracted = await fetchRemarks(
api,
from,
to,
prefixToArray(opts.prefixes)
);
console.log(deeplog(extracted));
console.log(getRemarksFromBlocks(extracted));
let outputFileName = `remarks-${from}-${to}-${opts.prefixes}.json`;
if (append) {
extracted = appendFile.concat(extracted);
console.log(`Appending ${appendFile.length} remarks found. Full set:`);
console.log(deeplog(extracted));
outputFileName = append;
}
extracted.push({
block: to,
calls: [],
});
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.writeFileSync(outputFileName, JSON.stringify(extracted));
process.exit(0);
});
console.log(`Processing block range from ${from} to ${to}.`);
let extracted = await fetchRemarks(
api,
from,
to,
prefixToArray(args["--prefixes"] || "")
);
console.log(deeplog(extracted));
console.log(getRemarksFromBlocks(extracted));
let outputFileName = `remarks-${from}-${to}-${args["--prefixes"] || ""}.json`;
if (append) {
extracted = appendFile.concat(extracted);
console.log(`Appending ${appendFile.length} remarks found. Full set:`);
console.log(deeplog(extracted));
outputFileName = append;
}
extracted.push({
block: to,
calls: [],
});
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.writeFileSync(outputFileName, JSON.stringify(extracted));
process.exit(0);
};

fetch();
89 changes: 44 additions & 45 deletions cli/getevents.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
#! /usr/bin/env node
import commander from "commander";
import { Options } from "../src/tools/types";
import { deeplog, getApi } from "../src/tools/utils";
import arg from "arg";

export const addTo = (program: commander.CommanderStatic | typeof commander) =>
program
.command("getevents")
.option("--ws <ws>", "The websocket URL", "ws://127.0.0.1:9944")
.option(
"--blocks <blocks>",
"Blocks to extract events from, comma separated"
)
.action(async (opts: Options) => {
const api = await getApi(opts.ws);
console.log("Connecting to " + opts.ws);
const blocks = opts.blocks.split(",").map(parseInt);
console.log(`Processing blocks: ` + blocks.toString());
for (const blockNum of blocks) {
console.log(`========== Block ${blockNum} =============`);
const blockHash = await api.rpc.chain.getBlockHash(blockNum);
const events = await api.query.system.events.at(blockHash);
const block = await api.rpc.chain.getBlock(blockHash);
if (block.block === undefined) {
console.error("Block is undefined for block " + blockHash);
continue;
}
console.log(`Found ${events.length} events`);
console.log(`Found ${block.block.extrinsics.length} extrincics`);
for (const e of events) {
console.log(`~~~~ Event ${e.event.method.toString()} ~~~~`);
deeplog(e.toHuman());
deeplog(e.event.meta.toHuman());
console.log(e.event.section.toString());
console.log(e.event.method.toString());
console.log(`~~~~ Event ${e.event.method.toString()} END ~~~~`);
}
let index = 0;
for (const ex of block.block.extrinsics) {
console.log(`=== Extrinsic ${blockNum}-${index} =============`);
deeplog(ex.toHuman());
console.log(`=== Extrinsic ${blockNum}-${index} END =============`);
index++;
}
console.log(`========== Block ${blockNum} END =============`);
}
process.exit(0);
});
const getEvents = async () => {
const args = arg({
// Types
"--ws": String, // The websocket URL
"--blocks": String, // Blocks to extract events from, comma separated
});

const api = await getApi(args["--ws"] || "ws://127.0.0.1:9944");
console.log("Connecting to " + args["--ws"]);
const blocks = (args["--blocks"] || "").split(",").map(parseInt);
console.log(`Processing blocks: ` + blocks.toString());
for (const blockNum of blocks) {
console.log(`========== Block ${blockNum} =============`);
const blockHash = await api.rpc.chain.getBlockHash(blockNum);
const events = await api.query.system.events.at(blockHash);
const block = await api.rpc.chain.getBlock(blockHash);
if (block.block === undefined) {
console.error("Block is undefined for block " + blockHash);
continue;
}
console.log(`Found ${events.length} events`);
console.log(`Found ${block.block.extrinsics.length} extrincics`);
for (const e of events) {
console.log(`~~~~ Event ${e.event.method.toString()} ~~~~`);
deeplog(e.toHuman());
deeplog(e.event.meta.toHuman());
console.log(e.event.section.toString());
console.log(e.event.method.toString());
console.log(`~~~~ Event ${e.event.method.toString()} END ~~~~`);
}
let index = 0;
for (const ex of block.block.extrinsics) {
console.log(`=== Extrinsic ${blockNum}-${index} =============`);
deeplog(ex.toHuman());
console.log(`=== Extrinsic ${blockNum}-${index} END =============`);
index++;
}
console.log(`========== Block ${blockNum} END =============`);
}
process.exit(0);
};

getEvents();
15 changes: 0 additions & 15 deletions cli/index.ts

This file was deleted.

40 changes: 19 additions & 21 deletions cli/seed.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
#! /usr/bin/env node
import commander from "commander";
import { Options } from "../src/tools/types";
import { getApi } from "../src/tools/utils";
import { Seeder } from "../test/seed/seeder";
import arg from "arg";

export const addTo = (program: commander.CommanderStatic | typeof commander) =>
program
.command("seed")
.option(
"--folder <folder>",
"The folder from which to read seeds",
"default"
)
.action(async (opts: Options) => {
let folder = opts.folder;
if (!folder.startsWith("test/seed")) folder = "test/seed/" + folder;
console.log("Connecting to local chain...");
const api = await getApi("ws://127.0.0.1:9944");
console.log("Connected.");
console.log("Looking for seed files inside " + folder);
const s = new Seeder(api);
await s.seedFromFolder(folder);
process.exit(0);
});
const seed = async () => {
const args = arg({
// Types
"--folder": String, // The folder from which to read seeds
});

let folder = args["--folder"] || "default";
if (!folder.startsWith("test/seed")) folder = "test/seed/" + folder;
console.log("Connecting to local chain...");
const api = await getApi("ws://127.0.0.1:9944");
console.log("Connected.");
console.log("Looking for seed files inside " + folder);
const s = new Seeder(api);
await s.seedFromFolder(folder);
process.exit(0);
};

seed();
Loading

0 comments on commit 527c560

Please sign in to comment.