Browse Source

Merge pull request #80 from zeit/add/piping

Add piping support
master
Tony Kovanen 9 years ago
committed by GitHub
parent
commit
29d569771a
  1. 53
      bin/now-deploy
  2. 22
      lib/build-logger.js
  3. 3
      lib/check-update.js
  4. 18
      lib/index.js
  5. 1
      package.json

53
bin/now-deploy

@ -96,11 +96,12 @@ const exit = (code) => {
const debug = argv.debug; const debug = argv.debug;
const clipboard = !argv['no-clipboard']; const clipboard = !argv['no-clipboard'];
const forwardNpm = argv['forward-npm']; const forwardNpm = argv['forward-npm'];
const force = argv.force; const forceNew = argv.force;
const forceSync = argv.forceSync; const forceSync = argv.forceSync;
const shouldLogin = argv.login; const shouldLogin = argv.login;
const apiUrl = argv.url || 'https://api.zeit.co'; const apiUrl = argv.url || 'https://api.zeit.co';
const isTTY = process.stdout.isTTY;
const quiet = !isTTY;
const config = cfg.read(); const config = cfg.read();
const alwaysForwardNpm = config.forwardNpm; const alwaysForwardNpm = config.forwardNpm;
@ -137,12 +138,19 @@ if (argv.h || argv.help) {
async function sync (token) { async function sync (token) {
const start = Date.now(); const start = Date.now();
console.log(`> Deploying "${path}"`); if (!quiet) {
console.log(`> Deploying "${path}"`);
}
const now = new Now(apiUrl, token, { debug }); const now = new Now(apiUrl, token, { debug });
try { try {
await now.create(path, { forceNew: force, forceSync: forceSync, forwardNpm: alwaysForwardNpm || forwardNpm }); await now.create(path, {
forceNew,
forceSync,
forwardNpm: alwaysForwardNpm || forwardNpm,
quiet
});
} catch (err) { } catch (err) {
handleError(err); handleError(err);
process.exit(1); process.exit(1);
@ -151,22 +159,28 @@ async function sync (token) {
const { url } = now; const { url } = now;
const elapsed = ms(new Date() - start); const elapsed = ms(new Date() - start);
if (clipboard) { if (isTTY) {
try { if (clipboard) {
await copy(url); try {
console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} (copied to clipboard) [${elapsed}]`); await copy(url);
} catch (err) { console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} (copied to clipboard) [${elapsed}]`);
console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} [${elapsed}]`); } catch (err) {
console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} [${elapsed}]`);
}
} else {
console.log(`> ${url} [${elapsed}]`);
} }
} else { } else {
console.log(`> ${url} [${elapsed}]`); process.stdout.write(url);
} }
const start_u = new Date(); const start_u = new Date();
const complete = () => { const complete = () => {
const elapsed_u = ms(new Date() - start_u); if (!quiet) {
console.log(`> Sync complete (${bytes(now.syncAmount)}) [${elapsed_u}] `); const elapsed_u = ms(new Date() - start_u);
console.log('> Initializing…'); console.log(`> Sync complete (${bytes(now.syncAmount)}) [${elapsed_u}] `);
console.log('> Initializing…');
}
// close http2 agent // close http2 agent
now.close(); now.close();
@ -201,7 +215,9 @@ async function sync (token) {
process.exit(1); process.exit(1);
}); });
} else { } else {
console.log(`> Initializing…`); if (!quiet) {
console.log(`> Initializing…`);
}
// close http2 agent // close http2 agent
now.close(); now.close();
@ -213,13 +229,14 @@ async function sync (token) {
function printLogs (host) { function printLogs (host) {
// log build // log build
const logger = new Logger(host); const logger = new Logger(host, { debug, quiet });
logger.on('error', () => { logger.on('error', () => {
console.log('> Connection error.');
process.exit(1); process.exit(1);
}); });
logger.on('close', () => { logger.on('close', () => {
console.log(`${chalk.cyan('> Deployment complete!')}`); if (!quiet) {
console.log(`${chalk.cyan('> Deployment complete!')}`);
}
process.exit(0); process.exit(0);
}); });
} }

22
lib/build-logger.js

@ -5,15 +5,16 @@ import EventEmitter from 'events';
export default class Logger extends EventEmitter { export default class Logger extends EventEmitter {
constructor (host) { constructor (host, { debug = false, quiet = false } = {}) {
super(); super();
this.host = host; this.host = host;
this.quiet = quiet;
// readyState // readyState
this.building = false; this.building = false;
this.socket = io(`https://io.now.sh?host=${host}`); 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('state', this.onState.bind(this));
this.socket.on('logs', this.onLog.bind(this)); this.socket.on('logs', this.onLog.bind(this));
this.socket.on('backend', this.onComplete.bind(this)); this.socket.on('backend', this.onComplete.bind(this));
@ -25,6 +26,13 @@ export default class Logger extends EventEmitter {
if (!state.id) { if (!state.id) {
console.error('> Deployment not found'); console.error('> Deployment not found');
this.emit('error'); this.emit('error');
return;
}
if (state.error) {
console.error('> Deployment error');
this.emit('error');
return;
} }
if (state.backend) { if (state.backend) {
@ -39,10 +47,14 @@ export default class Logger extends EventEmitter {
onLog (log) { onLog (log) {
if (!this.building) { if (!this.building) {
console.log('> Building'); if (!this.quiet) {
console.log('> Building');
}
this.building = true; this.building = true;
} }
if (this.quiet) return;
if ('command' === log.type) { if ('command' === log.type) {
console.log(`${chalk.gray('>')}${log.data}`); console.log(`${chalk.gray('>')}${log.data}`);
this.lines.reset(); this.lines.reset();
@ -72,7 +84,9 @@ export default class Logger extends EventEmitter {
this.emit('close'); this.emit('close');
} }
onError () { onSocketError () {
this.removeAllListeners();
console.error('> Connection error');
this.emit('error'); this.emit('error');
} }

3
lib/check-update.js

@ -2,6 +2,7 @@ import ms from 'ms';
import pkg from '../../package'; // relative to `build/` :\ import pkg from '../../package'; // relative to `build/` :\
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import chalk from 'chalk'; import chalk from 'chalk';
import compare from 'semver-compare';
const isTTY = process.stdout.isTTY; const isTTY = process.stdout.isTTY;
@ -63,7 +64,7 @@ function check ({ debug = false }) {
const { latest } = data['dist-tags']; const { latest } = data['dist-tags'];
const current = pkg.version; 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}`); if (debug) console.log(`> [debug] Needs update. Current ${current}, latest ${latest}`);
resolve({ resolve({
latest, latest,

18
lib/index.js

@ -30,7 +30,7 @@ export default class Now extends EventEmitter {
this._onRetry = this._onRetry.bind(this); this._onRetry = this._onRetry.bind(this);
} }
async create (path, { forceNew, forceSync, forwardNpm }) { async create (path, { forceNew, forceSync, forwardNpm, quiet = false }) {
this._path = path; this._path = path;
try { try {
@ -168,20 +168,22 @@ export default class Now extends EventEmitter {
}); });
if (sizeExceeded) { 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' + 'exceeded the limit for your plan.\n' +
`> See ${chalk.underline('https://zeit.co/account')} to upgrade.`); `> See ${chalk.underline('https://zeit.co/account')} to upgrade.`);
} }
} }
if (engines && engines.node) { if (!quiet) {
if (missingVersion) { if (engines && engines.node) {
console.log(`> Using Node.js ${chalk.bold(deployment.nodeVersion)} (default)`); 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 { } 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; this._id = deployment.deploymentId;

1
package.json

@ -69,6 +69,7 @@
"node-fetch": "1.5.3", "node-fetch": "1.5.3",
"progress": "1.1.8", "progress": "1.1.8",
"resumer": "0.0.0", "resumer": "0.0.0",
"semver-compare": "1.0.0",
"socket.io-client": "1.4.6", "socket.io-client": "1.4.6",
"spdy": "3.3.3", "spdy": "3.3.3",
"split-array": "1.0.1", "split-array": "1.0.1",

Loading…
Cancel
Save