UNPKG

5.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.stripAnsi = exports.parseRepository = exports.parsePerson = exports.logDiagnostic = exports._formatDiagnostic = exports.formatDiagnostic = exports.diagnosticsLogger = exports.JSII_DIAGNOSTICS_CODE = exports.DIAGNOSTICS = void 0;
4const log4js = require("log4js");
5const ts = require("typescript");
6const jsii_diagnostic_1 = require("./jsii-diagnostic");
7/**
8 * Name of the logger for diagnostics information
9 */
10exports.DIAGNOSTICS = 'diagnostics';
11/**
12 * Diagnostic code for JSII-generated messages.
13 */
14exports.JSII_DIAGNOSTICS_CODE = 9999;
15/**
16 * Obtains the relevant logger to be used for a given diagnostic message.
17 *
18 * @param logger the ``log4js.Logger`` to use for emitting the message.
19 * @param diagnostic the message for which a logger is requested.
20 *
21 * @returns a logger method of the ``logger`` for the appropriate level.
22 */
23function diagnosticsLogger(logger, diagnostic) {
24 switch (diagnostic.category) {
25 case ts.DiagnosticCategory.Error:
26 if (!logger.isErrorEnabled()) {
27 return undefined;
28 }
29 return logger.error.bind(logger);
30 case ts.DiagnosticCategory.Warning:
31 if (!logger.isWarnEnabled()) {
32 return undefined;
33 }
34 return logger.warn.bind(logger);
35 case ts.DiagnosticCategory.Message:
36 if (!logger.isDebugEnabled()) {
37 return undefined;
38 }
39 return logger.debug.bind(logger);
40 case ts.DiagnosticCategory.Suggestion:
41 default:
42 if (!logger.isTraceEnabled()) {
43 return undefined;
44 }
45 return logger.trace.bind(logger);
46 }
47}
48exports.diagnosticsLogger = diagnosticsLogger;
49/**
50 * Formats a diagnostic message with color and context, if possible.
51 *
52 * @param diagnostic the diagnostic message ot be formatted.
53 * @param projectRoot the root of the TypeScript project.
54 *
55 * @returns a formatted string.
56 */
57function formatDiagnostic(diagnostic, projectRoot) {
58 if (jsii_diagnostic_1.JsiiDiagnostic.isJsiiDiagnostic(diagnostic)) {
59 // Ensure we leverage pre-rendered diagnostics where available.
60 return diagnostic.format(projectRoot);
61 }
62 return _formatDiagnostic(diagnostic, projectRoot);
63}
64exports.formatDiagnostic = formatDiagnostic;
65/**
66 * Formats a diagnostic message with color and context, if possible. Users
67 * should use `formatDiagnostic` instead, as this implementation is intended for
68 * internal usafe only.
69 *
70 * @param diagnostic the diagnostic message ot be formatted.
71 * @param projectRoot the root of the TypeScript project.
72 *
73 * @returns a formatted string.
74 */
75function _formatDiagnostic(diagnostic, projectRoot) {
76 const formatDiagnosticsHost = {
77 getCurrentDirectory: () => projectRoot,
78 getCanonicalFileName: (fileName) => fileName,
79 getNewLine: () => ts.sys.newLine,
80 };
81 const message = diagnostic.file != null
82 ? ts.formatDiagnosticsWithColorAndContext([diagnostic], formatDiagnosticsHost)
83 : ts.formatDiagnostic(diagnostic, formatDiagnosticsHost);
84 if (!jsii_diagnostic_1.JsiiDiagnostic.isJsiiDiagnostic(diagnostic)) {
85 return message;
86 }
87 // This is our own diagnostics, so we'll format appropriately (replacing TS#### with JSII####).
88 return message.replace(` TS${exports.JSII_DIAGNOSTICS_CODE}: `, ` JSII${diagnostic.jsiiCode}: `);
89}
90exports._formatDiagnostic = _formatDiagnostic;
91function logDiagnostic(diagnostic, projectRoot) {
92 const logFunc = diagnosticsLogger(log4js.getLogger(exports.DIAGNOSTICS), diagnostic);
93 if (!logFunc) {
94 return;
95 }
96 logFunc(formatDiagnostic(diagnostic, projectRoot).trim());
97}
98exports.logDiagnostic = logDiagnostic;
99const PERSON_REGEX = /^\s*(.+?)(?:\s*<([^>]+)>)?(?:\s*\(([^)]+)\))?\s*$/;
100/**
101 * Parses a string-formatted person entry from `package.json`.
102 * @param value the string-formatted person entry.
103 *
104 * @example
105 * parsePerson("Barney Rubble <b@rubble.com> (http://barnyrubble.tumblr.com/)");
106 * // => { name: "Barney Rubble", email: "b@rubble.com", url: "http://barnyrubble.tumblr.com/" }
107 */
108function parsePerson(value) {
109 const match = PERSON_REGEX.exec(value);
110 if (!match) {
111 throw new Error(`Invalid stringified "person" value: ${value}`);
112 }
113 const [, name, email, url] = match;
114 const result = {
115 name: name.trim(),
116 };
117 if (email) {
118 result.email = email.trim();
119 }
120 if (url) {
121 result.url = url.trim();
122 }
123 return result;
124}
125exports.parsePerson = parsePerson;
126const REPOSITORY_REGEX = /^(?:(github|gist|bitbucket|gitlab):)?([A-Za-z\d_-]+\/[A-Za-z\d_-]+)$/;
127function parseRepository(value) {
128 const match = REPOSITORY_REGEX.exec(value);
129 if (!match) {
130 return { url: value };
131 }
132 const [, host, slug] = match;
133 switch (host ?? 'github') {
134 case 'github':
135 return { url: `https://github.com/${slug}.git` };
136 case 'gist':
137 return { url: `https://gist.github.com/${slug}.git` };
138 case 'bitbucket':
139 return { url: `https://bitbucket.org/${slug}.git` };
140 case 'gitlab':
141 return { url: `https://gitlab.com/${slug}.git` };
142 default:
143 throw new Error(`Unknown host service: ${host}`);
144 }
145}
146exports.parseRepository = parseRepository;
147const ANSI_REGEX =
148// eslint-disable-next-line no-control-regex
149/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
150function stripAnsi(x) {
151 return x.replace(ANSI_REGEX, '');
152}
153exports.stripAnsi = stripAnsi;
154//# sourceMappingURL=utils.js.map
\No newline at end of file