UNPKG

6.27 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.patchOptionsWithManifest = exports.log = exports.sequence = exports.CancellationToken = exports.isCancelledError = exports.nonnull = exports.flatten = exports.chain = exports.normalize = exports.getPublicGalleryAPI = exports.getSecurityRolesAPI = exports.getGalleryAPI = exports.getHubUrl = exports.getPublishedUrl = exports.read = void 0;
7const util_1 = require("util");
8const read_1 = __importDefault(require("read"));
9const WebApi_1 = require("azure-devops-node-api/WebApi");
10const GalleryApi_1 = require("azure-devops-node-api/GalleryApi");
11const chalk_1 = __importDefault(require("chalk"));
12const publicgalleryapi_1 = require("./publicgalleryapi");
13const os_1 = require("os");
14const __read = (0, util_1.promisify)(read_1.default);
15function read(prompt, options = {}) {
16 if (process.env['VSCE_TESTS'] || !process.stdout.isTTY) {
17 return Promise.resolve('y');
18 }
19 return __read({ prompt, ...options });
20}
21exports.read = read;
22const marketplaceUrl = process.env['VSCE_MARKETPLACE_URL'] || 'https://marketplace.visualstudio.com';
23function getPublishedUrl(extension) {
24 return `${marketplaceUrl}/items?itemName=${extension}`;
25}
26exports.getPublishedUrl = getPublishedUrl;
27function getHubUrl(publisher, name) {
28 return `${marketplaceUrl}/manage/publishers/${publisher}/extensions/${name}/hub`;
29}
30exports.getHubUrl = getHubUrl;
31async function getGalleryAPI(pat) {
32 // from https://github.com/Microsoft/tfs-cli/blob/master/app/exec/extension/default.ts#L287-L292
33 const authHandler = (0, WebApi_1.getBasicHandler)('OAuth', pat);
34 return new GalleryApi_1.GalleryApi(marketplaceUrl, [authHandler]);
35 // const vsoapi = new WebApi(marketplaceUrl, authHandler);
36 // return await vsoapi.getGalleryApi();
37}
38exports.getGalleryAPI = getGalleryAPI;
39async function getSecurityRolesAPI(pat) {
40 const authHandler = (0, WebApi_1.getBasicHandler)('OAuth', pat);
41 const vsoapi = new WebApi_1.WebApi(marketplaceUrl, authHandler);
42 return await vsoapi.getSecurityRolesApi();
43}
44exports.getSecurityRolesAPI = getSecurityRolesAPI;
45function getPublicGalleryAPI() {
46 return new publicgalleryapi_1.PublicGalleryAPI(marketplaceUrl, '3.0-preview.1');
47}
48exports.getPublicGalleryAPI = getPublicGalleryAPI;
49function normalize(path) {
50 return path.replace(/\\/g, '/');
51}
52exports.normalize = normalize;
53function chain2(a, b, fn, index = 0) {
54 if (index >= b.length) {
55 return Promise.resolve(a);
56 }
57 return fn(a, b[index]).then(a => chain2(a, b, fn, index + 1));
58}
59function chain(initial, processors, process) {
60 return chain2(initial, processors, process);
61}
62exports.chain = chain;
63function flatten(arr) {
64 return [].concat.apply([], arr);
65}
66exports.flatten = flatten;
67function nonnull(arg) {
68 return !!arg;
69}
70exports.nonnull = nonnull;
71const CancelledError = 'Cancelled';
72function isCancelledError(error) {
73 return error === CancelledError;
74}
75exports.isCancelledError = isCancelledError;
76class CancellationToken {
77 constructor() {
78 this.listeners = [];
79 this._cancelled = false;
80 }
81 get isCancelled() {
82 return this._cancelled;
83 }
84 subscribe(fn) {
85 this.listeners.push(fn);
86 return () => {
87 const index = this.listeners.indexOf(fn);
88 if (index > -1) {
89 this.listeners.splice(index, 1);
90 }
91 };
92 }
93 cancel() {
94 const emit = !this._cancelled;
95 this._cancelled = true;
96 if (emit) {
97 this.listeners.forEach(l => l(CancelledError));
98 this.listeners = [];
99 }
100 }
101}
102exports.CancellationToken = CancellationToken;
103async function sequence(promiseFactories) {
104 for (const factory of promiseFactories) {
105 await factory();
106 }
107}
108exports.sequence = sequence;
109var LogMessageType;
110(function (LogMessageType) {
111 LogMessageType[LogMessageType["DONE"] = 0] = "DONE";
112 LogMessageType[LogMessageType["INFO"] = 1] = "INFO";
113 LogMessageType[LogMessageType["WARNING"] = 2] = "WARNING";
114 LogMessageType[LogMessageType["ERROR"] = 3] = "ERROR";
115})(LogMessageType || (LogMessageType = {}));
116const LogPrefix = {
117 [LogMessageType.DONE]: chalk_1.default.bgGreen.black(' DONE '),
118 [LogMessageType.INFO]: chalk_1.default.bgBlueBright.black(' INFO '),
119 [LogMessageType.WARNING]: chalk_1.default.bgYellow.black(' WARNING '),
120 [LogMessageType.ERROR]: chalk_1.default.bgRed.black(' ERROR '),
121};
122function _log(type, msg, ...args) {
123 args = [LogPrefix[type], msg, ...args];
124 if (type === LogMessageType.WARNING) {
125 process.env['GITHUB_ACTIONS'] ? logToGitHubActions('warning', msg) : console.warn(...args);
126 }
127 else if (type === LogMessageType.ERROR) {
128 process.env['GITHUB_ACTIONS'] ? logToGitHubActions('error', msg) : console.error(...args);
129 }
130 else {
131 process.env['GITHUB_ACTIONS'] ? logToGitHubActions('info', msg) : console.log(...args);
132 }
133}
134const EscapeCharacters = new Map([
135 ['%', '%25'],
136 ['\r', '%0D'],
137 ['\n', '%0A'],
138]);
139const EscapeRegex = new RegExp(`[${[...EscapeCharacters.keys()].join('')}]`, 'g');
140function escapeGitHubActionsMessage(message) {
141 return message.replace(EscapeRegex, c => EscapeCharacters.get(c) ?? c);
142}
143function logToGitHubActions(type, message) {
144 const command = type === 'info' ? message : `::${type}::${escapeGitHubActionsMessage(message)}`;
145 process.stdout.write(command + os_1.EOL);
146}
147exports.log = {
148 done: _log.bind(null, LogMessageType.DONE),
149 info: _log.bind(null, LogMessageType.INFO),
150 warn: _log.bind(null, LogMessageType.WARNING),
151 error: _log.bind(null, LogMessageType.ERROR),
152};
153function patchOptionsWithManifest(options, manifest) {
154 if (!manifest.vsce) {
155 return;
156 }
157 for (const key of Object.keys(manifest.vsce)) {
158 const optionsKey = key === 'yarn' ? 'useYarn' : key;
159 if (options[optionsKey] === undefined) {
160 options[optionsKey] = manifest.vsce[key];
161 }
162 }
163}
164exports.patchOptionsWithManifest = patchOptionsWithManifest;
165//# sourceMappingURL=util.js.map
\No newline at end of file