Skip to content

Commit

Permalink
improvement: expose api module to have dedicated instances of ui
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 28, 2020
1 parent c0249c4 commit 98e4063
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 56 deletions.
117 changes: 117 additions & 0 deletions api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* @poppinss/cliui
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import colorSupport from 'color-support'

import { Table } from './src/Table'
import { Logger } from './src/Logger'
import { TaskManager } from './src/Task/Manager'
import { Instructions } from './src/Instructions'
import { MemoryRenderer } from './src/Renderer/Memory'
import { ConsoleRenderer } from './src/Renderer/Console'

export function instantiate(testing: boolean) {
/**
* Is terminal interactive or not. The code is copied from
* https://github.com/sindresorhus/is-interactive/blob/master/index.js.
*
* Yes, we can install it as a dependency, but decided to copy/paste 4
* lines. NO STRONG REASONS BEHIND IT
*/
const isInteractive = Boolean(
process.stdout && process.stdout.isTTY && process.env.TERM !== 'dumb' && !('CI' in process.env)
)

/**
* Whether or not colors are enabled. They are enabled by default,
* unless the terminal doesn't support color. Also "FORCE_COLOR"
* env variable enables them forcefully.
*/
const supportsColors = !!process.env.FORCE_COLOR || colorSupport.level > 0

/**
* The renderer used in the testing mode. One can access it to listen
* for the log messages. Also, the memory renderer only works when
* the "CLI_UI_IS_TESTING" flag is set
*/
const testingRenderer = new MemoryRenderer()

/**
* Console renderer outputs to the console. We do not export it, since one
* cannot do much by having an access to it.
*/
const consoleRenderer = new ConsoleRenderer()

/**
* Logger
*/
const logger = new Logger({ colors: supportsColors, interactive: isInteractive }, testing)
logger.useRenderer(testing ? testingRenderer : consoleRenderer)

/**
* Reference to the instructions block to render a set of lines inside
* a box.
*/
const instructions = () => {
const instructionsInstance = new Instructions({ colors: supportsColors, icons: true }, testing)
instructionsInstance.useRenderer(testing ? testingRenderer : consoleRenderer)
return instructionsInstance
}

/**
* Similar to instructions. But the lines are not prefix with a pointer `>`
*/
const sticker = () => {
const stickerInstance = new Instructions({ colors: supportsColors, icons: false }, testing)
stickerInstance.useRenderer(testing ? testingRenderer : consoleRenderer)
return stickerInstance
}

/**
* Initiates a group of tasks
*/
const tasks = () => {
const manager = new TaskManager({ colors: supportsColors, interactive: isInteractive }, testing)
manager.useRenderer(testing ? testingRenderer : consoleRenderer)
return manager
}

/**
* Initiate tasks in verbose mode
*/
tasks.verbose = () => {
const manager = new TaskManager(
{ colors: supportsColors, interactive: isInteractive, verbose: true },
testing
)
manager.useRenderer(testing ? testingRenderer : consoleRenderer)
return manager
}

/**
* Instantiate a new table
*/
const table = () => {
const tableInstance = new Table({ colors: supportsColors }, testing)
tableInstance.useRenderer(testing ? testingRenderer : consoleRenderer)
return tableInstance
}

return {
table,
tasks,
logger,
sticker,
instructions,
isInteractive,
supportsColors,
consoleRenderer,
testingRenderer,
}
}
65 changes: 11 additions & 54 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@
* file that was distributed with this source code.
*/

import colorSupport from 'color-support'

import { Table } from './src/Table'
import { Logger } from './src/Logger'
import { TaskManager } from './src/Task/Manager'
import { Instructions } from './src/Instructions'
import { MemoryRenderer } from './src/Renderer/Memory'
import { ConsoleRenderer } from './src/Renderer/Console'
import { instantiate } from './api'
const ui = instantiate(!!process.env.CLI_UI_IS_TESTING)

/**
* Is terminal interactive or not. The code is copied from
Expand All @@ -23,87 +17,50 @@ import { ConsoleRenderer } from './src/Renderer/Console'
* Yes, we can install it as a dependency, but decided to copy/paste 4
* lines. NO STRONG REASONS BEHIND IT
*/
export const isInteractive = Boolean(
process.stdout && process.stdout.isTTY && process.env.TERM !== 'dumb' && !('CI' in process.env)
)
export const isInteractive = ui.isInteractive

/**
* Whether or not colors are enabled. They are enabled by default,
* unless the terminal doesn't support color. Also "FORCE_COLOR"
* env variable enables them forcefully.
*/
export const supportsColors = !!process.env.FORCE_COLOR || colorSupport.level > 0

/**
* A boolean to know, if we are in testing mode or not. During tests
* we will use the memory renderer.
*/
const testing = !!process.env.CLI_UI_IS_TESTING
export const supportsColors = ui.supportsColors

/**
* The renderer used in the testing mode. One can access it to listen
* for the log messages. Also, the memory renderer only works when
* the "CLI_UI_IS_TESTING" flag is set
*/
export const testingRenderer = new MemoryRenderer()
export const testingRenderer = ui.testingRenderer

/**
* Console renderer outputs to the console. We do not export it, since one
* cannot do much by having an access to it.
*/
const consoleRenderer = new ConsoleRenderer()
export const consoleRenderer = ui.consoleRenderer

/**
* Logger
*/
export const logger = new Logger({ colors: supportsColors, interactive: isInteractive }, testing)
logger.useRenderer(testing ? testingRenderer : consoleRenderer)
export const logger = ui.logger

/**
* Reference to the instructions block to render a set of lines inside
* a box.
*/
export const instructions = () => {
const instructionsInstance = new Instructions({ colors: supportsColors, icons: true }, testing)
instructionsInstance.useRenderer(testing ? testingRenderer : consoleRenderer)
return instructionsInstance
}
export const instructions = ui.instructions

/**
* Similar to instructions. But the lines are not prefix with a pointer `>`
*/
export const sticker = () => {
const stickerInstance = new Instructions({ colors: supportsColors, icons: false }, testing)
stickerInstance.useRenderer(testing ? testingRenderer : consoleRenderer)
return stickerInstance
}
export const sticker = ui.sticker

/**
* Initiates a group of tasks
*/
export const tasks = () => {
const manager = new TaskManager({ colors: supportsColors, interactive: isInteractive }, testing)
manager.useRenderer(testing ? testingRenderer : consoleRenderer)
return manager
}

/**
* Initiate tasks in verbose mode
*/
tasks.verbose = () => {
const manager = new TaskManager(
{ colors: supportsColors, interactive: isInteractive, verbose: true },
testing
)
manager.useRenderer(testing ? testingRenderer : consoleRenderer)
return manager
}
export const tasks = ui.tasks

/**
* Instantiate a new table
*/
export const table = () => {
const tableInstance = new Table({ colors: supportsColors }, testing)
tableInstance.useRenderer(testing ? testingRenderer : consoleRenderer)
return tableInstance
}
export const table = ui.table
2 changes: 1 addition & 1 deletion npm-audit.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h5 class="card-title">
<div class="card">
<div class="card-body">
<h5 class="card-title">
September 27th 2020, 6:09:02 pm
September 28th 2020, 6:43:22 am
</h5>
<p class="card-text">Last updated</p>
</div>
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"files": [
"build/src",
"build/index.d.ts",
"build/index.js"
"build/index.js",
"build/api.d.ts",
"build/api.js"
],
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
Expand Down

0 comments on commit 98e4063

Please sign in to comment.