Skip to content

Commit 32aa387

Browse files
committed
convert terminal util to TS
1 parent 598a3d8 commit 32aa387

File tree

4 files changed

+28
-37
lines changed

4 files changed

+28
-37
lines changed

packages/server/lib/util/print-run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import humanTime from './human_time'
99
import duration from './duration'
1010
import newlines from './newlines'
1111
import * as env from './env'
12-
import terminal from './terminal'
12+
import * as terminal from './terminal'
1313
import { getIsCi } from './ci_provider'
1414
import * as experiments from '../experiments'
1515
import type { SpecFile, ProtocolError } from '@packages/types'

packages/server/lib/util/terminal.js renamed to packages/server/lib/util/terminal.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
const _ = require('lodash')
2-
const chalk = require('chalk')
3-
const Table = require('cli-table3')
4-
const utils = require('cli-table3/src/utils')
5-
const widestLine = require('widest-line')
6-
const terminalSize = require('./terminal-size')
1+
import _ from 'lodash'
2+
import chalk from 'chalk'
3+
import Table from 'cli-table3'
4+
import utils from 'cli-table3/src/utils'
5+
import widestLine from 'widest-line'
6+
import { get as getTerminalSize } from './terminal-size'
7+
import type { HorizontalAlignment } from 'cli-table3'
78

89
const MAXIMUM_SIZE = 100
910
const EXPECTED_SUM = 100
1011

11-
const getMaximumColumns = () => {
12+
export const getMaximumColumns = () => {
1213
// get the maximum amount of columns
1314
// that can fit in the terminal
14-
return Math.min(MAXIMUM_SIZE, terminalSize.get().columns)
15+
return Math.min(MAXIMUM_SIZE, getTerminalSize().columns)
1516
}
1617

1718
const getBordersLength = (left, right) => {
@@ -23,9 +24,9 @@ const getBordersLength = (left, right) => {
2324
.value()
2425
}
2526

26-
const renderTables = (...tables) => {
27+
export const renderTables = (...tables) => {
2728
return _
28-
.chain([])
29+
.chain<string[]>([])
2930
.concat(tables)
3031
.invokeMap('toString')
3132
.join('\n')
@@ -130,7 +131,7 @@ const wrapBordersInGray = (chars) => {
130131
})
131132
}
132133

133-
const table = (options = {}) => {
134+
export const table = (options: { type: string, colWidths?: number[], colAligns?: string[], head?: string[], chars?: Record<string, string>, style?: { [key: string]: any } }) => {
134135
const { type } = options
135136
const defaults = utils.mergeOptions({})
136137

@@ -161,7 +162,7 @@ const table = (options = {}) => {
161162
if (bordersLength > 0) {
162163
// redistribute the columns to account for borders on each side...
163164
// and subtract borders size from the largest width cell
164-
const largestCellWidth = _.max(colWidths)
165+
const largestCellWidth = _.max(colWidths) ?? 0
165166

166167
const index = _.indexOf(colWidths, largestCellWidth)
167168

@@ -174,18 +175,22 @@ const table = (options = {}) => {
174175

175176
options.chars = wrapBordersInGray(chars)
176177

177-
return new Table(options)
178+
return new Table({
179+
...options,
180+
colAligns: options.colAligns as HorizontalAlignment[],
181+
})
178182
}
179183

180-
const header = (message, options = {}) => {
184+
export const header = (message: string, options: { color?: string[] | null } = {}) => {
181185
_.defaults(options, {
182186
color: null,
183187
})
184188

185189
message = ` (${chalk.underline.bold(message)})`
186190

187191
if (options.color) {
188-
const colors = [].concat(options.color)
192+
// @ts-expect-error
193+
const colors = <string[]>[].concat(options.color)
189194

190195
message = _.reduce(colors, (memo, color) => {
191196
return chalk[color](memo)
@@ -195,21 +200,9 @@ const header = (message, options = {}) => {
195200
console.log(message) // eslint-disable-line no-console
196201
}
197202

198-
const divider = (symbol, color = 'gray') => {
203+
export const divider = (symbol, color = 'gray') => {
199204
const cols = getMaximumColumns()
200205
const str = symbol.repeat(cols)
201206

202207
console.log(chalk[color](str)) // eslint-disable-line no-console
203208
}
204-
205-
module.exports = {
206-
table,
207-
208-
header,
209-
210-
divider,
211-
212-
renderTables,
213-
214-
getMaximumColumns,
215-
}

packages/server/test/unit/util/terminal_spec.js renamed to packages/server/test/unit/util/terminal_spec.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
require('../../spec_helper')
2-
3-
const snapshot = require('snap-shot-it')
4-
const stripAnsi = require('strip-ansi')
5-
const widestLine = require('widest-line')
6-
const env = require(`../../../lib/util/env`)
7-
const terminal = require(`../../../lib/util/terminal`)
8-
const terminalSize = require(`../../../lib/util/terminal-size`)
1+
import snapshot from 'snap-shot-it'
2+
import stripAnsi from 'strip-ansi'
3+
import widestLine from 'widest-line'
4+
import * as env from '../../../lib/util/env'
5+
import * as terminal from '../../../lib/util/terminal'
6+
import * as terminalSize from '../../../lib/util/terminal-size'
97

108
const sanitizeSnapshot = (str) => {
119
return snapshot(stripAnsi(str))

0 commit comments

Comments
 (0)