This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
feat(@angular-devkit/core): add support for color and no-color #245
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google Inc. All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.io/license | ||
| */ | ||
| import ReadableStream = NodeJS.ReadableStream; | ||
| import WriteStream = NodeJS.WriteStream; | ||
| import Socket = NodeJS.Socket; | ||
|
|
||
|
|
||
| /** | ||
| * Node specific stuff. | ||
| */ | ||
| declare const process: { | ||
| env: { [name: string]: string }; | ||
| platform: string; | ||
| versions: { | ||
| node: string; | ||
| }; | ||
|
|
||
| stdin: ReadableStream; | ||
| stdout: WriteStream; | ||
| stderr: WriteStream; | ||
| }; | ||
| declare const os: { | ||
| release: () => string; | ||
| }; | ||
|
|
||
|
|
||
| const _env = (typeof process == 'object' && process.env) || {}; | ||
| const _platform = (typeof process == 'object' && process.platform) || ''; | ||
| const _versions = (typeof process == 'object' && process.versions) || { node: '' }; | ||
| const _os = (typeof os == 'object' && os) || { release: () => '' }; | ||
|
|
||
| const streamMap = new WeakMap<{}, StreamCapabilities>(); | ||
|
|
||
|
|
||
| export interface StreamCapabilities { | ||
| readable: boolean; | ||
| writable: boolean; | ||
|
|
||
| /** | ||
| * Supports text. This should be true for any streams. | ||
| */ | ||
| text: boolean; | ||
|
|
||
| /** | ||
| * Supports colors (16 colors). | ||
| */ | ||
| colors: boolean; | ||
|
|
||
| /** | ||
| * Supports 256 colors. | ||
| */ | ||
| color256: boolean; | ||
|
|
||
| /** | ||
| * Supports 16 millions (3x8-bit channels) colors. | ||
| */ | ||
| color16m: boolean; | ||
|
|
||
| /** | ||
| * Height of the terminal. If the stream is not tied to a terminal, will be null. | ||
| */ | ||
| rows: number | null; | ||
|
|
||
| /** | ||
| * Width of the terminal. If the stream is not tied to a terminal, will be null. | ||
| */ | ||
| columns: number | null; | ||
| } | ||
|
|
||
|
|
||
| const ciVars = ['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI']; | ||
|
|
||
|
|
||
| function _getColorLevel(stream: Socket): number { | ||
| if (stream && !stream.isTTY) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (_platform.startsWith('win32')) { | ||
| // Node.js 7.5.0 is the first version of Node.js to include a patch to | ||
| // libuv that enables 256 color output on Windows. Anything earlier and it | ||
| // won't work. However, here we target Node.js 8 at minimum as it is an LTS | ||
| // release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows | ||
| // release that supports 256 colors. | ||
| const osRelease = _os.release().split('.'); | ||
| if (Number(_versions.node.split('.')[0]) >= 8 | ||
| && Number(osRelease[0]) >= 10 | ||
| && Number(osRelease[2]) >= 10586) { | ||
| return 2; | ||
| } | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| if ('CI' in _env) { | ||
| if (ciVars.some(sign => sign in _env) || _env.CI_NAME === 'codeship') { | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| if ('TEAMCITY_VERSION' in _env) { | ||
| return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(_env.TEAMCITY_VERSION) ? 1 : 0; | ||
| } | ||
|
|
||
| if ('TERM_PROGRAM' in _env) { | ||
| const version = parseInt((_env.TERM_PROGRAM_VERSION || '').split('.')[0], 10); | ||
|
|
||
| switch (_env.TERM_PROGRAM) { | ||
| case 'iTerm.app': | ||
| return version >= 3 ? 3 : 2; | ||
| case 'Hyper': | ||
| return 3; | ||
| case 'Apple_Terminal': | ||
| return 2; | ||
|
|
||
| // No default | ||
| } | ||
| } | ||
|
|
||
| if (/-256(color)?$/i.test(_env.TERM)) { | ||
| return 2; | ||
| } | ||
|
|
||
| if (/^screen|^xterm|^vt100|^rxvt|color|ansi|cygwin|linux/i.test(_env.TERM)) { | ||
| return 1; | ||
| } | ||
|
|
||
| if ('COLORTERM' in _env) { | ||
| return 1; | ||
| } | ||
|
|
||
| if (_env.TERM === 'dumb') { | ||
| return 0; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
|
|
||
| function _getRows() { | ||
| return process.stdout.rows || null; | ||
| } | ||
| function _getColumns() { | ||
| return process.stdout.columns || null; | ||
| } | ||
|
|
||
|
|
||
| function _createCapabilities(stream: Socket, isTerminalStream: boolean): StreamCapabilities { | ||
| const level = _getColorLevel(stream); | ||
|
|
||
| return { | ||
| readable: stream.readable, | ||
| writable: stream.writable, | ||
| text: true, | ||
|
|
||
| colors: level > 0, | ||
| color256: level > 1, | ||
| color16m: level > 2, | ||
|
|
||
| rows: isTerminalStream ? _getRows() : null, | ||
| columns: isTerminalStream ? _getColumns() : null, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| export function getCapabilities( | ||
| stream: Socket, | ||
| isTerminalStream = !!stream.isTTY, | ||
| ): StreamCapabilities { | ||
| let maybeCaps = streamMap.get(stream); | ||
| if (!maybeCaps) { | ||
| maybeCaps = _createCapabilities(stream, isTerminalStream); | ||
| streamMap.set(stream, maybeCaps); | ||
| } | ||
|
|
||
| return maybeCaps; | ||
| } | ||
|
|
||
|
|
||
| export const stdin = getCapabilities(process.stdin as Socket); | ||
| export const stdout = getCapabilities(process.stdout); | ||
| export const stderr = getCapabilities(process.stderr); | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google Inc. All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.io/license | ||
| */ | ||
| import * as caps from './caps'; | ||
| import * as ansi from './colors'; | ||
|
|
||
|
|
||
| export const reset = caps.stdout.colors ? ansi.reset : (x: string) => x; | ||
| export const bold = caps.stdout.colors ? ansi.bold : (x: string) => x; | ||
| export const dim = caps.stdout.colors ? ansi.dim : (x: string) => x; | ||
| export const italic = caps.stdout.colors ? ansi.italic : (x: string) => x; | ||
| export const underline = caps.stdout.colors ? ansi.underline : (x: string) => x; | ||
| export const inverse = caps.stdout.colors ? ansi.inverse : (x: string) => x; | ||
| export const hidden = caps.stdout.colors ? ansi.hidden : (x: string) => x; | ||
| export const strikethrough = caps.stdout.colors ? ansi.strikethrough : (x: string) => x; | ||
|
|
||
| export const black = caps.stdout.colors ? ansi.black : (x: string) => x; | ||
| export const red = caps.stdout.colors ? ansi.red : (x: string) => x; | ||
| export const green = caps.stdout.colors ? ansi.green : (x: string) => x; | ||
| export const yellow = caps.stdout.colors ? ansi.yellow : (x: string) => x; | ||
| export const blue = caps.stdout.colors ? ansi.blue : (x: string) => x; | ||
| export const magenta = caps.stdout.colors ? ansi.magenta : (x: string) => x; | ||
| export const cyan = caps.stdout.colors ? ansi.cyan : (x: string) => x; | ||
| export const white = caps.stdout.colors ? ansi.white : (x: string) => x; | ||
| export const grey = caps.stdout.colors ? ansi.gray : (x: string) => x; | ||
| export const gray = caps.stdout.colors ? ansi.gray : (x: string) => x; | ||
|
|
||
| export const bgBlack = caps.stdout.colors ? ansi.bgBlack : (x: string) => x; | ||
| export const bgRed = caps.stdout.colors ? ansi.bgRed : (x: string) => x; | ||
| export const bgGreen = caps.stdout.colors ? ansi.bgGreen : (x: string) => x; | ||
| export const bgYellow = caps.stdout.colors ? ansi.bgYellow : (x: string) => x; | ||
| export const bgBlue = caps.stdout.colors ? ansi.bgBlue : (x: string) => x; | ||
| export const bgMagenta = caps.stdout.colors ? ansi.bgMagenta : (x: string) => x; | ||
| export const bgCyan = caps.stdout.colors ? ansi.bgCyan : (x: string) => x; | ||
| export const bgWhite = caps.stdout.colors ? ansi.bgWhite : (x: string) => x; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use
stream.isTTYinstead of passing inisTerminalStream: https://nodejs.org/api/tty.html.We had a PR for doing that in the CLI recently angular/angular-cli#8501
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've set the default to it, but technically this could be used outside of Node. I've let that other PR in, that's actually nice to have.