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IAAI,MAAM,EAAE,CAAC;QACxD,KAAK,WAAW;YACd,OAAO,EAAE,GAAG,EAAE,yBAAyB,IAAI,MAAM,EAAE,CAAC;QACtD,KAAK,QAAQ;YACX,OAAO,EAAE,GAAG,EAAE,sBAAsB,IAAI,MAAM,EAAE,CAAC;QACnD;YACE,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,EAAE,CAAC,CAAC;IACrD,CAAC;AACH,CAAC;AAlBD,0CAkBC;AAED,MAAM,UAAU;AACd,4CAA4C;AAC5C,6EAA6E,CAAC;AAEhF,SAAgB,SAAS,CAAC,CAAS;IACjC,OAAO,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;AACnC,CAAC;AAFD,8BAEC","sourcesContent":["import * as log4js from 'log4js';\nimport * as ts from 'typescript';\n\nimport { JsiiDiagnostic } from './jsii-diagnostic';\n\n/**\n * Name of the logger for diagnostics information\n */\nexport const DIAGNOSTICS = 'diagnostics';\n/**\n * Diagnostic code for JSII-generated messages.\n */\nexport const JSII_DIAGNOSTICS_CODE = 9999;\n\n/**\n * Obtains the relevant logger to be used for a given diagnostic message.\n *\n * @param logger the ``log4js.Logger`` to use for emitting the message.\n * @param diagnostic the message for which a logger is requested.\n *\n * @returns a logger method of the ``logger`` for the appropriate level.\n */\nexport function diagnosticsLogger(\n logger: log4js.Logger,\n diagnostic: ts.Diagnostic,\n): ((message: any, ...args: any[]) => void) | undefined {\n switch (diagnostic.category) {\n case ts.DiagnosticCategory.Error:\n if (!logger.isErrorEnabled()) {\n return undefined;\n }\n return logger.error.bind(logger);\n case ts.DiagnosticCategory.Warning:\n if (!logger.isWarnEnabled()) {\n return undefined;\n }\n return logger.warn.bind(logger);\n case ts.DiagnosticCategory.Message:\n if (!logger.isDebugEnabled()) {\n return undefined;\n }\n return logger.debug.bind(logger);\n case ts.DiagnosticCategory.Suggestion:\n default:\n if (!logger.isTraceEnabled()) {\n return undefined;\n }\n return logger.trace.bind(logger);\n }\n}\n\n/**\n * Formats a diagnostic message with color and context, if possible.\n *\n * @param diagnostic the diagnostic message ot be formatted.\n * @param projectRoot the root of the TypeScript project.\n *\n * @returns a formatted string.\n */\nexport function formatDiagnostic(diagnostic: ts.Diagnostic, projectRoot: string) {\n if (JsiiDiagnostic.isJsiiDiagnostic(diagnostic)) {\n // Ensure we leverage pre-rendered diagnostics where available.\n return diagnostic.format(projectRoot);\n }\n return _formatDiagnostic(diagnostic, projectRoot);\n}\n\n/**\n * Formats a diagnostic message with color and context, if possible. Users\n * should use `formatDiagnostic` instead, as this implementation is intended for\n * internal usafe only.\n *\n * @param diagnostic the diagnostic message ot be formatted.\n * @param projectRoot the root of the TypeScript project.\n *\n * @returns a formatted string.\n */\nexport function _formatDiagnostic(diagnostic: ts.Diagnostic, projectRoot: string) {\n const formatDiagnosticsHost: ts.FormatDiagnosticsHost = {\n getCurrentDirectory: () => projectRoot,\n getCanonicalFileName: (fileName) => fileName,\n getNewLine: () => ts.sys.newLine,\n };\n\n const message =\n diagnostic.file != null\n ? ts.formatDiagnosticsWithColorAndContext([diagnostic], formatDiagnosticsHost)\n : ts.formatDiagnostic(diagnostic, formatDiagnosticsHost);\n\n if (!JsiiDiagnostic.isJsiiDiagnostic(diagnostic)) {\n return message;\n }\n\n // This is our own diagnostics, so we'll format appropriately (replacing TS#### with JSII####).\n return message.replace(` TS${JSII_DIAGNOSTICS_CODE}: `, ` JSII${diagnostic.jsiiCode}: `);\n}\n\nexport function logDiagnostic(diagnostic: ts.Diagnostic, projectRoot: string) {\n const logFunc = diagnosticsLogger(log4js.getLogger(DIAGNOSTICS), diagnostic);\n if (!logFunc) {\n return;\n }\n logFunc(formatDiagnostic(diagnostic, projectRoot).trim());\n}\n\nconst PERSON_REGEX = /^\\s*(.+?)(?:\\s*<([^>]+)>)?(?:\\s*\\(([^)]+)\\))?\\s*$/;\n/**\n * Parses a string-formatted person entry from `package.json`.\n * @param value the string-formatted person entry.\n *\n * @example\n * parsePerson(\"Barney Rubble <b@rubble.com> (http://barnyrubble.tumblr.com/)\");\n * // => { name: \"Barney Rubble\", email: \"b@rubble.com\", url: \"http://barnyrubble.tumblr.com/\" }\n */\nexport function parsePerson(value: string) {\n const match = PERSON_REGEX.exec(value);\n if (!match) {\n throw new Error(`Invalid stringified \"person\" value: ${value}`);\n }\n const [, name, email, url] = match;\n const result: { name: string; email?: string; url?: string } = {\n name: name.trim(),\n };\n if (email) {\n result.email = email.trim();\n }\n if (url) {\n result.url = url.trim();\n }\n return result;\n}\n\nconst REPOSITORY_REGEX = /^(?:(github|gist|bitbucket|gitlab):)?([A-Za-z\\d_-]+\\/[A-Za-z\\d_-]+)$/;\nexport function parseRepository(value: string): { url: string } {\n const match = REPOSITORY_REGEX.exec(value);\n if (!match) {\n return { url: value };\n }\n const [, host, slug] = match;\n switch (host ?? 'github') {\n case 'github':\n return { url: `https://github.com/${slug}.git` };\n case 'gist':\n return { url: `https://gist.github.com/${slug}.git` };\n case 'bitbucket':\n return { url: `https://bitbucket.org/${slug}.git` };\n case 'gitlab':\n return { url: `https://gitlab.com/${slug}.git` };\n default:\n throw new Error(`Unknown host service: ${host}`);\n }\n}\n\nconst ANSI_REGEX =\n // eslint-disable-next-line no-control-regex\n /[\\u001b\\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;\n\nexport function stripAnsi(x: string): string {\n return x.replace(ANSI_REGEX, '');\n}\n\n/**\n * Maps the provided type to stip all `readonly` modifiers from its properties.\n */\nexport type Mutable<T> = { -readonly [K in keyof T]: Mutable<T[K]> };\n"]} |