-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcommon.ts
177 lines (159 loc) · 5.16 KB
/
common.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import * as fs from 'fs';
import { tmpdir } from 'os';
import * as path from 'path';
import * as vscode from 'vscode';
import moment = require('moment');
export const LANG_TLAPLUS = 'tlaplus';
export const LANG_TLAPLUS_CFG = 'tlaplus_cfg';
const MAX_TEMP_DIR_ATTEMPTS = 100;
export const emptyFunc = function(): void {
return undefined;
};
/**
* Thrown when there's some problem with parsing.
*/
export class ParsingError extends Error {
constructor(message: string) {
super(message);
}
}
export function pathToUri(filePath: string): vscode.Uri {
return vscode.Uri.file(filePath).with({ scheme: 'file' });
}
export function replaceExtension(filePath: string, newExt: string): string {
const lastDotIdx = filePath.lastIndexOf('.');
const basePath = lastDotIdx < 0 ? filePath : filePath.substring(0, lastDotIdx);
return `${basePath}.${newExt}`;
}
export function parseDateTime(str: string): moment.Moment {
const dateTime = moment(str, moment.ISO_8601, true);
if (dateTime.isValid()) {
return dateTime;
}
throw new ParsingError(`Cannot parse date/time ${str}`);
}
export function pathToModuleName(filePath: string): string {
// It's necessary to check both separators here, not just `path.sep`
// to support .out files portability. TLA+ doesn't support slashes in module names,
// so it breaks nothing.
// path.basename() doesn't work in some cases
const sid = Math.max(filePath.lastIndexOf('/'), filePath.lastIndexOf('\\'));
return filePath.substring(sid + 1, filePath.length - 4); // remove path and .tla
}
export function createTempDirSync(): string | undefined {
const baseDir = tmpdir();
for (let i = 0; i < MAX_TEMP_DIR_ATTEMPTS; i++) {
const timestamp = new Date().getTime();
const tempDir = `${baseDir}${path.sep}vscode-tlaplus-${timestamp}`;
if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir);
return tempDir;
}
}
vscode.window.showErrorMessage(`Cannot create temporary directory inside ${baseDir}.`);
return undefined;
}
export async function mkDir(dirPath: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.mkdir(dirPath, null, (err) => {
if (err) {
reject(err);
return;
}
resolve();
});
});
}
export async function deleteDir(dirPath: string): Promise<void> {
for (const fileName of fs.readdirSync(dirPath)) {
const filePath = path.join(dirPath, fileName);
try {
const fileInfo = await getFileInfo(filePath);
if (fileInfo.isDirectory()) {
await deleteDir(filePath);
} else {
await deleteFile(filePath);
}
} catch (err) {
console.error(`Cannot delete file ${filePath}: ${err}`);
}
}
fs.rmdir(dirPath, (err) => {
if (err) {
console.error(`Cannot delete directory ${dirPath}: ${err}`);
}
});
}
async function deleteFile(filePath: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.unlink(filePath, (err) => {
if (err) {
reject(err);
return;
}
resolve(undefined);
});
});
}
async function getFileInfo(filePath: string): Promise<fs.Stats> {
return new Promise((resolve, reject) => {
fs.lstat(filePath, (err, stats) => {
if (err) {
reject(err);
return;
}
resolve(stats);
});
});
}
export async function copyFile(filePath: string, destDir: string): Promise<void> {
return new Promise((resolve, reject) => {
const fileName = path.basename(filePath);
fs.copyFile(filePath, path.join(destDir, fileName), (err) => {
if (err) {
reject(err);
return;
}
resolve(undefined);
});
});
}
export async function writeFile(filePath: string, ...contents: string[]): Promise<boolean> {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, contents.join('\n'), (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
});
}
export async function readFile(filePath: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
export async function listFiles(dirPath: string, predicate?: (name: string) => boolean): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
fs.readdir(dirPath, (err, files) => {
if (err) {
reject(err);
return;
}
const result = predicate ? files.filter(predicate) : files;
resolve(result);
});
});
}
export async function exists(filePath: string): Promise<boolean> {
return new Promise(resolve => {
fs.exists(filePath, (ex) => resolve(ex));
});
}