From fca3015a5ec63f166df5567ba7b05def45cbdf7f Mon Sep 17 00:00:00 2001 From: ac360 Date: Mon, 14 Aug 2017 22:28:46 -0700 Subject: [PATCH] serverless run ui improvements --- lib/plugins/run/index.js | 8 +++--- .../utils/deployFunctionsToLocalEmulator.js | 2 +- lib/plugins/run/utils/logEventGateway.js | 27 +++++++++---------- lib/plugins/run/utils/logLocalEmulator.js | 25 ++++++++++++++--- lib/plugins/run/utils/logServerless.js | 2 +- lib/plugins/run/utils/manageEventGateway.js | 2 +- lib/plugins/run/utils/manageLocalEmulator.js | 4 +-- 7 files changed, 43 insertions(+), 27 deletions(-) diff --git a/lib/plugins/run/index.js b/lib/plugins/run/index.js index 3eb76b1a5..f3a4246d7 100644 --- a/lib/plugins/run/index.js +++ b/lib/plugins/run/index.js @@ -74,7 +74,7 @@ class Run { return localEmulatorRunning(getLocalRootUrl(this.options.lport)) .then(localEmulatorAlreadyRunning => { if (localEmulatorAlreadyRunning) { - logServerless('Emulator already running'); + // logServerless('Emulator already running'); functionsDeployed = true; return deployFunctionsToLocalEmulator( this.serverless.service, @@ -82,7 +82,7 @@ class Run { getLocalRootUrl(this.options.lport) ).then(() => { // eslint-disable-next-line max-len - logServerless('Functions deployed. For details please review the terminal running the Emulator.'); + logServerless('Functions loaded successfully in your current "serverless run" session.'); }); } return BbPromise.resolve(); @@ -91,7 +91,7 @@ class Run { .then(eventGatewayAlreadyRunning => { if (eventGatewayAlreadyRunning) { functionsRegistered = true; - logServerless('Event Gateway already running'); + // logServerless('Event Gateway already running'); return registerFunctionsToEventGateway( this.serverless.service, getLocalRootUrl(this.options.eport), @@ -99,7 +99,7 @@ class Run { getLocalRootUrl(this.options.lport) ).then(() => { // eslint-disable-next-line max-len - logServerless('Functions and subscriptions registered. For details please review the terminal running the Event Gateway.'); + // logServerless('Functions and subscriptions registered. For details please review the terminal running the Event Gateway.'); }); } return BbPromise.resolve(); diff --git a/lib/plugins/run/utils/deployFunctionsToLocalEmulator.js b/lib/plugins/run/utils/deployFunctionsToLocalEmulator.js index ed398b1ad..b1ad9c41f 100644 --- a/lib/plugins/run/utils/deployFunctionsToLocalEmulator.js +++ b/lib/plugins/run/utils/deployFunctionsToLocalEmulator.js @@ -8,7 +8,7 @@ const logLocalEmulator = require('./logLocalEmulator'); function deployFunctionsToLocalEmulator(service, servicePath, localEmulatorRootUrl) { // NOTE important for UX since it takes a while to upload large functions - logLocalEmulator('Deploying functions...'); + logLocalEmulator('Functions loading...'); const functionDeploymentPromises = []; diff --git a/lib/plugins/run/utils/logEventGateway.js b/lib/plugins/run/utils/logEventGateway.js index 82a77f126..c720bcadf 100644 --- a/lib/plugins/run/utils/logEventGateway.js +++ b/lib/plugins/run/utils/logEventGateway.js @@ -6,19 +6,18 @@ const os = require('os'); const colorPrefix = chalk.hex('#D86121'); const colorDim = chalk.hex('#777777'); -const spaceSmall = ' '; -const spaceLarge = ' '; +const spaceSmall = ' '; const prefix = colorPrefix(` Event Gateway${spaceSmall}`); const prettifyValue = value => { const prettified = JSON.stringify(value, null, 2).replace( new RegExp('\\n', 'g'), - `${os.EOL} ` + `${os.EOL}${spaceSmall}` ); - return `${spaceLarge}${prettified}`; + return `${spaceSmall}${prettified}`; }; -const getMessage = msg => { +const processMessage = msg => { let parsedMsg; try { parsedMsg = JSON.parse(msg); @@ -34,7 +33,7 @@ const getMessage = msg => { return `Subscription removed: event:${parsedMsg.event} >>> function:${parsedMsg.functionId}`; } else if (parsedMsg.msg === 'Event received.') { const event = JSON.parse(parsedMsg.event); - const text = `Event received: ${event.event}${os.EOL}`; + const text = `Event received: ${event.event}${os.EOL}${os.EOL}`; return `${text}${colorDim(prettifyValue(event))}${os.EOL}`; } else if (parsedMsg.msg === 'Function triggered.') { const event = JSON.parse(parsedMsg.event); @@ -42,16 +41,12 @@ const getMessage = msg => { return `${text}`; } else if (parsedMsg.msg === 'Function finished.') { const response = prettifyValue(JSON.parse(parsedMsg.response)); - const text = `Function finished: ${parsedMsg.functionId}${os.EOL}${colorDim(response)}${os.EOL}`; + const text = `Function finished: ${parsedMsg.functionId}${os.EOL}${os.EOL}${colorDim(response)}${os.EOL}`; return `${text}`; } else if (parsedMsg.msg === 'Function not found for HTTP event.') { - const response = prettifyValue(JSON.parse(parsedMsg.event)); - const text = `Function not found for HTTP event:${os.EOL}${colorDim(response)}`; - return `${text}`; + return false; } else if (parsedMsg.msg === 'Function invocation failed.') { - const text = `Failed to invoke function ${parsedMsg.functionId}${os.EOL}`; - const errorText = `${spaceLarge}error: ${parsedMsg.error}`; - return `${text}${errorText}`; + return false; } else if (parsedMsg.msg.startsWith('Running in development mode with embedded etcd')) { const partOne = 'Running in development mode with embedded etcd. Event API listening on '; const re = new RegExp(`${partOne}(.*). Config API listening on (.*).`); @@ -68,8 +63,10 @@ const getMessage = msg => { module.exports = msg => { try { - const processedMsg = getMessage(msg); - log(`${prefix}${processedMsg}${os.EOL}`); + const processedMsg = processMessage(msg); + if (processedMsg) { + log(`${prefix}${processedMsg}${os.EOL}`); + } } catch (err) { // NOTE keep this here - it's a worse UX to skip messages // rather than having an unformated message logged diff --git a/lib/plugins/run/utils/logLocalEmulator.js b/lib/plugins/run/utils/logLocalEmulator.js index a095541a6..3de65d4d9 100644 --- a/lib/plugins/run/utils/logLocalEmulator.js +++ b/lib/plugins/run/utils/logLocalEmulator.js @@ -1,13 +1,32 @@ 'use strict'; +const os = require('os'); const chalk = require('chalk'); const colorPrefix = chalk.hex('#bdb018'); -const spaceSmall = ' '; -const prefix = colorPrefix(` Emulator ${spaceSmall}`); +const colorDim = chalk.hex('#777777'); +const spaceSmall = ' '; +const prefix = colorPrefix(` Serverless${spaceSmall}`); + +const processMessage = msg => { + if (msg.trim().length < 0) { + return false; + } else if (typeof msg === 'string') { + msg = msg.trim(); + + if (msg.startsWith('Error:')) { + msg = `Function error:${os.EOL}${os.EOL}${colorDim(msg)}${os.EOL}` + } + } + + return msg; +}; function logLocalEmulator(message) { - process.stdout.write(`${prefix}${message.trim()}\n`); + message = processMessage(message); + if (message) { + process.stdout.write(`${prefix}${message}${os.EOL}`); + } } module.exports = logLocalEmulator; diff --git a/lib/plugins/run/utils/logServerless.js b/lib/plugins/run/utils/logServerless.js index c49bb8649..3103dfef9 100644 --- a/lib/plugins/run/utils/logServerless.js +++ b/lib/plugins/run/utils/logServerless.js @@ -3,7 +3,7 @@ const chalk = require('chalk'); const colorPrefix = chalk.hex('#bdb018'); -const spaceSmall = ' '; +const spaceSmall = ' '; const prefix = colorPrefix(` Serverless${spaceSmall}`); function logServerless(message) { diff --git a/lib/plugins/run/utils/manageEventGateway.js b/lib/plugins/run/utils/manageEventGateway.js index afebf887e..f20874a0f 100644 --- a/lib/plugins/run/utils/manageEventGateway.js +++ b/lib/plugins/run/utils/manageEventGateway.js @@ -26,7 +26,7 @@ const splitLinesAndLog = data => { function manageEventGateway(service, eventsPort, configurationPort, localEmulatorPort) { let initialized = false; const binaryFilePath = path.join(os.homedir(), '.serverless', 'event-gateway', 'event-gateway'); - logServerless('Initializing Event Gateway...'); + logServerless('Event Gateway initializing...'); const args = [ `-embed-data-dir=${getTmpDirPath()}`, diff --git a/lib/plugins/run/utils/manageLocalEmulator.js b/lib/plugins/run/utils/manageLocalEmulator.js index 229c444e6..b0f281491 100644 --- a/lib/plugins/run/utils/manageLocalEmulator.js +++ b/lib/plugins/run/utils/manageLocalEmulator.js @@ -14,9 +14,9 @@ function manageLocalEmulator(service, servicePath, options) { let params = ['--port', port]; if (debug) { params = params.concat(['--debug']); - logServerless('Initializing Emulator in debug mode...'); + logServerless('Emulator initializing in debug mode...'); } else { - logServerless('Initializing Emulator...'); + logServerless('Emulator initializing...'); } const cp = childProcess.spawn('sle', params);