From de932b8643fe7ae85530352e4d33ff1ba579d651 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Tue, 5 Jul 2016 16:16:01 -0700 Subject: [PATCH 1/6] now-deploy: stay quiet on non-tty --- bin/now-deploy | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/bin/now-deploy b/bin/now-deploy index 22174ff..9b63ed5 100755 --- a/bin/now-deploy +++ b/bin/now-deploy @@ -130,6 +130,8 @@ if (argv.h || argv.help) { }); } +const isTTY = process.stdout.isTTY; + async function sync (token) { const start = Date.now(); @@ -138,7 +140,11 @@ async function sync (token) { const now = new Now(apiUrl, token, { debug }); try { - await now.create(path, { forceNew: force, forceSync: forceSync }); + await now.create(path, { + forceNew: force, + forceSync: forceSync, + quiet: !isTTY + }); } catch (err) { handleError(err); process.exit(1); @@ -147,15 +153,19 @@ async function sync (token) { const { url } = now; const elapsed = ms(new Date() - start); - if (clipboard) { - try { - await copy(url); - console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} (copied to clipboard) [${elapsed}]`); - } catch (err) { - console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} [${elapsed}]`); + if (isTTY) { + if (clipboard) { + try { + await copy(url); + console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} (copied to clipboard) [${elapsed}]`); + } catch (err) { + console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} [${elapsed}]`); + } + } else { + console.log(`> ${url} [${elapsed}]`); } } else { - console.log(`> ${url} [${elapsed}]`); + system.stdout.write(url); } const start_u = new Date(); @@ -209,9 +219,8 @@ async function sync (token) { function printLogs (host) { // log build - const logger = new Logger(host); + const logger = new Logger(host, { debug, quiet: !isTTY }); logger.on('error', () => { - console.log('> Connection error.'); process.exit(1); }); logger.on('close', () => { From 7eab45a017741ef75eaab4ae86d5447d74ea3088 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Tue, 5 Jul 2016 16:16:24 -0700 Subject: [PATCH 2/6] build-logger: log only errors in quiet mode --- lib/build-logger.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/build-logger.js b/lib/build-logger.js index a40f976..1baa7e9 100644 --- a/lib/build-logger.js +++ b/lib/build-logger.js @@ -5,15 +5,16 @@ import EventEmitter from 'events'; export default class Logger extends EventEmitter { - constructor (host) { + constructor (host, { debug = false, quiet = false } = {}) { super(); this.host = host; + this.quiet = quiet; // readyState this.building = false; this.socket = io(`https://io.now.sh?host=${host}`); - this.socket.once('error', this.onError.bind(this)); + this.socket.once('error', this.onSocketError.bind(this)); this.socket.on('state', this.onState.bind(this)); this.socket.on('logs', this.onLog.bind(this)); this.socket.on('backend', this.onComplete.bind(this)); @@ -25,6 +26,13 @@ export default class Logger extends EventEmitter { if (!state.id) { console.error('> Deployment not found'); this.emit('error'); + return; + } + + if (state.error) { + console.error('> Deployment error'); + this.emit('error'); + return; } if (state.backend) { @@ -43,6 +51,8 @@ export default class Logger extends EventEmitter { this.building = true; } + if (this.quiet) return; + if ('command' === log.type) { console.log(`${chalk.gray('>')} ▲ ${log.data}`); this.lines.reset(); @@ -72,7 +82,9 @@ export default class Logger extends EventEmitter { this.emit('close'); } - onError () { + onSocketError () { + this.removeAllListeners(); + console.error('> Connection error'); this.emit('error'); } From 0af1f69ed20d20a7ba5f6307a71f065abf3a5684 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Tue, 5 Jul 2016 16:17:21 -0700 Subject: [PATCH 3/6] index: add quiet mode --- lib/index.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index e0acd72..3131409 100644 --- a/lib/index.js +++ b/lib/index.js @@ -28,7 +28,7 @@ export default class Now extends EventEmitter { this._onRetry = this._onRetry.bind(this); } - async create (path, { forceNew, forceSync }) { + async create (path, { forceNew, forceSync, quiet = false }) { this._path = path; try { @@ -139,20 +139,22 @@ export default class Now extends EventEmitter { }); if (sizeExceeded) { - console.log(`> \u001b[31mWarning!\u001b[39m ${sizeExceeded} of the files ` + + console.error(`> \u001b[31mWarning!\u001b[39m ${sizeExceeded} of the files ` + 'exceeded the limit for your plan.\n' + `> See ${chalk.underline('https://zeit.co/account')} to upgrade.`); } } - if (engines && engines.node) { - if (missingVersion) { - console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`); + if (!quiet) { + if (engines && engines.node) { + if (missingVersion) { + console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`); + } else { + console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (requested: ${chalk.dim(`\`${engines.node}\``)})`); + } } else { - console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (requested: ${chalk.dim(`\`${engines.node}\``)})`); + console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`); } - } else { - console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`); } this._id = deployment.deploymentId; From 3eabe0e183bfa1b571284f82d3f9673db62393df Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Sat, 30 Jul 2016 17:30:09 -0700 Subject: [PATCH 4/6] fix updates warning about older versions --- lib/check-update.js | 1 + package.json | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/check-update.js b/lib/check-update.js index ecdba9b..6fccca7 100644 --- a/lib/check-update.js +++ b/lib/check-update.js @@ -2,6 +2,7 @@ import ms from 'ms'; import pkg from '../../package'; // relative to `build/` :\ import fetch from 'node-fetch'; import chalk from 'chalk'; +import compare from 'semver-compare'; const isTTY = process.stdout.isTTY; diff --git a/package.json b/package.json index 0e2e281..b9c8c65 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "node-fetch": "1.5.3", "progress": "1.1.8", "resumer": "0.0.0", + "semver-compare": "1.0.0", "socket.io-client": "1.4.6", "spdy": "3.3.3", "split-array": "1.0.1", From 396bd8713f62ba70194e00c7e3fba658f17e21c8 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Sat, 30 Jul 2016 17:30:36 -0700 Subject: [PATCH 5/6] activate quiet mode when piping --- bin/now-deploy | 31 +++++++++++++++++++------------ lib/build-logger.js | 4 +++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/bin/now-deploy b/bin/now-deploy index 70168fc..019a22a 100755 --- a/bin/now-deploy +++ b/bin/now-deploy @@ -100,7 +100,8 @@ const forceNew = argv.force; const forceSync = argv.forceSync; const shouldLogin = argv.login; const apiUrl = argv.url || 'https://api.zeit.co'; - +const isTTY = process.stdout.isTTY; +const quiet = !isTTY; const config = cfg.read(); if (argv.h || argv.help) { @@ -133,12 +134,12 @@ if (argv.h || argv.help) { }); } -const isTTY = process.stdout.isTTY; - async function sync (token) { const start = Date.now(); - console.log(`> Deploying "${path}"`); + if (!quiet) { + console.log(`> Deploying "${path}"`); + } const now = new Now(apiUrl, token, { debug }); @@ -147,7 +148,7 @@ async function sync (token) { forceNew, forceSync, forwardNpm, - quiet: !isTTY + quiet }); } catch (err) { handleError(err); @@ -169,14 +170,16 @@ async function sync (token) { console.log(`> ${url} [${elapsed}]`); } } else { - system.stdout.write(url); + process.stdout.write(url); } const start_u = new Date(); const complete = () => { - const elapsed_u = ms(new Date() - start_u); - console.log(`> Sync complete (${bytes(now.syncAmount)}) [${elapsed_u}] `); - console.log('> Initializing…'); + if (!quiet) { + const elapsed_u = ms(new Date() - start_u); + console.log(`> Sync complete (${bytes(now.syncAmount)}) [${elapsed_u}] `); + console.log('> Initializing…'); + } // close http2 agent now.close(); @@ -211,7 +214,9 @@ async function sync (token) { process.exit(1); }); } else { - console.log(`> Initializing…`); + if (!quiet) { + console.log(`> Initializing…`); + } // close http2 agent now.close(); @@ -223,12 +228,14 @@ async function sync (token) { function printLogs (host) { // log build - const logger = new Logger(host, { debug, quiet: !isTTY }); + const logger = new Logger(host, { debug, quiet }); logger.on('error', () => { process.exit(1); }); logger.on('close', () => { - console.log(`${chalk.cyan('> Deployment complete!')}`); + if (!quiet) { + console.log(`${chalk.cyan('> Deployment complete!')}`); + } process.exit(0); }); } diff --git a/lib/build-logger.js b/lib/build-logger.js index 1baa7e9..286e804 100644 --- a/lib/build-logger.js +++ b/lib/build-logger.js @@ -47,7 +47,9 @@ export default class Logger extends EventEmitter { onLog (log) { if (!this.building) { - console.log('> Building'); + if (!this.quiet) { + console.log('> Building'); + } this.building = true; } From de8e3e03cd445a1e50687660030a97e841ca23eb Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Sat, 30 Jul 2016 17:30:48 -0700 Subject: [PATCH 6/6] check-update: only warn about truly new versions --- lib/check-update.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/check-update.js b/lib/check-update.js index 6fccca7..f64afc9 100644 --- a/lib/check-update.js +++ b/lib/check-update.js @@ -64,7 +64,7 @@ function check ({ debug = false }) { const { latest } = data['dist-tags']; const current = pkg.version; - if (latest !== pkg.version) { + if (1 === compare(latest, pkg.version)) { if (debug) console.log(`> [debug] Needs update. Current ${current}, latest ${latest}`); resolve({ latest,