1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.stripAnsi = exports.parseRepository = exports.parsePerson = exports.logDiagnostic = exports._formatDiagnostic = exports.formatDiagnostic = exports.diagnosticsLogger = exports.JSII_DIAGNOSTICS_CODE = exports.DIAGNOSTICS = void 0;
|
4 | const log4js = require("log4js");
|
5 | const ts = require("typescript");
|
6 | const jsii_diagnostic_1 = require("./jsii-diagnostic");
|
7 |
|
8 |
|
9 |
|
10 | exports.DIAGNOSTICS = 'diagnostics';
|
11 |
|
12 |
|
13 |
|
14 | exports.JSII_DIAGNOSTICS_CODE = 9999;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function 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 | }
|
48 | exports.diagnosticsLogger = diagnosticsLogger;
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function formatDiagnostic(diagnostic, projectRoot) {
|
58 | if (jsii_diagnostic_1.JsiiDiagnostic.isJsiiDiagnostic(diagnostic)) {
|
59 |
|
60 | return diagnostic.format(projectRoot);
|
61 | }
|
62 | return _formatDiagnostic(diagnostic, projectRoot);
|
63 | }
|
64 | exports.formatDiagnostic = formatDiagnostic;
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function _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 |
|
88 | return message.replace(` TS${exports.JSII_DIAGNOSTICS_CODE}: `, ` JSII${diagnostic.jsiiCode}: `);
|
89 | }
|
90 | exports._formatDiagnostic = _formatDiagnostic;
|
91 | function 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 | }
|
98 | exports.logDiagnostic = logDiagnostic;
|
99 | const PERSON_REGEX = /^\s*(.+?)(?:\s*<([^>]+)>)?(?:\s*\(([^)]+)\))?\s*$/;
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | function 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 | }
|
125 | exports.parsePerson = parsePerson;
|
126 | const REPOSITORY_REGEX = /^(?:(github|gist|bitbucket|gitlab):)?([A-Za-z\d_-]+\/[A-Za-z\d_-]+)$/;
|
127 | function 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 | }
|
146 | exports.parseRepository = parseRepository;
|
147 | const ANSI_REGEX =
|
148 |
|
149 | /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
|
150 | function stripAnsi(x) {
|
151 | return x.replace(ANSI_REGEX, '');
|
152 | }
|
153 | exports.stripAnsi = stripAnsi;
|
154 |
|
\ | No newline at end of file |