basant307/AI_Governance_Project
048
1"use strict";2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {3 if (k2 === undefined) k2 = k;4 var desc = Object.getOwnPropertyDescriptor(m, k);5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {6 desc = { enumerable: true, get: function() { return m[k]; } };7 }8 Object.defineProperty(o, k2, desc);9}) : (function(o, m, k, k2) {10 if (k2 === undefined) k2 = k;11 o[k2] = m[k];12}));13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {14 Object.defineProperty(o, "default", { enumerable: true, value: v });15}) : function(o, v) {16 o["default"] = v;17});18var __importStar = (this && this.__importStar) || (function () {19 var ownKeys = function(o) {20 ownKeys = Object.getOwnPropertyNames || function (o) {21 var ar = [];22 for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;23 return ar;24 };25 return ownKeys(o);26 };27 return function (mod) {28 if (mod && mod.__esModule) return mod;29 var result = {};30 if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);31 __setModuleDefault(result, mod);32 return result;33 };34})();35var __importDefault = (this && this.__importDefault) || function (mod) {36 return (mod && mod.__esModule) ? mod : { "default": mod };37};38Object.defineProperty(exports, "__esModule", { value: true });39exports.log = exports.CancellationToken = void 0;40exports.read = read;41exports.getPublishedUrl = getPublishedUrl;42exports.getMarketplaceUrl = getMarketplaceUrl;43exports.getHubUrl = getHubUrl;44exports.getGalleryAPI = getGalleryAPI;45exports.getSecurityRolesAPI = getSecurityRolesAPI;46exports.getPublicGalleryAPI = getPublicGalleryAPI;47exports.normalize = normalize;48exports.chain = chain;49exports.flatten = flatten;50exports.nonnull = nonnull;51exports.isCancelledError = isCancelledError;52exports.sequence = sequence;53exports.patchOptionsWithManifest = patchOptionsWithManifest;54exports.bytesToString = bytesToString;55exports.filePathToVsixPath = filePathToVsixPath;56exports.vsixPathToFilePath = vsixPathToFilePath;57exports.generateFileStructureTree = generateFileStructureTree;58const util_1 = require("util");59const fs = __importStar(require("fs"));60const read_1 = __importDefault(require("read"));61const WebApi_1 = require("azure-devops-node-api/WebApi");62const GalleryApi_1 = require("azure-devops-node-api/GalleryApi");63const chalk_1 = __importDefault(require("chalk"));64const publicgalleryapi_1 = require("./publicgalleryapi");65const os_1 = require("os");66const __read = (0, util_1.promisify)(read_1.default);67function read(prompt, options = {}) {68 if (process.env['VSCE_TESTS'] || !process.stdout.isTTY) {69 return Promise.resolve('y');70 }71 return __read({ prompt, ...options });72}73const marketplaceUrl = process.env['VSCE_MARKETPLACE_URL'] || 'https://marketplace.visualstudio.com';74function getPublishedUrl(extension) {75 return `${marketplaceUrl}/items?itemName=${extension}`;76}77function getMarketplaceUrl() {78 return marketplaceUrl;79}80function getHubUrl(publisher, name) {81 return `${marketplaceUrl}/manage/publishers/${publisher}/extensions/${name}/hub`;82}83async function getGalleryAPI(pat) {84 // from https://github.com/Microsoft/tfs-cli/blob/master/app/exec/extension/default.ts#L287-L29285 const authHandler = (0, WebApi_1.getBasicHandler)('OAuth', pat);86 return new GalleryApi_1.GalleryApi(marketplaceUrl, [authHandler]);87 // const vsoapi = new WebApi(marketplaceUrl, authHandler);88 // return await vsoapi.getGalleryApi();89}90async function getSecurityRolesAPI(pat) {91 const authHandler = (0, WebApi_1.getBasicHandler)('OAuth', pat);92 const vsoapi = new WebApi_1.WebApi(marketplaceUrl, authHandler);93 return await vsoapi.getSecurityRolesApi();94}95function getPublicGalleryAPI() {96 return new publicgalleryapi_1.PublicGalleryAPI(marketplaceUrl, '3.0-preview.1');97}98function normalize(path) {99 return path.replace(/\\/g, '/');100}101function chain2(a, b, fn, index = 0) {102 if (index >= b.length) {103 return Promise.resolve(a);104 }105 return fn(a, b[index]).then(a => chain2(a, b, fn, index + 1));106}107function chain(initial, processors, process) {108 return chain2(initial, processors, process);109}110function flatten(arr) {111 return [].concat.apply([], arr);112}113function nonnull(arg) {114 return !!arg;115}116const CancelledError = 'Cancelled';117function isCancelledError(error) {118 return error === CancelledError;119}120class CancellationToken {121 constructor() {122 this.listeners = [];123 this._cancelled = false;124 }125 get isCancelled() {126 return this._cancelled;127 }128 subscribe(fn) {129 this.listeners.push(fn);130 return () => {131 const index = this.listeners.indexOf(fn);132 if (index > -1) {133 this.listeners.splice(index, 1);134 }135 };136 }137 cancel() {138 const emit = !this._cancelled;139 this._cancelled = true;140 if (emit) {141 this.listeners.forEach(l => l(CancelledError));142 this.listeners = [];143 }144 }145}146exports.CancellationToken = CancellationToken;147async function sequence(promiseFactories) {148 for (const factory of promiseFactories) {149 await factory();150 }151}152var LogMessageType;153(function (LogMessageType) {154 LogMessageType[LogMessageType["DONE"] = 0] = "DONE";155 LogMessageType[LogMessageType["INFO"] = 1] = "INFO";156 LogMessageType[LogMessageType["WARNING"] = 2] = "WARNING";157 LogMessageType[LogMessageType["ERROR"] = 3] = "ERROR";158})(LogMessageType || (LogMessageType = {}));159const LogPrefix = {160 [LogMessageType.DONE]: chalk_1.default.bgGreen.black(' DONE '),161 [LogMessageType.INFO]: chalk_1.default.bgBlueBright.black(' INFO '),162 [LogMessageType.WARNING]: chalk_1.default.bgYellow.black(' WARNING '),163 [LogMessageType.ERROR]: chalk_1.default.bgRed.black(' ERROR '),164};165function _log(type, msg, ...args) {166 args = [LogPrefix[type], msg, ...args];167 if (type === LogMessageType.WARNING) {168 process.env['GITHUB_ACTIONS'] ? logToGitHubActions('warning', msg) : console.warn(...args);169 }170 else if (type === LogMessageType.ERROR) {171 process.env['GITHUB_ACTIONS'] ? logToGitHubActions('error', msg) : console.error(...args);172 }173 else {174 process.env['GITHUB_ACTIONS'] ? logToGitHubActions('info', msg) : console.log(...args);175 }176}177const EscapeCharacters = new Map([178 ['%', '%25'],179 ['\r', '%0D'],180 ['\n', '%0A'],181]);182const EscapeRegex = new RegExp(`[${[...EscapeCharacters.keys()].join('')}]`, 'g');183function escapeGitHubActionsMessage(message) {184 return message.replace(EscapeRegex, c => EscapeCharacters.get(c) ?? c);185}186function logToGitHubActions(type, message) {187 const command = type === 'info' ? message : `::${type}::${escapeGitHubActionsMessage(message)}`;188 process.stdout.write(command + os_1.EOL);189}190exports.log = {191 done: _log.bind(null, LogMessageType.DONE),192 info: _log.bind(null, LogMessageType.INFO),193 warn: _log.bind(null, LogMessageType.WARNING),194 error: _log.bind(null, LogMessageType.ERROR),195};196function patchOptionsWithManifest(options, manifest) {197 if (!manifest.vsce) {198 return;199 }200 for (const key of Object.keys(manifest.vsce)) {201 const optionsKey = key === 'yarn' ? 'useYarn' : key;202 if (options[optionsKey] === undefined) {203 options[optionsKey] = manifest.vsce[key];204 }205 }206}207function bytesToString(bytes) {208 let size = 0;209 let unit = '';210 if (bytes > 1048576) {211 size = Math.round(bytes / 10485.76) / 100;212 unit = 'MB';213 }214 else {215 size = Math.round(bytes / 10.24) / 100;216 unit = 'KB';217 }218 return `${size} ${unit}`;219}220function filePathToVsixPath(originalFilePath) {221 return `extension/${originalFilePath}`;222}223function vsixPathToFilePath(extensionFilePath) {224 return extensionFilePath.startsWith('extension/') ? extensionFilePath.substring('extension/'.length) : extensionFilePath;225}226const FOLDER_SIZE_KEY = "/__FOlDER_SIZE__\\";227const FOLDER_FILES_TOTAL_KEY = "/__FOLDER_CHILDREN__\\";228const FILE_SIZE_WARNING_THRESHOLD = 0.85;229const FILE_SIZE_LARGE_THRESHOLD = 0.2;230async function generateFileStructureTree(rootFolder, filePaths, printLinesLimit = Number.MAX_VALUE) {231 const folderTree = {};232 const depthCounts = [];233 // Build a tree structure from the file paths234 // Store the file size in the leaf node and the folder size in the folder node235 // Store the number of children in the folder node236 for (const filePath of filePaths) {237 const parts = filePath.tree.split('/');238 let currentLevel = folderTree;239 parts.forEach((part, depth) => {240 const isFile = depth === parts.length - 1;241 // Create the node if it doesn't exist242 if (!currentLevel[part]) {243 if (isFile) {244 // The file size is stored in the leaf node, 245 currentLevel[part] = 0;246 }247 else {248 // The folder size is stored in the folder node249 currentLevel[part] = {};250 currentLevel[part][FOLDER_SIZE_KEY] = 0;251 currentLevel[part][FOLDER_FILES_TOTAL_KEY] = 0;252 }253 // Count the number of items at each depth254 if (depthCounts.length <= depth) {255 depthCounts.push(0);256 }257 depthCounts[depth]++;258 }259 currentLevel = currentLevel[part];260 // Count the total number of children in the nested folders261 if (!isFile) {262 currentLevel[FOLDER_FILES_TOTAL_KEY]++;263 }264 });265 }266 ;267 // Get max depth depending on the maximum number of lines allowed to print268 let currentDepth = 0;269 let countUpToCurrentDepth = depthCounts[0] + 1 /* root folder */;270 for (let i = 1; i < depthCounts.length; i++) {271 if (countUpToCurrentDepth + depthCounts[i] > printLinesLimit) {272 break;273 }274 currentDepth++;275 countUpToCurrentDepth += depthCounts[i];276 }277 const maxDepth = currentDepth;278 // Get all file sizes279 const fileSizes = await Promise.all(filePaths.map(async (filePath) => {280 try {281 const stats = await fs.promises.stat(filePath.origin);282 return [stats.size, filePath.tree];283 }284 catch (error) {285 return [0, filePath.origin];286 }287 }));288 // Store all file sizes in the tree289 let totalFileSizes = 0;290 fileSizes.forEach(([size, filePath]) => {291 totalFileSizes += size;292 const parts = filePath.split('/');293 let currentLevel = folderTree;294 parts.forEach(part => {295 if (currentLevel === undefined) {296 throw new Error(`currentLevel is undefined for ${part} in ${filePath}`);297 }298 if (typeof currentLevel[part] === 'number') {299 currentLevel[part] = size;300 }301 else if (currentLevel[part]) {302 currentLevel[part][FOLDER_SIZE_KEY] += size;303 }304 currentLevel = currentLevel[part];305 });306 });307 let output = [];308 output.push(chalk_1.default.bold(rootFolder));309 output.push(...createTreeOutput(folderTree, maxDepth, totalFileSizes));310 for (const [size, filePath] of fileSizes) {311 if (size > FILE_SIZE_WARNING_THRESHOLD * totalFileSizes) {312 output.push(`\nThe file ${filePath} is ${chalk_1.default.red('large')} (${bytesToString(size)})`);313 break;314 }315 }316 return output;317}318function createTreeOutput(fileSystem, maxDepth, totalFileSizes) {319 const getColorFromSize = (size) => {320 if (size > FILE_SIZE_WARNING_THRESHOLD * totalFileSizes) {321 return chalk_1.default.red;322 }323 else if (size > FILE_SIZE_LARGE_THRESHOLD * totalFileSizes) {324 return chalk_1.default.yellow;325 }326 else {327 return chalk_1.default.grey;328 }329 };330 const createFileOutput = (prefix, fileName, fileSize) => {331 let fileSizeColored = '';332 if (fileSize > 0) {333 const fileSizeString = `[${bytesToString(fileSize)}]`;334 fileSizeColored = getColorFromSize(fileSize)(fileSizeString);335 }336 return `${prefix}${fileName} ${fileSizeColored}`;337 };338 const createFolderOutput = (prefix, filesCount, folderSize, folderName, depth) => {339 if (depth < maxDepth) {340 // Max depth is not reached, print only the folder341 // as children will be printed342 return prefix + chalk_1.default.bold(`${folderName}/`);343 }344 // Max depth is reached, print the folder name and additional metadata345 // as children will not be printed346 const folderSizeString = bytesToString(folderSize);347 const folder = chalk_1.default.bold(`${folderName}/`);348 const numFilesString = chalk_1.default.green(`(${filesCount} ${filesCount === 1 ? 'file' : 'files'})`);349 const folderSizeColored = getColorFromSize(folderSize)(`[${folderSizeString}]`);350 return `${prefix}${folder} ${numFilesString} ${folderSizeColored}`;351 };352 const createTreeLayerOutput = (tree, depth, prefix, path) => {353 // Print all files before folders354 const sortedFolderKeys = Object.keys(tree).filter(key => typeof tree[key] !== 'number').sort();355 const sortedFileKeys = Object.keys(tree).filter(key => typeof tree[key] === 'number').sort();356 const sortedKeys = [...sortedFileKeys, ...sortedFolderKeys].filter(key => key !== FOLDER_SIZE_KEY && key !== FOLDER_FILES_TOTAL_KEY);357 const output = [];358 for (let i = 0; i < sortedKeys.length; i++) {359 const key = sortedKeys[i];360 const isLast = i === sortedKeys.length - 1;361 const localPrefix = prefix + (isLast ? '└─ ' : '├─ ');362 const childPrefix = prefix + (isLast ? ' ' : '│ ');363 if (typeof tree[key] === 'number') {364 // It's a file365 output.push(createFileOutput(localPrefix, key, tree[key]));366 }367 else {368 // It's a folder369 output.push(createFolderOutput(localPrefix, tree[key][FOLDER_FILES_TOTAL_KEY], tree[key][FOLDER_SIZE_KEY], key, depth));370 if (depth < maxDepth) {371 output.push(...createTreeLayerOutput(tree[key], depth + 1, childPrefix, path + key + '/'));372 }373 }374 }375 return output;376 };377 return createTreeLayerOutput(fileSystem, 0, '', '');378}379//# sourceMappingURL=util.js.map