@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
137 lines • 5.48 kB
JavaScript
;
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.findSwaggerToSDKConfiguration = exports.autorest = exports.autorestExecutable = void 0;
var tslib_1 = require("tslib");
var commonmark = tslib_1.__importStar(require("commonmark"));
var jsYaml = tslib_1.__importStar(require("js-yaml"));
var os = tslib_1.__importStar(require("os"));
var run_1 = require("./run");
/**
* Get the executable that will be used to run AutoRest.
* @param options The options for specifying which executable to use.
*/
function autorestExecutable(options) {
if (options === void 0) { options = {}; }
if (!options.osPlatform) {
options.osPlatform = os.platform();
}
var result = options.autorestPath || "";
if (!result.endsWith("autorest") && !result.endsWith("autorest.cmd")) {
if (result && !result.endsWith("/") && !result.endsWith("\\")) {
result += "/";
}
result += "autorest";
}
if (options.osPlatform === "win32" && !result.endsWith(".cmd")) {
result += ".cmd";
}
return result;
}
exports.autorestExecutable = autorestExecutable;
/**
* Run AutoRest in a new process using the provided options.
* @param autorestOptions The options to pass to AutoRest.
* @param runOptions The options to use when creating the AutoRest process.
*/
function autorest(readmeMdFileUrl, autorestOptions, runOptions) {
var e_1, _a;
runOptions = runOptions || {};
var autorestCommand = autorestExecutable(runOptions);
var args = [];
if (autorestOptions) {
try {
for (var _b = tslib_1.__values(Object.keys(autorestOptions)), _c = _b.next(); !_c.done; _c = _b.next()) {
var optionName = _c.value;
var argument = optionName;
if (!argument.startsWith("--")) {
argument = "--" + argument;
}
var optionValue = autorestOptions[optionName];
if (Array.isArray(optionValue)) {
argument += "=" + optionValue.join(",");
}
else if (optionValue != undefined && optionValue !== "") {
argument += "=" + optionValue;
}
args.push(argument);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
}
if (readmeMdFileUrl) {
args.push(readmeMdFileUrl);
}
return run_1.run(autorestCommand, args, runOptions);
}
exports.autorest = autorest;
function findSwaggerToSDKYamlBlocks(parsedMarkdownNode) {
var result = [];
if (parsedMarkdownNode) {
var nodesToVisit = [parsedMarkdownNode];
while (nodesToVisit.length > 0) {
var node = nodesToVisit.shift();
if (node.firstChild) {
nodesToVisit.push(node.firstChild);
}
if (node.next) {
nodesToVisit.push(node.next);
}
if (node.type === "code_block" && node.info && node.info.toLowerCase().indexOf("$(swagger-to-sdk)") !== -1) {
result.push(node);
}
}
}
return result;
}
/**
* Parse the contents of an AutoRest readme.md configuration file and return the parsed swagger to
* sdk configuration section.
* @param readmeMdFileContents The contents of an AutoRest readme.md configuration file.
*/
function findSwaggerToSDKConfiguration(readmeMdFileContents) {
var e_2, _a;
var result;
if (readmeMdFileContents) {
var markdownParser = new commonmark.Parser();
var parsedReadmeMd = markdownParser.parse(readmeMdFileContents);
var swaggerToSDKYamlBlocks = findSwaggerToSDKYamlBlocks(parsedReadmeMd);
var repositories = [];
try {
for (var swaggerToSDKYamlBlocks_1 = tslib_1.__values(swaggerToSDKYamlBlocks), swaggerToSDKYamlBlocks_1_1 = swaggerToSDKYamlBlocks_1.next(); !swaggerToSDKYamlBlocks_1_1.done; swaggerToSDKYamlBlocks_1_1 = swaggerToSDKYamlBlocks_1.next()) {
var swaggerToSDKYamlBlock = swaggerToSDKYamlBlocks_1_1.value;
var yamlBlockContents = swaggerToSDKYamlBlock.literal;
if (yamlBlockContents) {
var yaml = jsYaml.safeLoad(yamlBlockContents);
if (yaml) {
var swaggerToSDK = yaml["swagger-to-sdk"];
if (swaggerToSDK && Array.isArray(swaggerToSDK)) {
repositories.push.apply(repositories, tslib_1.__spread(swaggerToSDK));
}
}
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (swaggerToSDKYamlBlocks_1_1 && !swaggerToSDKYamlBlocks_1_1.done && (_a = swaggerToSDKYamlBlocks_1.return)) _a.call(swaggerToSDKYamlBlocks_1);
}
finally { if (e_2) throw e_2.error; }
}
result = { repositories: repositories };
}
return result;
}
exports.findSwaggerToSDKConfiguration = findSwaggerToSDKConfiguration;
//# sourceMappingURL=autorest.js.map