From ce1f0f7f1f1c2bf4aaddf35c5da228b1cb341671 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 3 Aug 2020 10:33:50 +0200 Subject: [PATCH] Add support for HTTPs URLs in --init --- CHANGELOG.md | 1 + packages/cli/src/cli.ts | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f054fab1..0726f44c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## HEAD +- @cosmjs/cli: Now supports HTTPs URLs for `--init` code sources. - @cosmjs/cosmwasm: Rename `CosmWasmClient.getNonce` method to `.getSequence`. - @cosmjs/cosmwasm: Remove `RestClient` class in favour of new modular `LcdClient` class from @cosmjs/sdk38. diff --git a/packages/cli/src/cli.ts b/packages/cli/src/cli.ts index adc81a25..22cd44d0 100644 --- a/packages/cli/src/cli.ts +++ b/packages/cli/src/cli.ts @@ -1,3 +1,4 @@ +import axios from "axios"; import * as fs from "fs"; import { join } from "path"; import yargs from "yargs"; @@ -6,12 +7,13 @@ import { TsRepl } from "./tsrepl"; import colors = require("colors/safe"); -export function main(originalArgs: readonly string[]): void { +export async function main(originalArgs: readonly string[]): Promise { const args = yargs .options({ // User options (we get --help and --version for free) init: { - describe: "Read initial TypeScript code from files", + describe: + "Read initial TypeScript code from the given sources. This can be URLs (supported schemes: https) or local file paths.", type: "array", }, code: { @@ -161,9 +163,14 @@ export function main(originalArgs: readonly string[]): void { } if (args.init) { - for (const path of args.init.map((arg) => arg.toString())) { - if (args.debug) console.info(`Adding file: '${path}' ...`); - init += fs.readFileSync(path, "utf8") + "\n"; + for (const source of args.init.map((arg) => arg.toString())) { + if (args.debug) console.info(`Adding code from: '${source}' ...`); + if (source.startsWith("https://")) { + const response = await axios.get(source); + init += response.data + "\n"; + } else { + init += fs.readFileSync(source, "utf8") + "\n"; + } } }