From 88b9f338aa4b6897558c3bea40dcbf31e8d02d0e Mon Sep 17 00:00:00 2001 From: Bryan Kendall Date: Fri, 14 Jun 2019 10:19:11 -0700 Subject: [PATCH] rewrite open command to typescript --- src/commands/open.js | 117 ------------------------------------------- src/commands/open.ts | 101 +++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 117 deletions(-) delete mode 100644 src/commands/open.js create mode 100644 src/commands/open.ts diff --git a/src/commands/open.js b/src/commands/open.js deleted file mode 100644 index f4046d466b3..00000000000 --- a/src/commands/open.js +++ /dev/null @@ -1,117 +0,0 @@ -"use strict"; - -var _ = require("lodash"); -var clc = require("cli-color"); -var open = require("open"); - -var api = require("../api"); -var Command = require("../command"); -var logger = require("../logger"); -var { prompt } = require("../prompt"); -var requirePermissions = require("../requirePermissions"); -var requireInstance = require("../requireInstance"); -var utils = require("../utils"); - -var LINKS = [ - { name: "Project Dashboard", arg: "dashboard", consoleUrl: "/overview" }, - { name: "Analytics", arg: "analytics", consoleUrl: "/analytics" }, - { name: "Database: Data", arg: "database", consoleUrl: "/database/data" }, - { - name: "Database: Rules", - arg: "database:rules", - consoleUrl: "/database/rules", - }, - { - name: "Authentication: Providers", - arg: "auth", - consoleUrl: "/authentication/providers", - }, - { - name: "Authentication: Users", - arg: "auth:users", - consoleUrl: "/authentication/users", - }, - { name: "Storage: Files", arg: "storage", consoleUrl: "/storage/files" }, - { - name: "Storage: Rules", - arg: "storage:rules", - consoleUrl: "/storage/rules", - }, - { name: "Hosting", arg: "hosting", consoleUrl: "/hosting/main" }, - { name: "Hosting: Deployed Site", arg: "hosting:site" }, - { name: "Remote Config", arg: "config", consoleUrl: "/config" }, - { - name: "Remote Config: Conditions", - arg: "config:conditions", - consoleUrl: "/config/conditions", - }, - { name: "Test Lab", arg: "testlab", consoleUrl: "/testlab/histories/" }, - { name: "Crash Reporting", arg: "crash", consoleUrl: "/monitoring" }, - { name: "Notifications", arg: "notifications", consoleUrl: "/notification" }, - { name: "Dynamic Links", arg: "links", consoleUrl: "/durablelinks" }, - { - name: "Project Settings", - arg: "settings", - consoleUrl: "/settings/general", - }, - { name: "Docs", arg: "docs", url: "https://firebase.google.com/docs" }, -]; - -var CHOICES = _.map(LINKS, "name"); - -module.exports = new Command("open [link]") - .description("quickly open a browser to relevant project resources") - .before(requirePermissions) - .before(requireInstance) - .action(function(linkName, options) { - var link = _.find(LINKS, { arg: linkName }); - if (linkName && !link) { - return utils.reject( - "Unrecognized link name. Valid links are:\n\n" + _.map(LINKS, "arg").join("\n") - ); - } - - var next = Promise.resolve(link); - if (!link) { - next = prompt - .once({ - type: "list", - message: "What link would you like to open?", - choices: CHOICES, - }) - .then(function(result) { - return _.find(LINKS, { name: result }); - }); - } - - return next.then(function(finalLink) { - var url; - if (finalLink.consoleUrl) { - url = utils.consoleUrl(options.project, finalLink.consoleUrl); - } else if (finalLink.url) { - url = finalLink.url; - } else if (finalLink.arg === "hosting:site") { - url = utils.addSubdomain(api.hostingOrigin, options.instance); - } else if (finalLink.arg === "functions") { - url = "https://console.firebase.google.com/project/" + options.project + "/functions/list"; - } else if (finalLink.arg === "functions:log") { - url = - "https://console.developers.google.com/logs/viewer?resource=cloudfunctions.googleapis.com&project=" + - options.project; - } - - if (finalLink.arg !== linkName) { - logger.info( - clc.bold.cyan("Tip: ") + - "You can also run " + - clc.bold.underline("firebase open " + finalLink.arg) - ); - logger.info(); - } - logger.info("Opening " + clc.bold(finalLink.name) + " link in your default browser:"); - logger.info(clc.bold.underline(url)); - - open(url); - return Promise.resolve(url); - }); - }); diff --git a/src/commands/open.ts b/src/commands/open.ts new file mode 100644 index 00000000000..2a9a60a62a7 --- /dev/null +++ b/src/commands/open.ts @@ -0,0 +1,101 @@ +import * as _ from "lodash"; +import * as clc from "cli-color"; +import * as open from "open"; + +import * as FirebaseError from "../error"; +import * as api from "../api"; +import * as Command from "../command"; +import * as logger from "../logger"; +import { promptOnce } from "../prompt"; +import * as requirePermissions from "../requirePermissions"; +import * as requireInstance from "../requireInstance"; +import * as utils from "../utils"; + +interface Link { + name: string; + arg: string; + consoleUrl?: string; + url?: string; +} + +const LINKS: Link[] = [ + { name: "Analytics", arg: "analytics", consoleUrl: "/analytics" }, + { name: "Authentication: Providers", arg: "auth", consoleUrl: "/authentication/providers" }, + { name: "Authentication: Users", arg: "auth:users", consoleUrl: "/authentication/users" }, + { name: "Crash Reporting", arg: "crash", consoleUrl: "/monitoring" }, + { name: "Database: Data", arg: "database", consoleUrl: "/database/data" }, + { name: "Database: Rules", arg: "database:rules", consoleUrl: "/database/rules" }, + { name: "Docs", arg: "docs", url: "https://firebase.google.com/docs" }, + { name: "Dynamic Links", arg: "links", consoleUrl: "/durablelinks" }, + { name: "Hosting: Deployed Site", arg: "hosting:site" }, + { name: "Hosting", arg: "hosting", consoleUrl: "/hosting/main" }, + { name: "Notifications", arg: "notifications", consoleUrl: "/notification" }, + { name: "Project Dashboard", arg: "dashboard", consoleUrl: "/overview" }, + { name: "Project Settings", arg: "settings", consoleUrl: "/settings/general" }, + { name: "Remote Config: Conditions", arg: "config:conditions", consoleUrl: "/config/conditions" }, + { name: "Remote Config", arg: "config", consoleUrl: "/config" }, + { name: "Storage: Files", arg: "storage", consoleUrl: "/storage/files" }, + { name: "Storage: Rules", arg: "storage:rules", consoleUrl: "/storage/rules" }, + { name: "Test Lab", arg: "testlab", consoleUrl: "/testlab/histories/" }, +]; + +const CHOICES = _.map(LINKS, "name"); + +export default new Command("open [link]") + .description("quickly open a browser to relevant project resources") + .before(requirePermissions) + .before(requireInstance) + .action( + async (linkName: string, options: any): Promise => { + let link = _.find(LINKS, { arg: linkName }); + if (linkName && !link) { + throw new FirebaseError( + "Unrecognized link name. Valid links are:\n\n" + _.map(LINKS, "arg").join("\n") + ); + } + + if (!link) { + const name = await promptOnce({ + type: "list", + message: "What link would you like to open?", + choices: CHOICES, + }); + link = _.find(LINKS, { name }); + } + if (!link) { + throw new FirebaseError( + "Unrecognized link name. Valid links are:\n\n" + _.map(LINKS, "arg").join("\n") + ); + } + + let url; + if (link.consoleUrl) { + url = utils.consoleUrl(options.project, link.consoleUrl); + } else if (link.url) { + url = link.url; + } else if (link.arg === "hosting:site") { + url = utils.addSubdomain(api.hostingOrigin, options.instance); + } else if (link.arg === "functions") { + url = "https://console.firebase.google.com/project/" + options.project + "/functions/list"; + } else if (link.arg === "functions:log") { + url = + "https://console.developers.google.com/logs/viewer?resource=cloudfunctions.googleapis.com&project=" + + options.project; + } else { + throw new FirebaseError(`Unable to determine URL for link: ${link}`); + } + + if (link.arg !== linkName) { + logger.info( + `${clc.bold.cyan("Tip:")} You can also run ${clc.bold.underline( + `firebase open ${link.arg}` + )}` + ); + logger.info(); + } + logger.info(`Opening ${clc.bold(link.name)} link in your default browser:`); + logger.info(clc.bold.underline(url)); + + open(url); + } + );