forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
88 lines (82 loc) · 2.95 KB
/
helpers.ts
File metadata and controls
88 lines (82 loc) · 2.95 KB
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
import { ModuleNotInstalledError } from './errors/moduleNotInstalledError';
// tslint:disable-next-line:no-require-imports no-var-requires
const tmp = require('tmp');
export function isNotInstalledError(error: Error): boolean {
const isError = typeof (error) === 'object' && error !== null;
// tslint:disable-next-line:no-any
const errorObj = <any>error;
if (!isError) {
return false;
}
if (error instanceof ModuleNotInstalledError) {
return true;
}
const isModuleNoInstalledError = errorObj.code === 1 && error.message.indexOf('No module named') >= 0;
return errorObj.code === 'ENOENT' || errorObj.code === 127 || isModuleNoInstalledError;
}
// tslint:disable-next-line:interface-name
export interface Deferred<T> {
readonly promise: Promise<T>;
readonly resolved: boolean;
readonly rejected: boolean;
readonly completed: boolean;
resolve(value?: T | PromiseLike<T>);
// tslint:disable-next-line:no-any
reject(reason?: any);
}
class DeferredImpl<T> implements Deferred<T> {
private _resolve: (value?: T | PromiseLike<T>) => void;
// tslint:disable-next-line:no-any
private _reject: (reason?: any) => void;
private _resolved: boolean = false;
private _rejected: boolean = false;
private _promise: Promise<T>;
// tslint:disable-next-line:no-any
constructor(private scope: any = null) {
// tslint:disable-next-line:promise-must-complete
this._promise = new Promise<T>((res, rej) => {
this._resolve = res;
this._reject = rej;
});
}
public resolve(value?: T | PromiseLike<T>) {
this._resolve.apply(this.scope ? this.scope : this, arguments);
this._resolved = true;
}
// tslint:disable-next-line:no-any
public reject(reason?: any) {
this._reject.apply(this.scope ? this.scope : this, arguments);
this._rejected = true;
}
get promise(): Promise<T> {
return this._promise;
}
get resolved(): boolean {
return this._resolved;
}
get rejected(): boolean {
return this._rejected;
}
get completed(): boolean {
return this._rejected || this._resolved;
}
}
// tslint:disable-next-line:no-any
export function createDeferred<T>(scope: any = null): Deferred<T> {
return new DeferredImpl<T>(scope);
}
export function createTemporaryFile(extension: string, temporaryDirectory?: string): Promise<{ filePath: string, cleanupCallback: Function }> {
// tslint:disable-next-line:no-any
const options: any = { postfix: extension };
if (temporaryDirectory) {
options.dir = temporaryDirectory;
}
return new Promise<{ filePath: string, cleanupCallback: Function }>((resolve, reject) => {
tmp.file(options, (err, tmpFile, fd, cleanupCallback) => {
if (err) {
return reject(err);
}
resolve({ filePath: tmpFile, cleanupCallback: cleanupCallback });
});
});
}