/
util.ts
109 lines (86 loc) · 3.07 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import * as _read from 'read';
import { WebApi, getBasicHandler } from 'vso-node-api/WebApi';
import { IGalleryApi } from 'vso-node-api/GalleryApi';
import * as denodeify from 'denodeify';
import { PublicGalleryAPI } from './publicgalleryapi';
import { ISecurityRolesApi } from 'vso-node-api/SecurityRolesApi';
const __read = denodeify<_read.Options, string>(_read);
export function read(prompt: string, options: _read.Options = {}): Promise<string> {
if (process.env['VSCE_TESTS'] || !process.stdout.isTTY) {
return Promise.resolve('y');
}
return __read({ prompt, ...options });
}
const marketplaceUrl = process.env['VSCE_MARKETPLACE_URL'] || 'https://marketplace.visualstudio.com';
export function getPublishedUrl(extension: string): string {
return `${marketplaceUrl}/items?itemName=${extension}`;
}
export function getGalleryAPI(pat: string): IGalleryApi {
const authHandler = getBasicHandler('oauth', pat);
const vsoapi = new WebApi('oauth', authHandler);
return vsoapi.getGalleryApi(marketplaceUrl);
}
export function getSecurityRolesAPI(pat: string): ISecurityRolesApi {
const authHandler = getBasicHandler('oauth', pat);
const vsoapi = new WebApi('oauth', authHandler);
return vsoapi.getSecurityRolesApi(marketplaceUrl);
}
export function getPublicGalleryAPI() {
return new PublicGalleryAPI(marketplaceUrl, '3.0-preview.1');
}
export function normalize(path: string): string {
return path.replace(/\\/g, '/');
}
function chain2<A, B>(a: A, b: B[], fn: (a: A, b: B) => Promise<A>, index = 0): Promise<A> {
if (index >= b.length) {
return Promise.resolve(a);
}
return fn(a, b[index]).then(a => chain2(a, b, fn, index + 1));
}
export function chain<T, P>(initial: T, processors: P[], process: (a: T, b: P) => Promise<T>): Promise<T> {
return chain2(initial, processors, process);
}
export function flatten<T>(arr: T[][]): T[] {
return [].concat.apply([], arr) as T[];
}
const CancelledError = 'Cancelled';
export function isCancelledError(error: any) {
return error === CancelledError;
}
export class CancellationToken {
private listeners: Function[] = [];
private _cancelled: boolean = false;
get isCancelled(): boolean { return this._cancelled; }
subscribe(fn: Function): Function {
this.listeners.push(fn);
return () => {
const index = this.listeners.indexOf(fn);
if (index > -1) {
this.listeners.splice(index, 1);
}
};
}
cancel(): void {
const emit = !this._cancelled;
this._cancelled = true;
if (emit) {
this.listeners.forEach(l => l(CancelledError));
this.listeners = [];
}
}
}
export async function sequence(promiseFactories: { (): Promise<any> }[]): Promise<void> {
for (const factory of promiseFactories) {
await factory();
}
}
const ANSI_COLORS = {
reset: '\x1b[0m',
bgGreen: '\x1b[42m',
bgYellow: '\x1b[43m',
bgRed: '\x1b[41m',
fgBlack: '\x1b[30m',
};
export const DONE = `${ANSI_COLORS.bgGreen}${ANSI_COLORS.fgBlack} DONE ${ANSI_COLORS.reset}`;
export const WARN = `${ANSI_COLORS.bgYellow}${ANSI_COLORS.fgBlack} WARNING ${ANSI_COLORS.reset}`;
export const ERROR = `${ANSI_COLORS.bgRed}${ANSI_COLORS.fgBlack} ERROR ${ANSI_COLORS.reset}`;