@azure-tools/codegen
Version:
Autorest Code generator common and base classes
121 lines • 4.47 kB
JavaScript
;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.guid = exports.copyBinaryResources = exports.copyResources = exports.applyOverrides = exports.fail = void 0;
const path_1 = require("path");
const aio = __importStar(require("@azure-tools/async-io"));
function getAllPropertyNames(obj) {
const props = new Array();
do {
Object.getOwnPropertyNames(obj).forEach(function (prop) {
if (props.indexOf(prop) === -1) {
props.push(prop);
}
});
/* eslint-disable */
} while ((obj = Object.getPrototypeOf(obj)));
return props;
}
function fail(text) {
throw new Error(text);
}
exports.fail = fail;
function applyOverrides(content, overrides) {
for (const [from, to] of Object.entries(overrides)) {
content = content.replace(new RegExp(from, "g"), to);
}
return content;
}
exports.applyOverrides = applyOverrides;
async function copyResources(sourceFolder, fileWriter, overrides = {}, contentManipulator = async (i) => {
return i;
}) {
const done = new Array();
try {
const files = await aio.readdir(sourceFolder);
for (const file of files) {
const fullPath = (0, path_1.join)(sourceFolder, file);
if (await aio.isDirectory(fullPath)) {
done.push(copyResources(fullPath, async (f, c) => fileWriter(`${file}/${f}`, c), overrides, contentManipulator));
continue;
}
if (await aio.isFile(fullPath)) {
done.push(aio
.readFile(fullPath)
.then(contentManipulator)
.then(async (content) => fileWriter(file, applyOverrides(content, overrides))));
}
}
}
catch (_a) {
// ignore failures
}
await Promise.all(done);
}
exports.copyResources = copyResources;
async function copyBinaryResources(sourceFolder, fileWriter) {
const done = new Array();
try {
const files = await aio.readdir(sourceFolder);
for (const file of files) {
const fullPath = (0, path_1.join)(sourceFolder, file);
if (await aio.isDirectory(fullPath)) {
done.push(copyBinaryResources(fullPath, async (f, c) => fileWriter(`${file}/${f}`, c)));
continue;
}
if (await aio.isFile(fullPath)) {
done.push(aio.readBinaryFile(fullPath).then(async (content) => fileWriter(file, content)));
}
}
}
catch (_a) {
// ignore failures
}
await Promise.all(done);
}
exports.copyBinaryResources = copyBinaryResources;
function quartet() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
return (quartet() +
quartet() +
"-" +
quartet() +
"-4" +
quartet().slice(0, 3) +
"-" +
quartet() +
"-" +
quartet() +
quartet() +
quartet()).toLowerCase();
}
exports.guid = guid;
//# sourceMappingURL=utility.js.map