Browse Source

Merge pull request #13 from zeithq/add/build-output

Add build output
master
Tony Kovanen 9 years ago
parent
commit
d46433433f
  1. 31
      bin/now
  2. 94
      lib/build-logger.js
  3. 16
      lib/index.js
  4. 4
      package.json

31
bin/now

@ -6,6 +6,7 @@ import login from '../lib/login';
import * as cfg from '../lib/cfg';
import { version } from '../../package';
import checkUpdate from '../lib/check-update';
import Logger from '../lib/build-logger';
import bytes from 'bytes';
import chalk from 'chalk';
import minimist from 'minimist';
@ -105,9 +106,9 @@ async function sync (token) {
if (clipboard) {
try {
await copy(url);
console.log(`> ${chalk.cyan('Ready!')} ${chalk.bold(`https://${url}`)} (copied to clipboard) [${elapsed}]`);
console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} (copied to clipboard) [${elapsed}]`);
} catch (err) {
console.log(`> ${chalk.cyan('Ready!')} ${chalk.bold(`https://${url}`)} [${elapsed}]`);
console.log(`${chalk.cyan('> Ready!')} ${chalk.bold(url)} [${elapsed}]`);
}
} else {
console.log(`> ${url} [${elapsed}]`);
@ -117,8 +118,12 @@ async function sync (token) {
const complete = () => {
const elapsed_u = ms(new Date() - start_u);
console.log(`> Sync complete (${bytes(now.syncAmount)}) [${elapsed_u}] `);
// show build logs
printLogs(now.host);
// close http2 agent
now.close();
exit(0);
};
if (now.syncAmount) {
@ -148,11 +153,29 @@ async function sync (token) {
});
} else {
console.log('> Sync complete (cached)');
if (force) {
// show build logs
printLogs(now.host);
}
now.close();
exit(0);
}
}
function printLogs (host) {
// log build
const logger = new Logger(host);
logger.on('error', () => {
console.log('> Connection error.');
exit(1);
});
logger.on('close', () => {
console.log(`${chalk.cyan('> Build completed!')}`);
exit(0);
});
}
function handleError (err) {
if (403 === err.status) {
error('Authentication error. Run `now -L` or `now --login` to log-in again.');

94
lib/build-logger.js

@ -0,0 +1,94 @@
import ansi from 'ansi-escapes';
import io from 'socket.io-client';
import chalk from 'chalk';
import EventEmitter from 'events';
export default class Logger extends EventEmitter {
constructor (host) {
super();
this.host = host;
this.socket = io(`https://io.now.sh?host=${host}`);
this.socket.once('error', this.onError.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));
this.lines = new Lines(10);
}
onState (state) {
if (!state.id) {
console.error('> Deployment not found');
this.emit('error');
}
if (state.backend) {
this.onComplete();
return;
}
if (state.logs) {
state.logs.forEach(this.onLog, this);
}
}
onLog (log) {
if ('command' === log.type) {
if ('npm install' === log.data) {
console.log('> Building');
}
console.log(`${chalk.gray('>')}${log.data}`);
this.lines.reset();
} else if ('stderr' === log.type) {
log.data.split('\n').forEach((v) => {
if (v.length) {
console.error(chalk.red(`> ${v}`));
}
});
this.lines.reset();
} else if ('stdout' === log.type) {
log.data.split('\n').forEach((v) => {
if (v.length) {
this.lines.write(`${chalk.gray('>')} ${v}`);
}
});
}
}
onComplete () {
this.socket.disconnect();
this.emit('close');
}
onError () {
this.emit('error');
}
}
class Lines {
constructor (maxLines = 100) {
this.max = maxLines;
this.buf = [];
}
write (str) {
const { max, buf } = this;
if (buf.length === max) {
process.stdout.write(ansi.eraseLines(max + 1));
buf.shift();
buf.forEach((line) => console.log(line));
}
buf.push(str);
console.log(str);
}
reset () {
this.buf = [];
}
}

16
lib/index.js

@ -103,7 +103,7 @@ export default class Now extends EventEmitter {
}, { retries: 3, minTimeout: 2500, onRetry: this._onRetry });
this._id = deployment.deploymentId;
this._url = deployment.url;
this._host = deployment.url;
this._missing = deployment.missing || [];
return this._url;
@ -165,8 +165,16 @@ export default class Now extends EventEmitter {
this._agent.close();
}
get id () {
return this._id;
}
get url () {
return this._url;
return `https://${this._host}`;
}
get host () {
return this._host;
}
get syncAmount () {
@ -178,10 +186,10 @@ export default class Now extends EventEmitter {
return this._syncAmount;
}
async _fetch (url, opts) {
async _fetch (_url, opts) {
opts.headers = opts.headers || {};
opts.headers.authorization = `Bearer ${this._token}`;
return await this._agent.fetch(url, opts);
return await this._agent.fetch(_url, opts);
}
}

4
package.json

@ -26,7 +26,9 @@
"retry": "0.9.0",
"spdy": "3.2.3",
"split-array": "1.0.1",
"minimist": "1.2.0"
"minimist": "1.2.0",
"ansi-escapes": "1.3.0",
"socket.io-client": "1.4.5"
},
"devDependencies": {
"alpha-sort": "1.0.2",

Loading…
Cancel
Save