Skip to content

Commit

Permalink
fixes login and refactors some of the work done prior (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmikey authored Sep 9, 2022
1 parent 164a7f8 commit feed45c
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/commands/function/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getToken } from "../../store/db";
import axios from "axios";
import { IDeploymentOptions } from "./interfaces";
import { run as runPublish } from "./publish";
import { getConsoleServer } from "../../lib/utils";
import { getConsoleServer } from "../../lib/urls";
import { basename, resolve } from "path";

const deploymentOptions: IDeploymentOptions = {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/function/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Chalk from "chalk";
import { store } from "../../store";
import { execSync } from "child_process";
import replace from "replace-in-file";
import { getNpmConfigInitVersion } from "../../lib/utils";
import { getNpmConfigInitVersion } from "../../lib/npm";
import prompt from "prompt";
import {
uniqueNamesGenerator,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/function/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import Table from "cli-table";
import { getToken } from "../../store/db";
import { getConsoleServer } from "../../lib/utils";
import { getConsoleServer } from "../../lib/urls";

export const run = (options: any) => {
const token = getToken();
Expand Down
2 changes: 1 addition & 1 deletion src/commands/function/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IDeploymentOptions } from "./interfaces";
import { run as runBuild } from "./build";
import { createWasmArchive, getBuildDir } from "./shared";
import { basename, resolve } from "path";
import { getWASMRepoServer } from "../../lib/utils";
import { getWASMRepoServer } from "../../lib/urls";

const deploymentOptions: IDeploymentOptions = {
functionId: "",
Expand Down
6 changes: 3 additions & 3 deletions src/commands/login/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Fastify from "fastify";
import { getDb } from "../../store/db";
import { getConsoleServer } from "../../lib/utils";
import { getConsoleServer } from "../../lib/urls";

const portastic = require("portastic");

Expand Down Expand Up @@ -103,12 +103,12 @@ const start = async () => {
fastify.get("/", async (request, reply) => {
console.log(`Sending user to ${consoleServer} to authenticate`);
reply.redirect(
`${consoleServer}/login?returnUrl=${localConsoleServer}/token&clientId=${clientId}`
`${consoleServer}/login?returnUrl=http://127.0.0.1:${port}/token&clientId=${clientId}`
);
});

fastify.listen({ port }).then(() => {
console.log(`Open Browser at ${localConsoleServer} to complete login`);
console.log(`Open Browser at http://127.0.0.1:${port} to complete login`);
});
} catch (err) {
fastify.log.error(err);
Expand Down
80 changes: 29 additions & 51 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import args from "args";
import Chalk from "chalk";
import { store, set as storeSet } from "./store";
import open from "open";

import * as wallet from "./commands/wallet";
Expand All @@ -15,43 +13,15 @@ import {
runUpdate,
runBuild,
} from "./commands/function";
import { IBlsFunctionRequiredOptions } from "./commands/function/interfaces";
import { run as runLogin } from "./commands/login";
import { run as runInfo } from "./commands/info";

let didRun = false;
let pkg = { version: "0.0.0" };
const name = "bls";
const consoleHost = "https://localhost:3005";

function printHelp(commands: any = [], options: any = []) {
console.log("");
console.log(
` Usage: ${Chalk.yellow(name)} ${Chalk.green("[command]")} ${Chalk.blue(
"[subcommand]"
)} [--options]`
);
console.log("");
console.log(" Commands:");
console.log("");
for (const command of commands) {
console.log(` ${Chalk.green(command[0])} ${command[1]}`);
}

console.log("");
if (options.length > 0) console.log(" Options:");
console.log("");
for (const option of options) {
console.log(` ${Chalk.yellow(option[0])} ${option[1]}`);
}
import { printHelp } from "./lib/help";
import { IBlsFunctionRequiredOptions } from "./commands/function/interfaces";
import { getConsoleServer } from "./lib/urls";
import { NAME } from "./store/constants";

console.log("");
console.log(
` ${Chalk.yellow("Blockless CLI")} ${store.system.platform}/${
store.system.arch
}-${pkg.version}`
);
}
let didRun = false;
let pkg: any;

args
.options([
Expand All @@ -77,7 +47,7 @@ args
async (name: string, sub: string[], options: any) => {
didRun = true;
if (!sub[0] || sub[0] === "help") {
printHelp([["install", "install the off-chain agent"]]);
printHelp([["install", "install the off-chain agent"]], null, { pkg });
return;
}
switch (sub[0]) {
Expand Down Expand Up @@ -116,19 +86,26 @@ args

didRun = true;
if (!sub[0] || sub[0] === "help") {
printHelp([
[
"init\t",
"Initialize a new function with blockless starter template.",
],
["deploy\t", "Deploy a function on Blockless."],
printHelp(
[
"list\t",
"Retrieve a list of funtions deployed at Blockless Console.",
[
"init\t",
"Initialize a new function with blockless starter template.",
],
["deploy\t", "Deploy a function on Blockless."],
[
"list\t",
"Retrieve a list of funtions deployed at Blockless Console.",
],
[
"invoke\t",
"Invokes the function at the current (cwd) directory.",
],
["update\t", "Update an existing function on Blockless."],
],
["invoke\t", "Invokes the function at the current (cwd) directory."],
["update\t", "Update an existing function on Blockless."],
]);
null,
{ pkg }
);
return;
}

Expand All @@ -141,7 +118,7 @@ args
// open url in default browser
.command("console", "Open the Blockless console in browser", () => {
didRun = true;
open(consoleHost);
open(getConsoleServer());
})

// check to see info about the user logged in
Expand Down Expand Up @@ -187,14 +164,15 @@ const help = () => {
["function", " Manages your functions"],
["help\t", " Shows the usage information"],
],
[["--yes\t", " Skip questions using default values"]]
[["--yes\t", " Skip questions using default values"]],
{ pkg }
);
};

export async function cli(argv: any, packageJson: any) {
pkg = packageJson;
const flags = args.parse(process.argv, {
name: name,
name: NAME,
version: false,
help: false,
} as any);
Expand Down
32 changes: 32 additions & 0 deletions src/lib/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Chalk from "chalk";
import { NAME } from "../store/constants";
import { store } from "../store";

export function printHelp(commands: any = [], options: any = [], { pkg }: any) {
console.log("");
console.log(
` Usage: ${Chalk.yellow(NAME)} ${Chalk.green("[command]")} ${Chalk.blue(
"[subcommand]"
)} [--options]`
);
console.log("");
console.log(" Commands:");
console.log("");
for (const command of commands) {
console.log(` ${Chalk.green(command[0])} ${command[1]}`);
}

console.log("");
if (options.length > 0) console.log(" Options:");
console.log("");
for (const option of options) {
console.log(` ${Chalk.yellow(option[0])} ${option[1]}`);
}

console.log("");
console.log(
` ${Chalk.yellow("Blockless CLI")} ${store.system.platform}/${
store.system.arch
}-${pkg.version}`
);
}
5 changes: 5 additions & 0 deletions src/lib/npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { execSync } from "child_process";

// Node/npm config
export const getNpmConfigInitVersion = (): string =>
execSync("npm config get init-version").toString("utf-8");
6 changes: 0 additions & 6 deletions src/lib/utils.ts → src/lib/urls.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { execSync } from "child_process";

// Console API Server
export const getConsoleServer = (
location: "local" | "remote" = "remote",
Expand All @@ -19,7 +17,3 @@ export const getWASMRepoServer = (
const host = devMode ? "http://0.0.0.0" : "https://wasi.bls.dev";
return `${host}:${port ? port : devMode ? 3006 : 443}`;
};

// Node/npm config
export const getNpmConfigInitVersion = (): string =>
execSync("npm config get init-version").toString("utf-8");
3 changes: 3 additions & 0 deletions src/store/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const NAME = "bls";
export const CONSOLE_HOST_LOCAL = "http://localhost:3005";
export const CONSOLE_HOST_REMOTE = "https://console.bls.dev";

0 comments on commit feed45c

Please sign in to comment.