Merge pull request #1640 from balena-io/lazy-load-chalk

Lazy-load chalk
This commit is contained in:
Page- 2020-02-29 00:16:03 +00:00 committed by GitHub
commit fb321b8c5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 34 additions and 32 deletions

View File

@ -22,7 +22,7 @@ dockerUtils = require('../utils/docker')
compose = require('../utils/compose') compose = require('../utils/compose')
{ registrySecretsHelp } = require('../utils/messages') { registrySecretsHelp } = require('../utils/messages')
{ ExpectedError } = require('../errors') { ExpectedError } = require('../errors')
{ getBalenaSdk } = require('../utils/lazy') { getBalenaSdk, getChalk } = require('../utils/lazy')
### ###
Opts must be an object with the following keys: Opts must be an object with the following keys:
@ -89,10 +89,9 @@ deployProject = (docker, logger, composeOpts, opts) ->
} }
.then (images) -> .then (images) ->
if opts.app.application_type?[0]?.is_legacy if opts.app.application_type?[0]?.is_legacy
chalk = require('chalk')
legacyDeploy = require('../utils/deploy-legacy') legacyDeploy = require('../utils/deploy-legacy')
msg = chalk.yellow('Target application requires legacy deploy method.') msg = getChalk().yellow('Target application requires legacy deploy method.')
logger.logWarn(msg) logger.logWarn(msg)
return Promise.join( return Promise.join(

View File

@ -1,9 +1,9 @@
Promise = require('bluebird') Promise = require('bluebird')
_ = require('lodash') _ = require('lodash')
chalk = require('chalk')
dockerUtils = require('../../utils/docker') dockerUtils = require('../../utils/docker')
{ exitWithExpectedError } = require('../../utils/patterns') { exitWithExpectedError } = require('../../utils/patterns')
{ getChalk } = require('../../utils/lazy')
exports.dockerPort = dockerPort = 2375 exports.dockerPort = dockerPort = 2375
exports.dockerTimeout = dockerTimeout = 2000 exports.dockerTimeout = dockerTimeout = 2000
@ -56,7 +56,7 @@ exports.pipeContainerStream = Promise.method ({ deviceIp, name, outStream, follo
.catch (err) -> .catch (err) ->
err = '' + err.statusCode err = '' + err.statusCode
if err is '404' if err is '404'
return console.log(chalk.red.bold("Container '#{name}' not found.")) return console.log(getChalk().red.bold("Container '#{name}' not found."))
throw err throw err
exports.getSubShellCommand = require('../../utils/helpers').getSubShellCommand exports.getSubShellCommand = require('../../utils/helpers').getSubShellCommand

View File

@ -15,10 +15,9 @@ limitations under the License.
*/ */
import { CommandDefinition } from 'capitano'; import { CommandDefinition } from 'capitano';
import chalk from 'chalk';
import { stripIndent } from 'common-tags'; import { stripIndent } from 'common-tags';
import * as SDK from 'etcher-sdk'; import * as SDK from 'etcher-sdk';
import { getVisuals } from '../../utils/lazy'; import { getChalk, getVisuals } from '../../utils/lazy';
async function getDrive(options: { async function getDrive(options: {
drive?: string; drive?: string;
@ -88,7 +87,7 @@ export const flash: CommandDefinition<
default: false, default: false,
})); }));
if (yes !== true) { if (yes !== true) {
console.log(chalk.red.bold('Aborted image flash')); console.log(getChalk().red.bold('Aborted image flash'));
process.exit(0); process.exit(0);
} }
@ -109,7 +108,7 @@ export const flash: CommandDefinition<
[drive], [drive],
(_, error) => { (_, error) => {
// onFail // onFail
console.log(chalk.red.bold(error.message)); console.log(getChalk().red.bold(error.message));
}, },
(progress: SDK.multiWrite.MultiDestinationProgress) => { (progress: SDK.multiWrite.MultiDestinationProgress) => {
// onProgress // onProgress

View File

@ -20,6 +20,9 @@ limitations under the License.
# and `Cygwin` should be encapsulated in a # and `Cygwin` should be encapsulated in a
# re-usable package. # re-usable package.
# #
{ getChalk } = require('../../utils/lazy')
module.exports = module.exports =
signature: 'local stop [deviceIp]' signature: 'local stop [deviceIp]'
description: 'Stop a running container on a balenaOS device' description: 'Stop a running container on a balenaOS device'
@ -46,7 +49,7 @@ module.exports =
root: true root: true
action: (params, options) -> action: (params, options) ->
Promise = require('bluebird') Promise = require('bluebird')
chalk = require('chalk') chalk = getChalk()
{ forms, config, BalenaLocalDockerUtils } = require('balena-sync') { forms, config, BalenaLocalDockerUtils } = require('balena-sync')
{ selectContainerFromDevice, filterOutSupervisorContainer } = require('./common') { selectContainerFromDevice, filterOutSupervisorContainer } = require('./common')

View File

@ -20,9 +20,8 @@ limitations under the License.
*/ */
import { CommandDefinition } from 'capitano'; import { CommandDefinition } from 'capitano';
import chalk from 'chalk';
import { stripIndent } from 'common-tags'; import { stripIndent } from 'common-tags';
import { getVisuals } from '../utils/lazy'; import { getChalk, getVisuals } from '../utils/lazy';
export const availableDrives: CommandDefinition<{}, {}> = { export const availableDrives: CommandDefinition<{}, {}> = {
signature: 'util available-drives', signature: 'util available-drives',
@ -49,7 +48,9 @@ export const availableDrives: CommandDefinition<{}, {}> = {
if (scanner.drives.size === 0) { if (scanner.drives.size === 0) {
console.error( console.error(
`${chalk.red('x')} No available drives were detected, plug one in!`, `${getChalk().red(
'x',
)} No available drives were detected, plug one in!`,
); );
} else { } else {
console.log( console.log(

View File

@ -17,7 +17,7 @@
Promise = require('bluebird') Promise = require('bluebird')
path = require('path') path = require('path')
{ getBalenaSdk } = require('./lazy') { getBalenaSdk, getChalk } = require('./lazy')
exports.appendProjectOptions = appendProjectOptions = (opts) -> exports.appendProjectOptions = appendProjectOptions = (opts) ->
opts.concat [ opts.concat [
@ -394,7 +394,6 @@ authorizePush = (sdk, logger, tokenAuthEndpoint, registry, images, previousRepos
.catchReturn({}) .catchReturn({})
pushAndUpdateServiceImages = (docker, token, images, afterEach) -> pushAndUpdateServiceImages = (docker, token, images, afterEach) ->
chalk = require('chalk')
{ DockerProgress } = require('docker-progress') { DockerProgress } = require('docker-progress')
{ retry } = require('./helpers') { retry } = require('./helpers')
tty = require('./tty')(process.stdout) tty = require('./tty')(process.stdout)
@ -402,7 +401,7 @@ pushAndUpdateServiceImages = (docker, token, images, afterEach) ->
opts = { authconfig: registrytoken: token } opts = { authconfig: registrytoken: token }
progress = new DockerProgress(dockerToolbelt: docker) progress = new DockerProgress(dockerToolbelt: docker)
renderer = pushProgressRenderer(tty, chalk.blue('[Push]') + ' ') renderer = pushProgressRenderer(tty, getChalk().blue('[Push]') + ' ')
reporters = progress.aggregateProgress(images.length, renderer) reporters = progress.aggregateProgress(images.length, renderer)
Promise.using tty.cursorHidden(), -> Promise.using tty.cursorHidden(), ->
@ -441,11 +440,10 @@ exports.deployProject = (
skipLogUpload skipLogUpload
) -> ) ->
_ = require('lodash') _ = require('lodash')
chalk = require('chalk')
releaseMod = require('resin-release') releaseMod = require('resin-release')
tty = require('./tty')(process.stdout) tty = require('./tty')(process.stdout)
prefix = chalk.cyan('[Info]') + ' ' prefix = getChalk().cyan('[Info]') + ' '
spinner = createSpinner() spinner = createSpinner()
runloop = runSpinner(tty, spinner, "#{prefix}Creating release...") runloop = runSpinner(tty, spinner, "#{prefix}Creating release...")
@ -599,7 +597,6 @@ createRunLoop = (tick) ->
class BuildProgressUI class BuildProgressUI
constructor: (tty, descriptors) -> constructor: (tty, descriptors) ->
_ = require('lodash') _ = require('lodash')
chalk = require('chalk')
through = require('through2') through = require('through2')
eventHandler = @_handleEvent eventHandler = @_handleEvent
@ -621,7 +618,7 @@ class BuildProgressUI
# Logger magically prefixes the log line with [Build] etc., but it doesn't # Logger magically prefixes the log line with [Build] etc., but it doesn't
# work well with the spinner we're also showing. Manually build the prefix # work well with the spinner we're also showing. Manually build the prefix
# here and bypass the logger. # here and bypass the logger.
prefix = chalk.blue('[Build]') + ' ' prefix = getChalk().blue('[Build]') + ' '
offset = 10 # account for escape sequences inserted for colouring offset = 10 # account for escape sequences inserted for colouring
@_prefixWidth = offset + prefix.length + _.max(_.map(services, 'length')) @_prefixWidth = offset + prefix.length + _.max(_.map(services, 'length'))
@ -718,7 +715,7 @@ class BuildProgressUI
_renderSummary: (serviceToStrMap) -> _renderSummary: (serviceToStrMap) ->
_ = require('lodash') _ = require('lodash')
chalk = require('chalk') chalk = getChalk()
truncate = require('cli-truncate') truncate = require('cli-truncate')
strlen = require('string-width') strlen = require('string-width')
@ -785,7 +782,6 @@ class BuildProgressInline
_renderEvent: (service, event) => _renderEvent: (service, event) =>
_ = require('lodash') _ = require('lodash')
chalk = require('chalk')
str = do -> str = do ->
{ status, error } = event { status, error } = event
@ -796,7 +792,7 @@ class BuildProgressInline
else else
return 'Waiting...' return 'Waiting...'
prefix = _.padEnd(chalk.bold(service), @_prefixWidth) prefix = _.padEnd(getChalk().bold(service), @_prefixWidth)
@_outStream.write(prefix) @_outStream.write(prefix)
@_outStream.write(str) @_outStream.write(str)
@_outStream.write('\n') @_outStream.write('\n')

View File

@ -1,9 +1,9 @@
import * as Bluebird from 'bluebird'; import * as Bluebird from 'bluebird';
import chalk from 'chalk';
import ColorHash = require('color-hash'); import ColorHash = require('color-hash');
import * as _ from 'lodash'; import * as _ from 'lodash';
import { Readable } from 'stream'; import { Readable } from 'stream';
import { getChalk } from '../lazy';
import Logger = require('../logger'); import Logger = require('../logger');
interface Log { interface Log {
@ -116,5 +116,5 @@ const colorHash = new ColorHash();
function _getServiceColourFn(serviceName: string): (msg: string) => string { function _getServiceColourFn(serviceName: string): (msg: string) => string {
const [r, g, b] = colorHash.rgb(serviceName); const [r, g, b] = colorHash.rgb(serviceName);
return chalk.rgb(r, g, b); return getChalk().rgb(r, g, b);
} }

View File

@ -17,13 +17,12 @@ limitations under the License.
import { InitializeEmitter, OperationState } from 'balena-device-init'; import { InitializeEmitter, OperationState } from 'balena-device-init';
import * as BalenaSdk from 'balena-sdk'; import * as BalenaSdk from 'balena-sdk';
import Bluebird = require('bluebird'); import Bluebird = require('bluebird');
import chalk from 'chalk';
import _ = require('lodash'); import _ = require('lodash');
import os = require('os'); import os = require('os');
import * as ShellEscape from 'shell-escape'; import * as ShellEscape from 'shell-escape';
import { ExpectedError } from '../errors'; import { ExpectedError } from '../errors';
import { getBalenaSdk, getVisuals } from './lazy'; import { getBalenaSdk, getChalk, getVisuals } from './lazy';
export function getGroupDefaults(group: { export function getGroupDefaults(group: {
options: Array<{ name: string; default?: string }>; options: Array<{ name: string; default?: string }>;
@ -37,6 +36,7 @@ export function getGroupDefaults(group: {
export function stateToString(state: OperationState) { export function stateToString(state: OperationState) {
const percentage = _.padStart(`${state.percentage}`, 3, '0'); const percentage = _.padStart(`${state.percentage}`, 3, '0');
const chalk = getChalk();
const result = `${chalk.blue(percentage + '%')} ${chalk.cyan( const result = `${chalk.blue(percentage + '%')} ${chalk.cyan(
state.operation.command, state.operation.command,
)}`; )}`;

View File

@ -15,6 +15,7 @@ limitations under the License.
*/ */
import * as BalenaSdk from 'balena-sdk'; import * as BalenaSdk from 'balena-sdk';
import chalk from 'chalk';
import * as visuals from 'resin-cli-visuals'; import * as visuals from 'resin-cli-visuals';
// Equivalent of _.once but avoiding the need to import lodash for lazy deps // Equivalent of _.once but avoiding the need to import lodash for lazy deps
@ -35,3 +36,5 @@ export const getBalenaSdk = once(() =>
export const getVisuals = once( export const getVisuals = once(
() => require('resin-cli-visuals') as typeof visuals, () => require('resin-cli-visuals') as typeof visuals,
); );
export const getChalk = once(() => require('chalk') as typeof chalk);

View File

@ -14,10 +14,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import chalk from 'chalk';
import _ = require('lodash'); import _ = require('lodash');
import { EOL as eol } from 'os'; import { EOL as eol } from 'os';
import { StreamLogger } from 'resin-stream-logger'; import { StreamLogger } from 'resin-stream-logger';
import { getChalk } from './lazy';
enum Level { enum Level {
BUILD = 'build', BUILD = 'build',
@ -57,6 +57,7 @@ class Logger {
protected constructor() { protected constructor() {
const logger = new StreamLogger(); const logger = new StreamLogger();
const chalk = getChalk();
logger.addPrefix('build', chalk.blue('[Build]')); logger.addPrefix('build', chalk.blue('[Build]'));
logger.addPrefix('info', chalk.cyan('[Info]')); logger.addPrefix('info', chalk.cyan('[Info]'));
logger.addPrefix('debug', chalk.magenta('[Debug]')); logger.addPrefix('debug', chalk.magenta('[Debug]'));

View File

@ -16,13 +16,12 @@ limitations under the License.
import { BalenaApplicationNotFound } from 'balena-errors'; import { BalenaApplicationNotFound } from 'balena-errors';
import * as BalenaSdk from 'balena-sdk'; import * as BalenaSdk from 'balena-sdk';
import Bluebird = require('bluebird'); import Bluebird = require('bluebird');
import chalk from 'chalk';
import { stripIndent } from 'common-tags'; import { stripIndent } from 'common-tags';
import _ = require('lodash'); import _ = require('lodash');
import _form = require('resin-cli-form'); import _form = require('resin-cli-form');
import { NotLoggedInError } from '../errors'; import { NotLoggedInError } from '../errors';
import { getBalenaSdk, getVisuals } from './lazy'; import { getBalenaSdk, getChalk, getVisuals } from './lazy';
import messages = require('./messages'); import messages = require('./messages');
import validation = require('./validation'); import validation = require('./validation');
@ -431,6 +430,7 @@ export function selectFromList<T>(
} }
export function printErrorMessage(message: string) { export function printErrorMessage(message: string) {
const chalk = getChalk();
console.error(chalk.red(message)); console.error(chalk.red(message));
console.error(chalk.red(`\n${messages.getHelp}\n`)); console.error(chalk.red(`\n${messages.getHelp}\n`));
} }

View File

@ -1,6 +1,6 @@
import chalk from 'chalk';
import * as _sdk from 'etcher-sdk'; import * as _sdk from 'etcher-sdk';
import { getChalk } from '../lazy';
import { CustomDynamicList } from './custom-dynamic-list'; import { CustomDynamicList } from './custom-dynamic-list';
export class DriveList extends CustomDynamicList< export class DriveList extends CustomDynamicList<
@ -9,7 +9,7 @@ export class DriveList extends CustomDynamicList<
constructor(private scanner: _sdk.scanner.Scanner) { constructor(private scanner: _sdk.scanner.Scanner) {
super( super(
'Select a drive', 'Select a drive',
`${chalk.red('x')} No available drives were detected, plug one in!`, `${getChalk().red('x')} No available drives were detected, plug one in!`,
); );
const refresh = this.refresh.bind(this); const refresh = this.refresh.bind(this);
scanner.on('attach', refresh); scanner.on('attach', refresh);