mirror of
https://github.com/microsoft/vscode.git
synced 2026-03-03 15:29:23 +00:00
76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import * as vscode from 'vscode';
|
|
import * as fs from 'fs';
|
|
import * as os from 'os';
|
|
import { join } from 'path';
|
|
|
|
export function rndName() {
|
|
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10);
|
|
}
|
|
|
|
export function createRandomFile(contents = '', dir: string = os.tmpdir(), ext = ''): Thenable<vscode.Uri> {
|
|
return new Promise((resolve, reject) => {
|
|
const tmpFile = join(dir, rndName() + ext);
|
|
fs.writeFile(tmpFile, contents, (error) => {
|
|
if (error) {
|
|
return reject(error);
|
|
}
|
|
|
|
resolve(vscode.Uri.file(tmpFile));
|
|
});
|
|
});
|
|
}
|
|
|
|
export function pathEquals(path1: string, path2: string): boolean {
|
|
if (process.platform !== 'linux') {
|
|
path1 = path1.toLowerCase();
|
|
path2 = path2.toLowerCase();
|
|
}
|
|
|
|
return path1 === path2;
|
|
}
|
|
|
|
export function deleteFile(file: vscode.Uri): Thenable<boolean> {
|
|
return new Promise((resolve, reject) => {
|
|
fs.unlink(file.fsPath, (err) => {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
resolve(true);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
export function closeAllEditors(): Thenable<any> {
|
|
return vscode.commands.executeCommand('workbench.action.closeAllEditors');
|
|
|
|
}
|
|
|
|
export function disposeAll(disposables: vscode.Disposable[]) {
|
|
while (disposables.length) {
|
|
let item = disposables.pop();
|
|
if (item) {
|
|
item.dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
export function conditionalTest(name: string, testCallback: (done: MochaDone) => void | Thenable<any>) {
|
|
if (isTestTypeActive()) {
|
|
const async = !!testCallback.length;
|
|
if (async) {
|
|
test(name, (done) => testCallback(done));
|
|
} else {
|
|
test(name, () => (<() => void | Thenable<any>>testCallback)());
|
|
}
|
|
}
|
|
}
|
|
|
|
function isTestTypeActive(): boolean {
|
|
return !!vscode.extensions.getExtension('vscode-resolver-test');
|
|
} |