UNPKG

8.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.json = exports.prettyList = exports.pretty = exports.applyColor = exports.applyStyle = exports.tuple = exports.supportsHyperlinks = exports.supportsColor = exports.Style = exports.Type = void 0;
4const tslib_1 = require("tslib");
5const fslib_1 = require("@yarnpkg/fslib");
6const chalk_1 = tslib_1.__importDefault(require("chalk"));
7const miscUtils = tslib_1.__importStar(require("./miscUtils"));
8const structUtils = tslib_1.__importStar(require("./structUtils"));
9var Type;
10(function (Type) {
11 Type["NO_HINT"] = "NO_HINT";
12 Type["NULL"] = "NULL";
13 Type["SCOPE"] = "SCOPE";
14 Type["NAME"] = "NAME";
15 Type["RANGE"] = "RANGE";
16 Type["REFERENCE"] = "REFERENCE";
17 Type["NUMBER"] = "NUMBER";
18 Type["PATH"] = "PATH";
19 Type["URL"] = "URL";
20 Type["ADDED"] = "ADDED";
21 Type["REMOVED"] = "REMOVED";
22 Type["CODE"] = "CODE";
23 Type["DURATION"] = "DURATION";
24 Type["SIZE"] = "SIZE";
25 Type["IDENT"] = "IDENT";
26 Type["DESCRIPTOR"] = "DESCRIPTOR";
27 Type["LOCATOR"] = "LOCATOR";
28 Type["RESOLUTION"] = "RESOLUTION";
29 Type["DEPENDENT"] = "DEPENDENT";
30})(Type = exports.Type || (exports.Type = {}));
31var Style;
32(function (Style) {
33 Style[Style["BOLD"] = 2] = "BOLD";
34})(Style = exports.Style || (exports.Style = {}));
35const chalkOptions = process.env.GITHUB_ACTIONS
36 ? { level: 2 }
37 : chalk_1.default.supportsColor
38 ? { level: chalk_1.default.supportsColor.level }
39 : { level: 0 };
40exports.supportsColor = chalkOptions.level !== 0;
41exports.supportsHyperlinks = exports.supportsColor && !process.env.GITHUB_ACTIONS;
42const chalkInstance = new chalk_1.default.Instance(chalkOptions);
43const colors = new Map([
44 [Type.NO_HINT, null],
45 [Type.NULL, [`#a853b5`, 129]],
46 [Type.SCOPE, [`#d75f00`, 166]],
47 [Type.NAME, [`#d7875f`, 173]],
48 [Type.RANGE, [`#00afaf`, 37]],
49 [Type.REFERENCE, [`#87afff`, 111]],
50 [Type.NUMBER, [`#ffd700`, 220]],
51 [Type.PATH, [`#d75fd7`, 170]],
52 [Type.URL, [`#d75fd7`, 170]],
53 [Type.ADDED, [`#5faf00`, 70]],
54 [Type.REMOVED, [`#d70000`, 160]],
55 [Type.CODE, [`#87afff`, 111]],
56 [Type.SIZE, [`#ffd700`, 220]],
57]);
58// Just to make sure that the individual fields of the transform map have
59// compatible parameter types, without upcasting the map to a too generic type
60//
61// We also take the opportunity to downcast the configuration into `any`,
62// otherwise TypeScript will detect a circular reference and won't allow us to
63// properly type the `format` method from Configuration. Since transforms are
64// internal to this file, it should be fine.
65const validateTransform = (spec) => spec;
66const transforms = {
67 [Type.NUMBER]: validateTransform({
68 pretty: (configuration, value) => {
69 return `${value}`;
70 },
71 json: (value) => {
72 return value;
73 },
74 }),
75 [Type.IDENT]: validateTransform({
76 pretty: (configuration, ident) => {
77 return structUtils.prettyIdent(configuration, ident);
78 },
79 json: (ident) => {
80 return structUtils.stringifyIdent(ident);
81 },
82 }),
83 [Type.LOCATOR]: validateTransform({
84 pretty: (configuration, locator) => {
85 return structUtils.prettyLocator(configuration, locator);
86 },
87 json: (locator) => {
88 return structUtils.stringifyLocator(locator);
89 },
90 }),
91 [Type.DESCRIPTOR]: validateTransform({
92 pretty: (configuration, descriptor) => {
93 return structUtils.prettyDescriptor(configuration, descriptor);
94 },
95 json: (descriptor) => {
96 return structUtils.stringifyDescriptor(descriptor);
97 },
98 }),
99 [Type.RESOLUTION]: validateTransform({
100 pretty: (configuration, { descriptor, locator }) => {
101 return structUtils.prettyResolution(configuration, descriptor, locator);
102 },
103 json: ({ descriptor, locator }) => {
104 return {
105 descriptor: structUtils.stringifyDescriptor(descriptor),
106 locator: locator !== null
107 ? structUtils.stringifyLocator(locator)
108 : null,
109 };
110 },
111 }),
112 [Type.DEPENDENT]: validateTransform({
113 pretty: (configuration, { locator, descriptor }) => {
114 return structUtils.prettyDependent(configuration, locator, descriptor);
115 },
116 json: ({ locator, descriptor }) => {
117 return {
118 locator: structUtils.stringifyLocator(locator),
119 descriptor: structUtils.stringifyDescriptor(descriptor),
120 };
121 },
122 }),
123 [Type.DURATION]: validateTransform({
124 pretty: (configuration, duration) => {
125 if (duration > 1000 * 60) {
126 const minutes = Math.floor(duration / 1000 / 60);
127 const seconds = Math.ceil((duration - minutes * 60 * 1000) / 1000);
128 return seconds === 0 ? `${minutes}m` : `${minutes}m ${seconds}s`;
129 }
130 else {
131 const seconds = Math.floor(duration / 1000);
132 const milliseconds = duration - seconds * 1000;
133 return milliseconds === 0 ? `${seconds}s` : `${seconds}s ${milliseconds}ms`;
134 }
135 },
136 json: (duration) => {
137 return duration;
138 },
139 }),
140 [Type.SIZE]: validateTransform({
141 pretty: (configuration, size) => {
142 const thresholds = [`KB`, `MB`, `GB`, `TB`];
143 let power = thresholds.length;
144 while (power > 1 && size < 1024 ** power)
145 power -= 1;
146 const factor = 1024 ** power;
147 const value = Math.floor(size * 100 / factor) / 100;
148 return applyColor(configuration, `${value} ${thresholds[power - 1]}`, Type.NUMBER);
149 },
150 json: (size) => {
151 return size;
152 },
153 }),
154 [Type.PATH]: validateTransform({
155 pretty: (configuration, filePath) => {
156 return applyColor(configuration, fslib_1.npath.fromPortablePath(filePath), Type.PATH);
157 },
158 json: (filePath) => {
159 return fslib_1.npath.fromPortablePath(filePath);
160 },
161 }),
162};
163function tuple(formatType, value) {
164 return [value, formatType];
165}
166exports.tuple = tuple;
167function applyStyle(configuration, text, flags) {
168 if (!configuration.get(`enableColors`))
169 return text;
170 if (flags & Style.BOLD)
171 text = chalk_1.default.bold(text);
172 return text;
173}
174exports.applyStyle = applyStyle;
175function applyColor(configuration, value, formatType) {
176 if (!configuration.get(`enableColors`))
177 return value;
178 const colorSpec = colors.get(formatType);
179 if (colorSpec === null)
180 return value;
181 const color = typeof colorSpec === `undefined`
182 ? formatType
183 : chalkOptions.level >= 3
184 ? colorSpec[0]
185 : colorSpec[1];
186 const fn = typeof color === `number`
187 ? chalkInstance.ansi256(color)
188 : color.startsWith(`#`)
189 ? chalkInstance.hex(color)
190 : chalkInstance[color];
191 if (typeof fn !== `function`)
192 throw new Error(`Invalid format type ${color}`);
193 return fn(value);
194}
195exports.applyColor = applyColor;
196function pretty(configuration, value, formatType) {
197 if (value === null)
198 return applyColor(configuration, `null`, Type.NULL);
199 if (Object.prototype.hasOwnProperty.call(transforms, formatType)) {
200 const transform = transforms[formatType];
201 const typedTransform = transform;
202 return typedTransform.pretty(configuration, value);
203 }
204 if (typeof value !== `string`)
205 throw new Error(`Assertion failed: Expected the value to be a string, got ${typeof value}`);
206 return applyColor(configuration, value, formatType);
207}
208exports.pretty = pretty;
209function prettyList(configuration, values, formatType, { separator = `, ` } = {}) {
210 return [...values].map(value => pretty(configuration, value, formatType)).join(separator);
211}
212exports.prettyList = prettyList;
213function json(value, formatType) {
214 if (value === null)
215 return null;
216 if (Object.prototype.hasOwnProperty.call(transforms, formatType)) {
217 miscUtils.overrideType(formatType);
218 return transforms[formatType].json(value);
219 }
220 if (typeof value !== `string`)
221 throw new Error(`Assertion failed: Expected the value to be a string, got ${typeof value}`);
222 return value;
223}
224exports.json = json;