|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import ReadableStream = NodeJS.ReadableStream; |
| 9 | +import WriteStream = NodeJS.WriteStream; |
| 10 | +import Socket = NodeJS.Socket; |
| 11 | + |
| 12 | + |
| 13 | +/** |
| 14 | + * Node specific stuff. |
| 15 | + */ |
| 16 | +declare const process: { |
| 17 | + env: { [name: string]: string }; |
| 18 | + platform: string; |
| 19 | + versions: { |
| 20 | + node: string; |
| 21 | + }; |
| 22 | + |
| 23 | + stdin: ReadableStream; |
| 24 | + stdout: WriteStream; |
| 25 | + stderr: WriteStream; |
| 26 | +}; |
| 27 | +declare const os: { |
| 28 | + release: () => string; |
| 29 | +}; |
| 30 | + |
| 31 | + |
| 32 | +const _env = (typeof process == 'object' && process.env) || {}; |
| 33 | +const _platform = (typeof process == 'object' && process.platform) || ''; |
| 34 | +const _versions = (typeof process == 'object' && process.versions) || { node: '' }; |
| 35 | +const _os = (typeof os == 'object' && os) || { release: () => '' }; |
| 36 | + |
| 37 | +const streamMap = new WeakMap<{}, StreamCapabilities>(); |
| 38 | + |
| 39 | + |
| 40 | +export interface StreamCapabilities { |
| 41 | + readable: boolean; |
| 42 | + writable: boolean; |
| 43 | + |
| 44 | + /** |
| 45 | + * Supports text. This should be true for any streams. |
| 46 | + */ |
| 47 | + text: boolean; |
| 48 | + |
| 49 | + /** |
| 50 | + * Supports colors (16 colors). |
| 51 | + */ |
| 52 | + colors: boolean; |
| 53 | + |
| 54 | + /** |
| 55 | + * Supports 256 colors. |
| 56 | + */ |
| 57 | + color256: boolean; |
| 58 | + |
| 59 | + /** |
| 60 | + * Supports 16 millions (3x8-bit channels) colors. |
| 61 | + */ |
| 62 | + color16m: boolean; |
| 63 | + |
| 64 | + /** |
| 65 | + * Height of the terminal. If the stream is not tied to a terminal, will be null. |
| 66 | + */ |
| 67 | + rows: number | null; |
| 68 | + |
| 69 | + /** |
| 70 | + * Width of the terminal. If the stream is not tied to a terminal, will be null. |
| 71 | + */ |
| 72 | + columns: number | null; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +const ciVars = ['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI']; |
| 77 | + |
| 78 | + |
| 79 | +function _getColorLevel(stream: Socket): number { |
| 80 | + if (stream && !stream.isTTY) { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + if (_platform.startsWith('win32')) { |
| 85 | + // Node.js 7.5.0 is the first version of Node.js to include a patch to |
| 86 | + // libuv that enables 256 color output on Windows. Anything earlier and it |
| 87 | + // won't work. However, here we target Node.js 8 at minimum as it is an LTS |
| 88 | + // release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows |
| 89 | + // release that supports 256 colors. |
| 90 | + const osRelease = _os.release().split('.'); |
| 91 | + if (Number(_versions.node.split('.')[0]) >= 8 |
| 92 | + && Number(osRelease[0]) >= 10 |
| 93 | + && Number(osRelease[2]) >= 10586) { |
| 94 | + return 2; |
| 95 | + } |
| 96 | + |
| 97 | + return 1; |
| 98 | + } |
| 99 | + |
| 100 | + if ('CI' in _env) { |
| 101 | + if (ciVars.some(sign => sign in _env) || _env.CI_NAME === 'codeship') { |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + if ('TEAMCITY_VERSION' in _env) { |
| 109 | + return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(_env.TEAMCITY_VERSION) ? 1 : 0; |
| 110 | + } |
| 111 | + |
| 112 | + if ('TERM_PROGRAM' in _env) { |
| 113 | + const version = parseInt((_env.TERM_PROGRAM_VERSION || '').split('.')[0], 10); |
| 114 | + |
| 115 | + switch (_env.TERM_PROGRAM) { |
| 116 | + case 'iTerm.app': |
| 117 | + return version >= 3 ? 3 : 2; |
| 118 | + case 'Hyper': |
| 119 | + return 3; |
| 120 | + case 'Apple_Terminal': |
| 121 | + return 2; |
| 122 | + |
| 123 | + // No default |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (/-256(color)?$/i.test(_env.TERM)) { |
| 128 | + return 2; |
| 129 | + } |
| 130 | + |
| 131 | + if (/^screen|^xterm|^vt100|^rxvt|color|ansi|cygwin|linux/i.test(_env.TERM)) { |
| 132 | + return 1; |
| 133 | + } |
| 134 | + |
| 135 | + if ('COLORTERM' in _env) { |
| 136 | + return 1; |
| 137 | + } |
| 138 | + |
| 139 | + if (_env.TERM === 'dumb') { |
| 140 | + return 0; |
| 141 | + } |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +function _getRows() { |
| 148 | + return process.stdout.rows || null; |
| 149 | +} |
| 150 | +function _getColumns() { |
| 151 | + return process.stdout.columns || null; |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +function _createCapabilities(stream: Socket, isTerminalStream: boolean): StreamCapabilities { |
| 156 | + const level = _getColorLevel(stream); |
| 157 | + |
| 158 | + return { |
| 159 | + readable: stream.readable, |
| 160 | + writable: stream.writable, |
| 161 | + text: true, |
| 162 | + |
| 163 | + colors: level > 0, |
| 164 | + color256: level > 1, |
| 165 | + color16m: level > 2, |
| 166 | + |
| 167 | + rows: isTerminalStream ? _getRows() : null, |
| 168 | + columns: isTerminalStream ? _getColumns() : null, |
| 169 | + }; |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +export function getCapabilities( |
| 174 | + stream: Socket, |
| 175 | + isTerminalStream = !!stream.isTTY, |
| 176 | +): StreamCapabilities { |
| 177 | + let maybeCaps = streamMap.get(stream); |
| 178 | + if (!maybeCaps) { |
| 179 | + maybeCaps = _createCapabilities(stream, isTerminalStream); |
| 180 | + streamMap.set(stream, maybeCaps); |
| 181 | + } |
| 182 | + |
| 183 | + return maybeCaps; |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +export const stdin = getCapabilities(process.stdin as Socket); |
| 188 | +export const stdout = getCapabilities(process.stdout); |
| 189 | +export const stderr = getCapabilities(process.stderr); |
0 commit comments