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 clipboard = !argv['no-clipboard'];
const forwardNpm = argv['forward-npm'];
const force = argv.force;
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();
const alwaysForwardNpm = config.forwardNpm;
@ -137,12 +138,19 @@ if (argv.h || argv.help) {
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 });
try {
await now.create(path, { forceNew: force, forceSync: forceSync, forwardNpm: alwaysForwardNpm || forwardNpm });
await now.create(path, {
forceNew,
forceSync,
forwardNpm: alwaysForwardNpm || forwardNpm,
quiet
});
} catch (err) {
handleError(err);
process.exit(1);
@ -151,22 +159,28 @@ 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}]`);
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();
@ -201,7 +215,9 @@ async function sync (token) {
process.exit(1);
});
} else {
console.log(`> Initializing…`);
if (!quiet) {
console.log(`> Initializing…`);
}
// close http2 agent
now.close();
@ -213,13 +229,14 @@ async function sync (token) {
function printLogs (host) {
// log build
const logger = new Logger(host);
const logger = new Logger(host, { debug, quiet });
logger.on('error', () => {
console.log('> Connection error.');
process.exit(1);
});
logger.on('close', () => {
console.log(`${chalk.cyan('> Deployment complete!')}`);
if (!quiet) {
console.log(`${chalk.cyan('> Deployment complete!')}`);
}
process.exit(0);
});
}

22
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) {
@ -39,10 +47,14 @@ export default class Logger extends EventEmitter {
onLog (log) {
if (!this.building) {
console.log('> Building');
if (!this.quiet) {
console.log('> Building');
}
this.building = true;
}
if (this.quiet) return;
if ('command' === log.type) {
console.log(`${chalk.gray('>')}${log.data}`);
this.lines.reset();
@ -72,7 +84,9 @@ export default class Logger extends EventEmitter {
this.emit('close');
}
onError () {
onSocketError () {
this.removeAllListeners();
console.error('> Connection 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 fetch from 'node-fetch';
import chalk from 'chalk';
import compare from 'semver-compare';
const isTTY = process.stdout.isTTY;
@ -63,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,

18
lib/index.js

@ -30,7 +30,7 @@ export default class Now extends EventEmitter {
this._onRetry = this._onRetry.bind(this);
}
async create (path, { forceNew, forceSync, forwardNpm }) {
async create (path, { forceNew, forceSync, forwardNpm, quiet = false }) {
this._path = path;
try {
@ -168,20 +168,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;

1
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",

Loading…
Cancel
Save