From b9f4095521eff5b1786d86d98af007dd16ce7794 Mon Sep 17 00:00:00 2001 From: Yuri Date: Wed, 17 Mar 2021 17:58:04 +0000 Subject: [PATCH] don't use commander for cli tools --- cli/consolidate.ts | 49 ++++++++------- cli/fetch.ts | 146 ++++++++++++++++++++++----------------------- cli/getevents.ts | 89 ++++++++++++++------------- cli/index.ts | 15 ----- cli/seed.ts | 40 ++++++------- cli/validate.ts | 56 +++++++++-------- package.json | 19 +++--- yarn.lock | 10 ++-- 8 files changed, 199 insertions(+), 225 deletions(-) delete mode 100644 cli/index.ts diff --git a/cli/consolidate.ts b/cli/consolidate.ts index f62b863c..571bb443 100644 --- a/cli/consolidate.ts +++ b/cli/consolidate.ts @@ -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 ", "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(); diff --git a/cli/fetch.ts b/cli/fetch.ts index aa373aed..1c6b9806 100644 --- a/cli/fetch.ts +++ b/cli/fetch.ts @@ -1,5 +1,4 @@ #! /usr/bin/env node -import { Options } from "../src/tools/types"; import { deeplog, getApi, @@ -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 ", "The websocket URL", "ws://127.0.0.1:9944") - .option("--from ", "The starting block, defaults to 0", "0") - .option( - "--append ", - "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 ", - "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 ", - "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(); diff --git a/cli/getevents.ts b/cli/getevents.ts index d2ef7b25..180bd76a 100644 --- a/cli/getevents.ts +++ b/cli/getevents.ts @@ -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 ", "The websocket URL", "ws://127.0.0.1:9944") - .option( - "--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(); diff --git a/cli/index.ts b/cli/index.ts deleted file mode 100644 index 812c65b4..00000000 --- a/cli/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -#! /usr/bin/env node -import program from "commander"; -import * as fetch from "./fetch"; -import * as consolidate from "./consolidate"; -import * as seed from "./seed"; -import * as getevents from "./getevents"; -import * as validate from "./validate"; - -fetch.addTo(program); -consolidate.addTo(program); -seed.addTo(program); -getevents.addTo(program); -validate.addTo(program); - -program.parse(process.argv); diff --git a/cli/seed.ts b/cli/seed.ts index e638ad8d..237ce86c 100644 --- a/cli/seed.ts +++ b/cli/seed.ts @@ -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 ", - "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(); diff --git a/cli/validate.ts b/cli/validate.ts index da742f59..df72f773 100644 --- a/cli/validate.ts +++ b/cli/validate.ts @@ -1,34 +1,32 @@ #! /usr/bin/env node -import commander from "commander"; -import { Options } from "../src/tools/types"; import { NFT as N100 } from "../src/rmrk1.0.0/classes/nft"; import { deeplog } from "../src/tools/utils"; import { OP_TYPES } from "../src/tools/constants"; +import arg from "arg"; -export const addTo = (program: commander.CommanderStatic | typeof commander) => - program - .command("validate") - .option("--remark ", "The remark to validate") - .action(async (opts: Options) => { - const remark = opts.remark; - const exploded = remark.split("::"); - if (exploded.length < 2) { - throw new Error( - "Invalid RMRK remark, cannot explode by double-colon (::)" - ); - } - if (exploded[0].toUpperCase() !== "RMRK") { - throw new Error( - "This is not a RMRK remark - does not begin with RMRK/rmrk" - ); - } - switch (exploded[1]) { - case OP_TYPES.MINTNFT: - console.log(`Identified as ${OP_TYPES.MINTNFT}`); - const n = N100.fromRemark(remark); - deeplog(n); - break; - default: - throw new Error(`${exploded[1]} interaction not supported`); - } - }); +const validate = async () => { + const args = arg({ + // Types + "--remark": String, // The remark to validate + }); + + const remark = args["--remark"] || ""; + const exploded = remark.split("::"); + if (exploded.length < 2) { + throw new Error("Invalid RMRK remark, cannot explode by double-colon (::)"); + } + if (exploded[0].toUpperCase() !== "RMRK") { + throw new Error( + "This is not a RMRK remark - does not begin with RMRK/rmrk" + ); + } + switch (exploded[1]) { + case OP_TYPES.MINTNFT: + console.log(`Identified as ${OP_TYPES.MINTNFT}`); + const n = N100.fromRemark(remark); + deeplog(n); + break; + default: + throw new Error(`${exploded[1]} interaction not supported`); + } +}; diff --git a/package.json b/package.json index 743551ef..2ea4f636 100644 --- a/package.json +++ b/package.json @@ -31,20 +31,21 @@ "clean": "rm -rf build", "lint": "tsc --noEmit && eslint '*/**/*.{js,ts,tsx}' --quiet", "lint:fix": "tsc --noEmit && eslint '*/**/*.{js,ts,tsx}' --quiet --fix", - "cli:fetch": "ts-node --project tsconfig.cli.json cli/index fetch", - "cli:consolidate": "ts-node --project tsconfig.cli.json cli/index consolidate", - "cli:seed": "ts-node --project tsconfig.cli.json cli/index seed", - "cli:getevents": "ts-node --project tsconfig.cli.json cli/index getevents", - "cli:validate": "ts-node --project tsconfig.cli.json src/index validate", + "cli:fetch": "ts-node --project tsconfig.cli.json cli/fetch.ts", + "cli:consolidate": "ts-node --project tsconfig.cli.json cli/consolidate", + "cli:seed": "ts-node --project tsconfig.cli.json cli/seed", + "cli:getevents": "ts-node --project tsconfig.cli.json cli/getevents", + "cli:validate": "ts-node --project tsconfig.cli.json src/validate", "cli:run-listener": "ts-node --project tsconfig.cli.json cli/run-listener.ts", "test": "jest --silent", "test:coverage": "jest --coverage --coverageDirectory='coverage'", "prepublishOnly": "yarn build" }, "bin": { - "rmrk-tools-fetch": "dist-cli/cli/index.js fetch", - "rmrk-tools-consolidate": "dist-cli/cli/index.js consolidate", - "rmrk-tools-seed": "dist-cli/cli/index.js seed" + "rmrk-tools-fetch": "./dist-cli/cli/fetch.js", + "rmrk-tools-consolidate": "./dist-cli/cli/consolidate.js", + "rmrk-tools-seed": "./dist-cli/cli/seed.js", + "rmrk-tools-validate": "./dist-cli/cli/validate.js" }, "author": "Bruno Skvorc ", "license": "GPL-3.0", @@ -65,6 +66,7 @@ "@types/node": "^14.11.2", "@typescript-eslint/eslint-plugin": "^4.2.0", "@typescript-eslint/parser": "^4.2.0", + "arg": "^5.0.0", "babel-jest": "^26.6.3", "eslint": "^7.9.0", "eslint-config-prettier": "^6.11.0", @@ -87,7 +89,6 @@ "@polkadot/keyring": "^6.0.5", "@polkadot/util": "^6.0.5", "chalk": "^4.1.0", - "commander": "^6.1.0", "it-all": "^1.0.5", "node-fetch": "^2.6.1", "superstruct": "^0.14.2", diff --git a/yarn.lock b/yarn.lock index 72907ced..930eec93 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2397,6 +2397,11 @@ arg@^4.1.0: resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA== +arg@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/arg/-/arg-5.0.0.tgz#a20e2bb5710e82950a516b3f933fee5ed478be90" + integrity sha512-4P8Zm2H+BRS+c/xX1LrHw0qKpEhdlZjLCgWy+d78T9vqa2Z2SiD2wMrYuWIAFy5IZUD7nnNXroRttz+0RzlrzQ== + argparse@^1.0.7: version "1.0.10" resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" @@ -3118,11 +3123,6 @@ commander@^2.15.0, commander@^2.20.0: resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ== -commander@^6.1.0: - version "6.2.1" - resolved "https://registry.yarnpkg.com/commander/-/commander-6.2.1.tgz#0792eb682dfbc325999bb2b84fddddba110ac73c" - integrity sha512-U7VdrJFnJgo4xjrHpTzu0yrHPGImdsmD95ZlgYSEajAn2JKzDhDTPG9kBTefmObL2w/ngeZnilk+OV9CG3d7UA== - commander@^7.0.0: version "7.1.0" resolved "https://registry.yarnpkg.com/commander/-/commander-7.1.0.tgz#f2eaecf131f10e36e07d894698226e36ae0eb5ff"