Skip to content

Commit 9f33253

Browse files
committed
refactor: use vendored supports-color
1 parent fae77e9 commit 9f33253

File tree

6 files changed

+37
-98
lines changed

6 files changed

+37
-98
lines changed

packages/angular_devkit/core/BUILD

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ ts_library(
2121
"src/**/*_benchmark.ts",
2222
],
2323
),
24-
data = glob(["**/*.json"]),
24+
data = glob(["**/*.json"]) + [
25+
"//packages/angular_devkit/core/third_party/github.com/chalk/supports-color",
26+
],
2527
module_name = "@angular-devkit/core",
2628
module_root = "src/index.d.ts",
2729
# strict_checks = False,

packages/angular_devkit/core/src/terminal/caps.ts

+6-88
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import ReadableStream = NodeJS.ReadableStream;
99
import WriteStream = NodeJS.WriteStream;
1010
import Socket = NodeJS.Socket;
11-
11+
const supportsColor = require('../../third_party/github.com/chalk/supports-color');
1212

1313
/**
1414
* Node specific stuff.
@@ -29,11 +29,6 @@ declare const os: {
2929
};
3030

3131

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-
3732
const streamMap = new WeakMap<{}, StreamCapabilities>();
3833

3934

@@ -72,85 +67,6 @@ export interface StreamCapabilities {
7267
columns: number | null;
7368
}
7469

75-
76-
const ciVars = ['TRAVIS', 'CIRCLECI', 'APPVEYOR', 'GITLAB_CI'];
77-
78-
function _getColorLevel(stream: Socket): 0 | 1 | 2 | 3 {
79-
if ('FORCE_COLOR' in _env) {
80-
if (_env.FORCE_COLOR === '1') {
81-
return 3;
82-
} else if (_env.FORCE_COLOR === '0') {
83-
return 0;
84-
}
85-
}
86-
87-
if (stream && !stream.isTTY && !_env.MSYSTEM) {
88-
return 0;
89-
}
90-
91-
if (_platform.startsWith('win32') && !_env.MSYSTEM) {
92-
// Node.js 7.5.0 is the first version of Node.js to include a patch to
93-
// libuv that enables 256 color output on Windows. Anything earlier and it
94-
// won't work. However, here we target Node.js 8 at minimum as it is an LTS
95-
// release, and Node.js 7 is not. Windows 10 build 10586 is the first Windows
96-
// release that supports 256 colors.
97-
const osRelease = _os.release().split('.');
98-
if (Number(_versions.node.split('.')[0]) >= 8
99-
&& Number(osRelease[0]) >= 10
100-
&& Number(osRelease[2]) >= 10586) {
101-
return 2;
102-
}
103-
104-
return 1;
105-
}
106-
107-
if ('CI' in _env) {
108-
if (ciVars.some(sign => sign in _env) || _env.CI_NAME === 'codeship') {
109-
return 1;
110-
}
111-
112-
return 0;
113-
}
114-
115-
if ('TEAMCITY_VERSION' in _env) {
116-
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(_env.TEAMCITY_VERSION) ? 1 : 0;
117-
}
118-
119-
if ('TERM_PROGRAM' in _env) {
120-
const version = parseInt((_env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
121-
122-
switch (_env.TERM_PROGRAM) {
123-
case 'iTerm.app':
124-
return version >= 3 ? 3 : 2;
125-
case 'Hyper':
126-
return 3;
127-
case 'Apple_Terminal':
128-
return 2;
129-
130-
// No default
131-
}
132-
}
133-
134-
if (/-256(color)?$/i.test(_env.TERM)) {
135-
return 2;
136-
}
137-
138-
if (/^screen|^xterm|^vt100|^rxvt|color|ansi|cygwin|linux/i.test(_env.TERM)) {
139-
return 1;
140-
}
141-
142-
if ('COLORTERM' in _env) {
143-
return 1;
144-
}
145-
146-
if (_env.TERM === 'dumb') {
147-
return 0;
148-
}
149-
150-
return 0;
151-
}
152-
153-
15470
function _getRows() {
15571
return typeof process == 'object' && process.stdout.rows || null;
15672
}
@@ -159,9 +75,11 @@ function _getColumns() {
15975
}
16076

16177

162-
function _createCapabilities(stream: Socket, isTerminalStream: boolean): StreamCapabilities {
163-
const level = _getColorLevel(stream);
164-
78+
function _createCapabilities(
79+
stream: Socket,
80+
isTerminalStream: boolean,
81+
level: 0|1|2|3 = supportsColor.stdout.level,
82+
): StreamCapabilities {
16583
return {
16684
readable: stream.readable,
16785
writable: stream.writable,
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# TODO(alexeagle): move this file to /third_party so Bazel can enforce the licensing
12
licenses(["notice"])
23

34
# Downloaded from: https://github.com/chalk/supports-color/tree/17e9579fec886a1058553b6f7529f05e4f7a90dc
@@ -6,6 +7,9 @@ exports_files(["license"])
67

78
filegroup(
89
name = "supports-color",
9-
srcs = ["index.js"],
10+
srcs = [
11+
"index.js",
12+
"//packages/angular_devkit/core/third_party/github.com/sindresorhus/has-flag",
13+
],
1014
visibility = ["//:__subpackages__"],
1115
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* Added check for `env.MSYSTEM` in windows case, see https://github.com/angular/angular-cli/commit/b8d4e19fc4209ff6a52b6e6a151927f6fe34b60e
2+
* require the locally vendored has-flag rather than npm package
3+
* support browser runtimes by mocking out the process object if it doesn't exist

packages/angular_devkit/core/third_party/github.com/chalk/supports-color/index.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
'use strict';
22
const os = require('os');
3-
const hasFlag = require('has-flag');
3+
// LOCAL MOD: import the dependency from local vendored location
4+
const hasFlag = require('../../sindresorhus/has-flag');
45

5-
const {env} = process;
6+
// LOCAL MOD: support loading this file in a browser
7+
const {env, versions, stdout, stderr, platform} = typeof process == 'object' ? process : {
8+
platform: '',
9+
env: {},
10+
versions: {node: ''},
11+
};
612

713
let forceColor;
814
if (hasFlag('no-color') ||
@@ -54,7 +60,8 @@ function supportsColor(stream) {
5460
return 2;
5561
}
5662

57-
if (stream && !stream.isTTY && forceColor === undefined) {
63+
// LOCAL MOD: support mingw
64+
if (stream && !stream.isTTY && forceColor === undefined && !env.MSYSTEM) {
5865
return 0;
5966
}
6067

@@ -64,7 +71,8 @@ function supportsColor(stream) {
6471
return min;
6572
}
6673

67-
if (process.platform === 'win32') {
74+
// LOCAL MOD: support mingw
75+
if (platform.startsWith('win32') && !env.MSYSTEM) {
6876
// Node.js 7.5.0 is the first version of Node.js to include a patch to
6977
// libuv that enables 256 color output on Windows. Anything earlier and it
7078
// won't work. However, here we target Node.js 8 at minimum as it is an LTS
@@ -73,7 +81,7 @@ function supportsColor(stream) {
7381
// that supports 16m/TrueColor.
7482
const osRelease = os.release().split('.');
7583
if (
76-
Number(process.versions.node.split('.')[0]) >= 8 &&
84+
Number(versions.node.split('.')[0]) >= 8 &&
7785
Number(osRelease[0]) >= 10 &&
7886
Number(osRelease[2]) >= 10586
7987
) {
@@ -133,6 +141,6 @@ function getSupportLevel(stream) {
133141

134142
module.exports = {
135143
supportsColor: getSupportLevel,
136-
stdout: getSupportLevel(process.stdout),
137-
stderr: getSupportLevel(process.stderr)
138-
};
144+
stdout: getSupportLevel(stdout),
145+
stderr: getSupportLevel(stderr)
146+
};

third_party/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# third_party vendored sources in Angular CLI
22

3+
Note, other third_party directories under subpackages currently exist to support the legacy pre-bazel build system.
4+
5+
For example, `/packages/angular_devkit/core/third_party`
6+
37
## TL;DR: don't copy sources into this repo
48

59
All sources in this repo should be authored from scratch by the committer.

0 commit comments

Comments
 (0)