From 103c6abbdc99d63af6d2c77908c35f47acf242f4 Mon Sep 17 00:00:00 2001 From: Jaga Santagostino Date: Thu, 23 Feb 2017 09:42:17 +0100 Subject: [PATCH] Implemented `now-browse` (#339) --- bin/now-browse.js | 138 ++++++++++++++++++++++++++++++++++++++++++++++ bin/now.js | 3 +- package.json | 1 + 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 bin/now-browse.js diff --git a/bin/now-browse.js b/bin/now-browse.js new file mode 100644 index 0000000..72895ea --- /dev/null +++ b/bin/now-browse.js @@ -0,0 +1,138 @@ +#!/usr/bin/env node + +// Packages +const fs = require('fs-promise') +const minimist = require('minimist') +const chalk = require('chalk') +const opn = require('opn') + +// Ours +const Now = require('../lib') +const login = require('../lib/login') +const cfg = require('../lib/cfg') +const {handleError, error} = require('../lib/error') + +const argv = minimist(process.argv.slice(2), { + string: ['config', 'token'], + boolean: ['help', 'debug'], + alias: { + help: 'h', + config: 'c', + debug: 'd', + token: 't' + } +}) + +const help = () => { + console.log(` + ${chalk.bold('𝚫 now browse')} + + ${chalk.dim('Options:')} + + -h, --help Output usage information + -c ${chalk.bold.underline('FILE')}, --config=${chalk.bold.underline('FILE')} Config file + -d, --debug Debug mode [off] + -t ${chalk.bold.underline('TOKEN')}, --token=${chalk.bold.underline('TOKEN')} Login token + + ${chalk.dim('Examples:')} + + ${chalk.gray('–')} Open latest deployment for current app + + ${chalk.cyan('$ now browse')} + +`) +} + +if (argv.help) { + help() + process.exit(0) +} + +const app = argv._[0] + +// options +const debug = argv.debug +const apiUrl = argv.url || 'https://api.zeit.co' + +if (argv.config) { + cfg.setConfigFile(argv.config) +} + +const config = cfg.read() + +Promise.resolve(argv.token || config.token || login(apiUrl)) + .then(async token => { + try { + await browse(token) + } catch (err) { + error(`Unknown error: ${err}\n${err.stack}`) + process.exit(1) + } + }) + .catch(e => { + error(`Authentication error – ${e.message}`) + process.exit(1) + }) + +async function browse(token) { + const now = new Now(apiUrl, token, {debug}) + + let deployments + try { + deployments = await now.list(app) + } catch (err) { + handleError(err) + process.exit(1) + } + + now.close() + + const apps = new Map() + + for (const dep of deployments) { + const deps = apps.get(dep.name) || [] + apps.set(dep.name, deps.concat(dep)) + } + + let pkg + try { + const json = await fs.readFile('package.json') + pkg = JSON.parse(json) + } catch (err) { + pkg = {} + } + + const [currentProjectDeployments] = await getCurrentProjectDeployments([...apps], pkg) + + if (typeof currentProjectDeployments === 'undefined') { + console.log(`no deployments found for ${chalk.bold(pkg.name)}`) + process.exit(0) + } + + const sorted = await sortByCreation([...currentProjectDeployments]) + const latestDeploy = sorted[0] + + try { + const url = `https://${latestDeploy.url}` + console.log(`opening your latest deployment url for ${chalk.bold(pkg.name)} in browser - ${chalk.underline(url)}`) + + opn(url) + process.exit(0) + } catch (err) { + error(`Unknown error: ${err}\n${err.stack}`) + process.exit(1) + } +} + +async function getCurrentProjectDeployments(apps, pkg) { + return apps + .filter(app => pkg.name === app[0]) + .map(app => app[1]) +} + +async function sortByCreation(deps) { + return deps + .sort((depA, depB) => { + return depB.created - depA.created + }) +} diff --git a/bin/now.js b/bin/now.js index d108dc8..70e7b01 100755 --- a/bin/now.js +++ b/bin/now.js @@ -54,7 +54,8 @@ const commands = new Set([ 'cc', 'billing', 'upgrade', - 'downgrade' + 'downgrade', + 'browse' ]) const aliases = new Map([ diff --git a/package.json b/package.json index f9b946a..fda5306 100644 --- a/package.json +++ b/package.json @@ -81,6 +81,7 @@ "ms": "0.7.2", "node-fetch": "1.6.3", "node-version": "1.0.0", + "opn": "4.0.2", "ora": "1.1.0", "progress": "1.1.8", "psl": "1.1.16",