refactor: use vendored supports-color

This commit is contained in:
Alex Eagle 2019-02-14 12:05:43 -08:00 committed by Alex Eagle
parent fae77e9257
commit 9f33253f36
6 changed files with 37 additions and 98 deletions

View File

@ -21,7 +21,9 @@ ts_library(
"src/**/*_benchmark.ts",
],
),
data = glob(["**/*.json"]),
data = glob(["**/*.json"]) + [
"//packages/angular_devkit/core/third_party/github.com/chalk/supports-color",
],
module_name = "@angular-devkit/core",
module_root = "src/index.d.ts",
# strict_checks = False,

View File

@ -8,7 +8,7 @@
import ReadableStream = NodeJS.ReadableStream;
import WriteStream = NodeJS.WriteStream;
import Socket = NodeJS.Socket;
const supportsColor = require('../../third_party/github.com/chalk/supports-color');
/**
* Node specific stuff.
@ -29,11 +29,6 @@ declare const os: {
};
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>();
@ -72,85 +67,6 @@ export interface StreamCapabilities {
columns: number | null;
}
const ciVars = ['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'];
function _getColorLevel(stream: Socket): 0 | 1 | 2 | 3 {
if ('FORCE_COLOR' in _env) {
if (_env.FORCE_COLOR === '1') {
return 3;
} else if (_env.FORCE_COLOR === '0') {
return 0;
}
}
if (stream && !stream.isTTY && !_env.MSYSTEM) {
return 0;
}
if (_platform.startsWith('win32') && !_env.MSYSTEM) {
// 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 typeof process == 'object' && process.stdout.rows || null;
}
@ -159,9 +75,11 @@ function _getColumns() {
}
function _createCapabilities(stream: Socket, isTerminalStream: boolean): StreamCapabilities {
const level = _getColorLevel(stream);
function _createCapabilities(
stream: Socket,
isTerminalStream: boolean,
level: 0|1|2|3 = supportsColor.stdout.level,
): StreamCapabilities {
return {
readable: stream.readable,
writable: stream.writable,

View File

@ -1,3 +1,4 @@
# TODO(alexeagle): move this file to /third_party so Bazel can enforce the licensing
licenses(["notice"])
# Downloaded from: https://github.com/chalk/supports-color/tree/17e9579fec886a1058553b6f7529f05e4f7a90dc
@ -6,6 +7,9 @@ exports_files(["license"])
filegroup(
name = "supports-color",
srcs = ["index.js"],
srcs = [
"index.js",
"//packages/angular_devkit/core/third_party/github.com/sindresorhus/has-flag",
],
visibility = ["//:__subpackages__"],
)

View File

@ -0,0 +1,3 @@
* Added check for `env.MSYSTEM` in windows case, see https://github.com/angular/angular-cli/commit/b8d4e19fc4209ff6a52b6e6a151927f6fe34b60e
* require the locally vendored has-flag rather than npm package
* support browser runtimes by mocking out the process object if it doesn't exist

View File

@ -1,8 +1,14 @@
'use strict';
const os = require('os');
const hasFlag = require('has-flag');
// LOCAL MOD: import the dependency from local vendored location
const hasFlag = require('../../sindresorhus/has-flag');
const {env} = process;
// LOCAL MOD: support loading this file in a browser
const {env, versions, stdout, stderr, platform} = typeof process == 'object' ? process : {
platform: '',
env: {},
versions: {node: ''},
};
let forceColor;
if (hasFlag('no-color') ||
@ -54,7 +60,8 @@ function supportsColor(stream) {
return 2;
}
if (stream && !stream.isTTY && forceColor === undefined) {
// LOCAL MOD: support mingw
if (stream && !stream.isTTY && forceColor === undefined && !env.MSYSTEM) {
return 0;
}
@ -64,7 +71,8 @@ function supportsColor(stream) {
return min;
}
if (process.platform === 'win32') {
// LOCAL MOD: support mingw
if (platform.startsWith('win32') && !env.MSYSTEM) {
// 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
@ -73,7 +81,7 @@ function supportsColor(stream) {
// that supports 16m/TrueColor.
const osRelease = os.release().split('.');
if (
Number(process.versions.node.split('.')[0]) >= 8 &&
Number(versions.node.split('.')[0]) >= 8 &&
Number(osRelease[0]) >= 10 &&
Number(osRelease[2]) >= 10586
) {
@ -133,6 +141,6 @@ function getSupportLevel(stream) {
module.exports = {
supportsColor: getSupportLevel,
stdout: getSupportLevel(process.stdout),
stderr: getSupportLevel(process.stderr)
};
stdout: getSupportLevel(stdout),
stderr: getSupportLevel(stderr)
};

View File

@ -1,5 +1,9 @@
# third_party vendored sources in Angular CLI
Note, other third_party directories under subpackages currently exist to support the legacy pre-bazel build system.
For example, `/packages/angular_devkit/core/third_party`
## TL;DR: don't copy sources into this repo
All sources in this repo should be authored from scratch by the committer.