UNPKG

@autorest/openapi-to-typespec

Version:

Autorest plugin to scaffold a Typespec definition from an OpenAPI document

76 lines 2.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateSummary = exports.generateDocsContent = exports.generateDocs = void 0; const options_1 = require("../options"); function generateDocs({ doc }) { if (isEmptyDoc(doc)) { return ``; } const wrapped = lineWrap(doc); for (let i = 0; i < wrapped.length; i++) { if (wrapped[i].includes("@") || wrapped[i].includes("*/")) { if (wrapped.length === 1) { return `@doc("${wrapped[0].replace(/\\/g, "\\\\").replace(/"/g, '\\"')}")`; } return `@doc("""\n${wrapped.join("\n").replace(/\\/g, "\\\\").replace(/"/g, '\\"')}\n""")`; } } return `/**\n* ${wrapped.join("\n* ")}\n*/`; } exports.generateDocs = generateDocs; function generateDocsContent({ doc }) { if (isEmptyDoc(doc)) { return ``; } const wrapped = lineWrap(doc); return wrapped.length === 1 ? `${wrapped[0]}` : `""\n${wrapped.join("\n")}\n""`; } exports.generateDocsContent = generateDocsContent; function generateSummary({ summary }) { if (isEmptyDoc(summary)) { return ""; } const wrapped = lineWrap(summary); if (wrapped.length === 1) { return `@summary("${summary}")`; } return `@summary("""\n${wrapped.join("\n")}\n""")`; } exports.generateSummary = generateSummary; function lineWrap(doc) { const { isArm } = (0, options_1.getOptions)(); const maxLength = isArm ? Number.POSITIVE_INFINITY : 80; let docString = Array.isArray(doc) ? doc.join("\n") : doc; docString = docString.replace(/\r\n/g, "\n"); docString = docString.replace(/\r/g, "\n"); if (docString.length <= maxLength && !docString.includes("\n")) { return [docString]; } const oriLines = docString.split("\n"); const lines = []; for (const oriLine of oriLines) { const words = oriLine.split(" "); let line = ``; for (const word of words) { if (word.length + 1 > maxLength - line.length) { lines.push(line.substring(0, line.length - 1)); line = `${word} `; } else { line = `${line}${word} `; } } lines.push(`${line.substring(0, line.length - 1)}`); } return lines; } function isEmptyDoc(doc) { if (!doc) { return true; } if (Array.isArray(doc) && !doc.length) { return true; } return false; } //# sourceMappingURL=docs.js.map