Skip to content

Commit

Permalink
Merge pull request #96 from Bitcoin-com/clean-up
Browse files Browse the repository at this point in the history
Add more types. Clean up interfaces. Increase test coverage
  • Loading branch information
Gabriel Cardona authored May 11, 2019
2 parents e2ab760 + 9f525d0 commit 5c892e8
Show file tree
Hide file tree
Showing 45 changed files with 930 additions and 740 deletions.
11 changes: 0 additions & 11 deletions .flowconfig

This file was deleted.

138 changes: 105 additions & 33 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,93 @@
#!/usr/bin/env node
/// <reference path="./lib/interfaces/vendors.d.ts"/>

require("babel-register")
const path = require("path")
const program = require("commander")
const chalk = require("chalk")
const mkdirp = require("mkdirp")
const figlet = require("figlet")
const clear = require("clear")
const fs = require("fs")
const touch = require("touch")
const emoji = require("node-emoji")
const repl = require("repl")
const BITBOX = require("./lib/BITBOX").BITBOX
const clone = require("git-clone")

program.version("7.0.18 ", "-v, --version")
import * as path from "path"
import * as program from "commander"
import chalk from "chalk";
import * as fs from "fs";
import * as repl from "repl";
import * as mkdirp from "mkdirp";
import * as figlet from "figlet";
import * as touch from "touch";
import * as emoji from "node-emoji";

// TODO: port to `import` statement
let clear = require("clear")
let clone = require("git-clone")

import { BITBOX } from "./lib/BITBOX"
import { Address } from "./lib/Address"
import { BitcoinCash } from "./lib/BitcoinCash"
import { Block } from "./lib/Block"
import { Blockchain } from "./lib/Blockchain"
import { Control } from "./lib/Control"
import { Crypto } from "./lib/Crypto"
import { ECPair } from "./lib/ECPair"
import { Generating } from "./lib/Generating"
import { HDNode } from "./lib/HDNode"
import { Mining } from "./lib/Mining"
import { Mnemonic } from "./lib/Mnemonic"
import { Price } from "./lib/Price"
import { RawTransactions } from "./lib/RawTransactions"
import { Script } from "./lib/Script"
import { Transaction } from "./lib/Transaction"
import { TransactionBuilder } from "./lib/TransactionBuilder"
import { Util } from "./lib/Util"
import { Socket } from "./lib/Socket"
import { Wallet } from "./lib/Wallet"
import { Schnorr } from "./lib/Schnorr"

interface ConsoleOptions {
environment: string
}

interface NewOptions extends ConsoleOptions {
scaffold: string
restURL: string
}

program.version("7.1.0 ", "-v, --version")

program
.command("new <name>")
.option(
"-s, --scaffold <scaffold>",
"The framework to use. Options include react, angular, vuejs, nextjs, node and websockets."
"The framework to use. Options include react, angular, vuejs, nextjs, node and websockets. (Default: react)"
)
.option(
"-r, --restURL <restURL>",
"The rest URL to use. default: https://trest.bitcoin.com/v2/"
"The rest URL to use. (Default: https://trest.bitcoin.com/v2/)"
)
.option(
"-e, --environment <environment>",
"environment of running BITBOX instance. Ex: production, staging. (Default: development)"
)
.description(`create a new BITBOX application`)
.action((name: any, options: any) => {
.action((name: string, options: NewOptions): void => {
// confirm project doesn't already exist
if (fs.existsSync(`./${name}`)) {
console.log(chalk.red(`Project ${name} already exists`))
process.exit(1)
}

let config
// pass in empty config object as it's not needed for new command
const config: {} = {}

// get environment option. default to development if no options.environment
const environment = fetchOption("environment=development", config, options)

// get restURL option. default to TREST if no options.restURL
const restURL = fetchOption(
"restURL=https://trest.bitcoin.com/v2/",
config,
options
)

// scaffold flow
if (options && options.scaffold) {
let scaffold = options.scaffold.toLowerCase()
let repo
const conf = {}
let scaffold: string = options.scaffold.toLowerCase()
let repo: string = ''
if (scaffold === "node") {
repo = "https://github.com/Bitcoin-com/bitbox-scaffold-node.git"
} else if (scaffold === "angular") {
Expand All @@ -65,10 +105,11 @@ program
process.exit(1)
}

if (options && options.repo) {
scaffold = "custom repo"
repo = options.repo.toLowerCase()
}
// TODO: Bring this back when we allow --repo flag to clone random repos
// if (options && options.repo) {
// scaffold = "custom repo"
// repo = options.repo.toLowerCase()
// }

clear()
console.log(
Expand All @@ -80,8 +121,10 @@ program
)
)

// pass in empty conf object
const conf: {} = {}
console.log(chalk.blue(`Scaffolding ${scaffold} app in ${name}`))
clone(repo, `./${name}`, [conf], (res: any) => {
clone(repo, `./${name}`, [conf], (res: string): any => {
if (res === "Error: 'git clone' failed with status 128") {
console.log(chalk.red("Must create new app in to an empty directory"))
} else {
Expand Down Expand Up @@ -138,8 +181,13 @@ program
"environment of running BITBOX instance. Ex: production, staging. (Default: development)"
)
.description("Run a console with Bitcoin Cash RPC commands available")
.action((options: any) => {
let config
.action((options: ConsoleOptions): void => {

// TODO: create interface for `config`
let config: any = {
environments: {}
}

try {
config = require(`${process.cwd()}/bitbox.js`).config
} catch (err) {
Expand All @@ -157,10 +205,12 @@ program
replServer.context.bitbox = new BITBOX(config.environments[environment])
})

function fetchOption(kv: any, config: any, options: any) {
const parts = kv.split("=")
const key = parts[0]
const defaultVal = parts[1]

function fetchOption(kv: string, config: any, options: ConsoleOptions | NewOptions | any): string {
// TODO: remove `any` type from `options` argument
const parts: string[] = kv.split("=")
const key: string = parts[0]
const defaultVal: string = parts[1]
if (options && options[key]) return options[key]
else if (config && config.new && config.new[key]) return config.new[key]

Expand All @@ -172,4 +222,26 @@ program.parse(process.argv)
// print help if no command given
// if (!process.argv.slice(2).length) program.outputHelp()

module.exports = BITBOX
export {
Address,
BITBOX,
BitcoinCash,
Block,
Blockchain,
Control,
Crypto,
ECPair,
Generating,
HDNode,
Mining,
Mnemonic,
Price,
RawTransactions,
Script,
Transaction,
TransactionBuilder,
Util,
Socket,
Wallet,
Schnorr
}
Loading

0 comments on commit 5c892e8

Please sign in to comment.