forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileSystem.ts
More file actions
196 lines (177 loc) · 7 KB
/
fileSystem.ts
File metadata and controls
196 lines (177 loc) · 7 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { createHash } from 'crypto';
import * as fileSystem from 'fs';
import * as fs from 'fs-extra';
import * as glob from 'glob';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import * as tmp from 'tmp';
import { createDeferred } from '../utils/async';
import { IFileSystem, IPlatformService, TemporaryFile } from './types';
@injectable()
export class FileSystem implements IFileSystem {
constructor(@inject(IPlatformService) private platformService: IPlatformService) { }
public get directorySeparatorChar(): string {
return path.sep;
}
public objectExists(filePath: string, statCheck: (s: fs.Stats) => boolean): Promise<boolean> {
return new Promise<boolean>(resolve => {
fs.stat(filePath, (error, stats) => {
if (error) {
return resolve(false);
}
return resolve(statCheck(stats));
});
});
}
public fileExists(filePath: string): Promise<boolean> {
return this.objectExists(filePath, (stats) => stats.isFile());
}
public fileExistsSync(filePath: string): boolean {
return fs.existsSync(filePath);
}
/**
* Reads the contents of the file using utf8 and returns the string contents.
* @param {string} filePath
* @returns {Promise<string>}
* @memberof FileSystem
*/
public readFile(filePath: string): Promise<string> {
return fs.readFile(filePath).then(buffer => buffer.toString());
}
public async writeFile(filePath: string, data: {}, options: string | fs.WriteFileOptions = { encoding: 'utf8' }): Promise<void> {
await fs.writeFile(filePath, data, options);
}
public directoryExists(filePath: string): Promise<boolean> {
return this.objectExists(filePath, (stats) => stats.isDirectory());
}
public createDirectory(directoryPath: string): Promise<void> {
return fs.mkdirp(directoryPath);
}
public deleteDirectory(directoryPath: string): Promise<void> {
const deferred = createDeferred<void>();
fs.rmdir(directoryPath, err => err ? deferred.reject(err) : deferred.resolve());
return deferred.promise;
}
public getSubDirectories(rootDir: string): Promise<string[]> {
return new Promise<string[]>(resolve => {
fs.readdir(rootDir, async (error, files) => {
if (error) {
return resolve([]);
}
const subDirs = (
await Promise.all(
files.map(async name => {
const fullPath = path.join(rootDir, name);
try {
if ((await fs.stat(fullPath)).isDirectory()) {
return fullPath;
}
// tslint:disable-next-line:no-empty
} catch (ex) { }
})
))
.filter(dir => dir !== undefined) as string[];
resolve(subDirs);
});
});
}
public async getFiles(rootDir: string): Promise<string[]> {
const files = await fs.readdir(rootDir);
return files.filter(async f => {
const fullPath = path.join(rootDir, f);
if ((await fs.stat(fullPath)).isFile()) {
return true;
}
return false;
});
}
public arePathsSame(path1: string, path2: string): boolean {
path1 = path.normalize(path1);
path2 = path.normalize(path2);
if (this.platformService.isWindows) {
return path1.toUpperCase() === path2.toUpperCase();
} else {
return path1 === path2;
}
}
public appendFileSync(filename: string, data: {}, encoding: string): void;
public appendFileSync(filename: string, data: {}, options?: { encoding?: string; mode?: number; flag?: string }): void;
// tslint:disable-next-line:unified-signatures
public appendFileSync(filename: string, data: {}, options?: { encoding?: string; mode?: string; flag?: string }): void;
public appendFileSync(filename: string, data: {}, optionsOrEncoding: {}): void {
return fs.appendFileSync(filename, data, optionsOrEncoding);
}
public getRealPath(filePath: string): Promise<string> {
return new Promise<string>(resolve => {
fs.realpath(filePath, (err, realPath) => {
resolve(err ? filePath : realPath);
});
});
}
public copyFile(src: string, dest: string): Promise<void> {
const deferred = createDeferred<void>();
const rs = fs.createReadStream(src).on('error', (err) => {
deferred.reject(err);
});
const ws = fs.createWriteStream(dest).on('error', (err) => {
deferred.reject(err);
}).on('close', () => {
deferred.resolve();
});
rs.pipe(ws);
return deferred.promise;
}
public deleteFile(filename: string): Promise<void> {
const deferred = createDeferred<void>();
fs.unlink(filename, err => err ? deferred.reject(err) : deferred.resolve());
return deferred.promise;
}
public getFileHash(filePath: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
fs.lstat(filePath, (err, stats) => {
if (err) {
reject(err);
} else {
const actual = createHash('sha512').update(`${stats.ctimeMs}-${stats.mtimeMs}`).digest('hex');
resolve(actual);
}
});
});
}
public search(globPattern: string): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
glob(globPattern, (ex, files) => {
if (ex) {
return reject(ex);
}
resolve(Array.isArray(files) ? files : []);
});
});
}
public createTemporaryFile(extension: string): Promise<TemporaryFile> {
return new Promise<TemporaryFile>((resolve, reject) => {
tmp.file({ postfix: extension }, (err, tmpFile, _, cleanupCallback) => {
if (err) {
return reject(err);
}
resolve({ filePath: tmpFile, dispose: cleanupCallback });
});
});
}
public createWriteStream(filePath: string): fileSystem.WriteStream {
return fileSystem.createWriteStream(filePath);
}
public chmod(filePath: string, mode: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
fileSystem.chmod(filePath, mode, (err: NodeJS.ErrnoException) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
}