From 2222a90884f44f9e9cb89a4c1701aaa875bd6c14 Mon Sep 17 00:00:00 2001 From: Paulo Castro Date: Wed, 30 Oct 2019 15:25:05 +0000 Subject: [PATCH] Enable debug-mode "long stack traces" for Bluebird promises (async code) Change-type: patch Signed-off-by: Paulo Castro --- lib/app-common.ts | 35 ++++++++++++++++++++++++----------- tests/helpers.ts | 3 +++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/app-common.ts b/lib/app-common.ts index a309803e..20de1316 100644 --- a/lib/app-common.ts +++ b/lib/app-common.ts @@ -90,21 +90,34 @@ function setupBalenaSdkSharedOptions() { }); } -export function globalInit() { - setupRaven(); - checkNodeVersion(); - setupGlobalHttpProxy(); - setupBalenaSdkSharedOptions(); +let BluebirdConfigured = false; - // Assign bluebird as the global promise library. - // stream-to-promise will produce native promises if not for this module, - // which is likely to lead to errors as much of the CLI coffeescript code - // expects bluebird promises. - // The registration is only run if it hasn't already happened (for example - // in a test case). +/** + * Configure Bluebird and assign it as the global promise library. + * Modules like `stream-to-promise` will otherwise produce native promises, + * which leads to errors as much of the CLI CoffeeScript code expects Bluebird + * promises. + */ +export function configureBluebird() { + if (BluebirdConfigured) { + return; + } + BluebirdConfigured = true; + const Bluebird = require('bluebird'); + Bluebird.config({ + longStackTraces: process.env.DEBUG ? true : false, + }); if (!(global as any)['@@any-promise/REGISTRATION']) { require('any-promise/register/bluebird'); } +} + +export function globalInit() { + setupRaven(); + checkNodeVersion(); + configureBluebird(); + setupGlobalHttpProxy(); + setupBalenaSdkSharedOptions(); // check for CLI updates once a day require('./utils/update').notify(); diff --git a/tests/helpers.ts b/tests/helpers.ts index 413801c4..ed33e0a5 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -20,6 +20,9 @@ import * as nock from 'nock'; import * as path from 'path'; import * as balenaCLI from '../build/app'; +import { configureBluebird } from '../build/app-common'; + +configureBluebird(); export const runCommand = async (cmd: string) => { const preArgs = [process.argv[0], path.join(process.cwd(), 'bin', 'balena')];