From 25d22e91117577d835fdfff5e080e49aa2876d01 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Wed, 24 Feb 2016 14:01:59 -0800 Subject: [PATCH 1/2] incorporate login --- bin/now | 66 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/bin/now b/bin/now index 5d48baf..6a8fa1d 100755 --- a/bin/now +++ b/bin/now @@ -2,11 +2,15 @@ import program from 'commander'; import { resolve } from 'path'; import { copy } from 'copy-paste'; +import login from '../lib/login'; import now from '../lib'; +import fs from 'fs'; import ms from 'ms'; +import os from 'os'; program .option('-d, --debug', 'Debug mode [off]', false) +.option('-L, --login', 'Configure login') .option('-C, --no-clipboard', 'Do not attempt to copy URL to clipboard') .parse(process.argv); @@ -20,25 +24,53 @@ if (path) { path = process.cwd(); } -const debug = !!program.debug; -const clipboard = !program.noClipboard; -const start = Date.now(); +let config; -console.log(`> Deploying ${path}`); +try { + config = fs.readFileSync(resolve(os.homedir(), '.now.json'), 'utf8'); + config = JSON.parse(config); +} catch (err) { + login() + .then(() => { + if (program.L) { + console.log('> Logged in successfully. Token saved in ~/.now.json'); + process.exit(0); + } else { + sync(); + } + }) + .catch((e) => { + error(`Authentication error – ${e.message}`); + }); +} -now(path, { debug }).then((url) => { - const elapsed = ms(new Date() - start); - if (clipboard) { - copy(url, (err) => { - console.log(`> ${url} ${!err ? '(copied to clipboard)' : ''} [${elapsed}]`); - }); - } else { - console.log(`> ${url} [${elapsed}]`); - } -}, (err) => { - error(err.message); - process.exit(1); -}); +const sync = () => { + const debug = !!program.debug; + const clipboard = !program.noClipboard; + const start = Date.now(); + + console.log(`> Deploying ${path}`); + + now(path, { debug }) + .then((url) => { + const elapsed = ms(new Date() - start); + if (clipboard) { + copy(url, (err) => { + console.log(`> ${url} ${!err ? '(copied to clipboard)' : ''} [${elapsed}]`); + }); + } else { + console.log(`> ${url} [${elapsed}]`); + } + }, (err) => { + if (403 === err.statusCode) { + error('Authorization error. Run `now -L` to log-in again.'); + } else { + error(err.message); + } + + process.exit(1); + }); +}; function error (err) { console.error(`> \u001b[31mError!\u001b[39m ${err}.`); From bccfce021cd29d43f6482d89955243de23f3da4d Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Wed, 24 Feb 2016 14:02:26 -0800 Subject: [PATCH 2/2] inquirer --- lib/login.js | 9 +++++++++ package.json | 1 + 2 files changed, 10 insertions(+) create mode 100644 lib/login.js diff --git a/lib/login.js b/lib/login.js new file mode 100644 index 0000000..5208e83 --- /dev/null +++ b/lib/login.js @@ -0,0 +1,9 @@ +import { prompt } from 'inquirer'; + +export default function login () { + return new Promise((resolve, reject) => { + prompt([{ name: 'email', message: 'Enter your email' }], (data) => { + + }); + }); +} diff --git a/package.json b/package.json index c14833a..29ff063 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "commander": "2.9.0", "copy-paste": "1.1.4", "fs-promise": "0.4.1", + "inquirer": "0.12.0", "ms": "0.7.1", "node-fetch": "1.3.3", "spdy": "3.2.0"