From ca100213670948f7b6fb97ab31df8f3cf8da9be8 Mon Sep 17 00:00:00 2001 From: smuszel Date: Thu, 13 Jun 2019 18:53:07 +0200 Subject: [PATCH 1/6] feat: smaller bundle size, cut lodash imports --- src/action/base.ts | 7 +++++-- src/list.ts | 6 +++--- src/styled/table.ts | 13 +++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/action/base.ts b/src/action/base.ts index 4fd77f2f..4dbd7fd0 100644 --- a/src/action/base.ts +++ b/src/action/base.ts @@ -1,4 +1,4 @@ -import * as _ from 'lodash' +import castArray from 'lodash/castArray' import {inspect} from 'util' export interface ITask { @@ -182,6 +182,9 @@ export class ActionBase { * write to the real stdout/stderr */ protected _write(std: 'stdout' | 'stderr', s: string | string[]) { - this.stdmockOrigs[std].apply(process[std], _.castArray(s)) + let args = castArray(s) + if (0 in args) { + this.stdmockOrigs[std].apply(process[std], args as any) + } } } diff --git a/src/list.ts b/src/list.ts index bcaf59ba..04d8f049 100644 --- a/src/list.ts +++ b/src/list.ts @@ -1,6 +1,6 @@ // tslint:disable -import * as _ from 'lodash' +import maxBy from 'lodash/maxBy' import deps from './deps' @@ -17,14 +17,14 @@ export function renderList(items: IListItem[]): string { if (items.length === 0) { return '' } - const maxLength = (_.maxBy(items, '[0].length') as any)[0].length + const maxLength = (maxBy(items, '[0].length') as any)[0].length const lines = items.map(i => { let left = i[0] let right = i[1] if (!right) { return left } - left = `${_.padEnd(left, maxLength)}` + left = left.padEnd(maxLength) right = linewrap(maxLength + 2, right) return `${left} ${right}` }) diff --git a/src/styled/table.ts b/src/styled/table.ts index 91f93bbd..bac06161 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -1,7 +1,8 @@ import {flags as F} from '@oclif/command' import {stdtermwidth} from '@oclif/screen' import chalk from 'chalk' -import * as _ from 'lodash' +import capitalize from 'lodash/capitalize' +import sumBy from 'lodash/sumBy' import {inspect} from 'util' const sw = require('string-width') @@ -17,7 +18,7 @@ class Table { const col = columns[key] const extended = col.extended || false const get = col.get || ((row: any) => row[key]) - const header = typeof col.header === 'string' ? col.header : _.capitalize(key.replace(/\_/g, ' ')) + const header = typeof col.header === 'string' ? col.header : capitalize(key.replace(/\_/g, ' ')) const minWidth = Math.max(col.minWidth || 0, sw(header) + 1) return { @@ -153,7 +154,7 @@ class Table { if (options['no-truncate'] || !process.stdout.isTTY) return // don't shorten if there is enough screen width - let dataMaxWidth = _.sumBy(columns, c => c.width!) + let dataMaxWidth = sumBy(columns, c => c.width!) let overWidth = dataMaxWidth - maxWidth if (overWidth <= 0) return @@ -165,15 +166,15 @@ class Table { // if sum(minWidth's) is greater than term width // nothing can be done so // display all as minWidth - let dataMinWidth = _.sumBy(columns, c => c.minWidth!) + let dataMinWidth = sumBy(columns, c => c.minWidth!) if (dataMinWidth >= maxWidth) return // some wiggle room left, add it back to "needy" columns let wiggleRoom = maxWidth - dataMinWidth - let needyCols = _.sortBy(columns.map(c => ({key: c.key, needs: c.maxWidth! - c.width!})), c => c.needs) + let needyCols = columns.map(c => ({key: c.key, needs: c.maxWidth! - c.width!})).sort((a, b) => a.needs - b.needs) for (let {key, needs} of needyCols) { if (!needs) continue - let col = _.find(columns, c => (key === c.key)) + let col = columns.find(c => key === c.key) if (!col) continue if (wiggleRoom > needs) { col.width = col.width! + needs From b17969a416a2fbe8f2e37e97db3bd69013eb835a Mon Sep 17 00:00:00 2001 From: RasPhilCo Date: Fri, 15 May 2020 13:08:28 -0700 Subject: [PATCH 2/6] Update src/styled/table.ts --- src/styled/table.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/styled/table.ts b/src/styled/table.ts index 16193647..e0464571 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -203,8 +203,8 @@ class Table { if (options['no-truncate'] || (!process.stdout.isTTY && !process.env.CLI_UX_SKIP_TTY_CHECK)) return // don't shorten if there is enough screen width - let dataMaxWidth = sumBy(columns, c => c.width!) - let overWidth = dataMaxWidth - maxWidth + const dataMaxWidth = sumBy(columns, c => c.width!) + const overWidth = dataMaxWidth - maxWidth if (overWidth <= 0) return // not enough room, short all columns to minWidth From 59d283e09a3ad333fc12ce35b127bf4e1e5c1c01 Mon Sep 17 00:00:00 2001 From: RasPhilCo Date: Fri, 15 May 2020 13:09:06 -0700 Subject: [PATCH 3/6] Update src/styled/table.ts --- src/styled/table.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/styled/table.ts b/src/styled/table.ts index e0464571..7e58bc00 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -220,7 +220,7 @@ class Table { // some wiggle room left, add it back to "needy" columns let wiggleRoom = maxWidth - dataMinWidth - let needyCols = columns.map(c => ({key: c.key, needs: c.maxWidth! - c.width!})).sort((a, b) => a.needs - b.needs) + const needyCols = columns.map(c => ({key: c.key, needs: c.maxWidth! - c.width!})).sort((a, b) => a.needs - b.needs) for (let {key, needs} of needyCols) { if (!needs) continue let col = columns.find(c => key === c.key) From 13439761659569b0b37174b90be817ae61078433 Mon Sep 17 00:00:00 2001 From: RasPhilCo Date: Fri, 15 May 2020 13:09:50 -0700 Subject: [PATCH 4/6] Update src/styled/table.ts --- src/styled/table.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/styled/table.ts b/src/styled/table.ts index 7e58bc00..d977bc9e 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -223,7 +223,7 @@ class Table { const needyCols = columns.map(c => ({key: c.key, needs: c.maxWidth! - c.width!})).sort((a, b) => a.needs - b.needs) for (let {key, needs} of needyCols) { if (!needs) continue - let col = columns.find(c => key === c.key) + const col = columns.find(c => key === c.key) if (!col) continue if (wiggleRoom > needs) { col.width = col.width! + needs From 00877a56751b5b95ee8ff714a7f6cf6e55140347 Mon Sep 17 00:00:00 2001 From: RasPhilCo Date: Fri, 15 May 2020 13:12:40 -0700 Subject: [PATCH 5/6] Update src/styled/table.ts --- src/styled/table.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/styled/table.ts b/src/styled/table.ts index d977bc9e..93e9bc84 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -20,7 +20,7 @@ class Table { const col = columns[key] const extended = col.extended || false const get = col.get || ((row: any) => row[key]) - const header = typeof col.header === 'string' ? col.header : capitalize(key.replace(/\_/g, ' ')) + const header = typeof col.header === 'string' ? col.header : capitalize(key.replace(/_/g, ' ')) const minWidth = Math.max(col.minWidth || 0, sw(header) + 1) return { From ef0d773581813cde338448a4f2ec16ed9e41e19b Mon Sep 17 00:00:00 2001 From: RasPhilCo Date: Fri, 15 May 2020 13:13:38 -0700 Subject: [PATCH 6/6] Update src/styled/table.ts --- src/styled/table.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/styled/table.ts b/src/styled/table.ts index 93e9bc84..fc815271 100644 --- a/src/styled/table.ts +++ b/src/styled/table.ts @@ -221,7 +221,7 @@ class Table { // some wiggle room left, add it back to "needy" columns let wiggleRoom = maxWidth - dataMinWidth const needyCols = columns.map(c => ({key: c.key, needs: c.maxWidth! - c.width!})).sort((a, b) => a.needs - b.needs) - for (let {key, needs} of needyCols) { + for (const {key, needs} of needyCols) { if (!needs) continue const col = columns.find(c => key === c.key) if (!col) continue