-
Notifications
You must be signed in to change notification settings - Fork 198
/
TestPlatform.ts
64 lines (57 loc) · 1.56 KB
/
TestPlatform.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
import { IOHelper } from '@src/io/IOHelper';
import * as fs from 'fs';
import * as path from 'path';
/**
* @partial
*/
export class TestPlatform {
/**
* @target web
* @partial
*/
public static async saveFile(name: string, data: Uint8Array): Promise<void> {
const directory = path.dirname(name);
await fs.promises.mkdir(directory, { recursive: true })
await fs.promises.writeFile(name, data);
}
/**
* @target web
* @partial
*/
public static async deleteFile(name: string): Promise<void> {
await fs.promises.rm(name, { force: true })
}
/**
* @target web
* @partial
*/
public static loadFile(path: string): Promise<Uint8Array> {
return fs.promises.readFile(path);
}
/**
* @target web
* @partial
*/
public static async listDirectory(path: string): Promise<string[]> {
return await fs.promises.readdir(path);
}
public static async loadFileAsString(path: string): Promise<string> {
const data = await TestPlatform.loadFile(path);
return IOHelper.toString(data, 'UTF-8');
}
public static changeExtension(file: string, extension: string): string {
let lastDot: number = file.lastIndexOf('.');
if (lastDot === -1) {
return file + extension;
} else {
return file.substr(0, lastDot) + extension;
}
}
/**
* @target web
* @partial
*/
public static joinPath(...parts: string[]): string {
return path.join(...parts);
}
}