-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathutils.ts
51 lines (43 loc) · 1.43 KB
/
utils.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
'use strict';
// imports
import * as vscode from 'vscode';
import * as path from 'path';
// get project root directory
var projectRoot = null;
export function setProjectRoot(root: string) {
projectRoot = root;
}
export function getProjectRoot(): string {
if (projectRoot == null &&
vscode.workspace.workspaceFolders &&vscode.workspace.workspaceFolders.length > 0) {
projectRoot = vscode.workspace.workspaceFolders[0].uri.fsPath;
}
return projectRoot;
}
// replace all
export function replaceAll(str: string, needle: string, what: string) {
const pattern = needle.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
const re = new RegExp(pattern, 'g');
return str.replace(re, what);
}
// replace variables
export function replaceVars(str: string): string {
// init replacements
const replacements = [
['${workspaceRoot}', getProjectRoot()],
['${workspaceRootFolderName}', path.basename(getProjectRoot() || '.')]
] as [string, string][];
// replace all variables
return replacements.reduce((accdir, [needle, what]) => replaceAll(accdir, needle, what), str);
}
// simplistic function for just checking if a string can be parsed as json
export function isJson(text?: string): boolean {
try {
JSON.parse(text);
} catch(e) {
return false;
}
return true;
}
// sleep some times
export const sleep = ms => new Promise(res => setTimeout(res, ms));