/
pfs.ts
197 lines (162 loc) · 5.93 KB
/
pfs.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { TPromise } from 'vs/base/common/winjs.base';
import * as extfs from 'vs/base/node/extfs';
import { join } from 'path';
import { nfcall, Queue } from 'vs/base/common/async';
import * as fs from 'fs';
import * as os from 'os';
import * as platform from 'vs/base/common/platform';
import { once } from 'vs/base/common/event';
export function readdir(path: string): TPromise<string[]> {
return nfcall(extfs.readdir, path);
}
export function exists(path: string): TPromise<boolean> {
return new TPromise(c => fs.exists(path, c), () => { });
}
export function chmod(path: string, mode: number): TPromise<boolean> {
return nfcall(fs.chmod, path, mode);
}
export import mkdirp = extfs.mkdirp;
export function rimraf(path: string): TPromise<void> {
return lstat(path).then(stat => {
if (stat.isDirectory() && !stat.isSymbolicLink()) {
return readdir(path)
.then(children => TPromise.join(children.map(child => rimraf(join(path, child)))))
.then(() => rmdir(path));
} else {
return unlink(path);
}
}, (err: NodeJS.ErrnoException) => {
if (err.code === 'ENOENT') {
return void 0;
}
return TPromise.wrapError<void>(err);
});
}
export function realpath(path: string): TPromise<string> {
return nfcall(extfs.realpath, path);
}
export function stat(path: string): TPromise<fs.Stats> {
return nfcall(fs.stat, path);
}
export function statLink(path: string): TPromise<{ stat: fs.Stats, isSymbolicLink: boolean }> {
return nfcall(extfs.statLink, path);
}
export function lstat(path: string): TPromise<fs.Stats> {
return nfcall(fs.lstat, path);
}
export function rename(oldPath: string, newPath: string): TPromise<void> {
return nfcall(fs.rename, oldPath, newPath);
}
export function rmdir(path: string): TPromise<void> {
return nfcall(fs.rmdir, path);
}
export function unlink(path: string): TPromise<void> {
return nfcall(fs.unlink, path);
}
export function symlink(target: string, path: string, type?: string): TPromise<void> {
return nfcall<void>(fs.symlink, target, path, type);
}
export function readlink(path: string): TPromise<string> {
return nfcall<string>(fs.readlink, path);
}
export function truncate(path: string, len: number): TPromise<void> {
return nfcall(fs.truncate, path, len);
}
export function readFile(path: string): TPromise<Buffer>;
export function readFile(path: string, encoding: string): TPromise<string>;
export function readFile(path: string, encoding?: string): TPromise<Buffer | string> {
return nfcall(fs.readFile, path, encoding);
}
// According to node.js docs (https://nodejs.org/docs/v6.5.0/api/fs.html#fs_fs_writefile_file_data_options_callback)
// it is not safe to call writeFile() on the same path multiple times without waiting for the callback to return.
// Therefor we use a Queue on the path that is given to us to sequentialize calls to the same path properly.
const writeFilePathQueue: { [path: string]: Queue<void> } = Object.create(null);
export function writeFile(path: string, data: string, options?: extfs.IWriteFileOptions): TPromise<void>;
export function writeFile(path: string, data: Buffer, options?: extfs.IWriteFileOptions): TPromise<void>;
export function writeFile(path: string, data: Uint8Array, options?: extfs.IWriteFileOptions): TPromise<void>;
export function writeFile(path: string, data: NodeJS.ReadableStream, options?: extfs.IWriteFileOptions): TPromise<void>;
export function writeFile(path: string, data: any, options?: extfs.IWriteFileOptions): TPromise<void> {
const queueKey = toQueueKey(path);
return ensureWriteFileQueue(queueKey).queue(() => nfcall(extfs.writeFileAndFlush, path, data, options));
}
function toQueueKey(path: string): string {
let queueKey = path;
if (platform.isWindows || platform.isMacintosh) {
queueKey = queueKey.toLowerCase(); // accomodate for case insensitive file systems
}
return queueKey;
}
function ensureWriteFileQueue(queueKey: string): Queue<void> {
let writeFileQueue = writeFilePathQueue[queueKey];
if (!writeFileQueue) {
writeFileQueue = new Queue<void>();
writeFilePathQueue[queueKey] = writeFileQueue;
const onFinish = once(writeFileQueue.onFinished);
onFinish(() => {
delete writeFilePathQueue[queueKey];
writeFileQueue.dispose();
});
}
return writeFileQueue;
}
/**
* Read a dir and return only subfolders
*/
export function readDirsInDir(dirPath: string): TPromise<string[]> {
return readdir(dirPath).then(children => {
return TPromise.join(children.map(c => dirExists(join(dirPath, c)))).then(exists => {
return children.filter((_, i) => exists[i]);
});
});
}
/**
* `path` exists and is a directory
*/
export function dirExists(path: string): TPromise<boolean> {
return stat(path).then(stat => stat.isDirectory(), () => false);
}
/**
* `path` exists and is a file.
*/
export function fileExists(path: string): TPromise<boolean> {
return stat(path).then(stat => stat.isFile(), () => false);
}
/**
* Deletes a path from disk.
*/
let _tmpDir: string = null;
function getTmpDir(): string {
if (!_tmpDir) {
_tmpDir = os.tmpdir();
}
return _tmpDir;
}
export function del(path: string, tmp = getTmpDir()): TPromise<void> {
return nfcall(extfs.del, path, tmp);
}
export function whenDeleted(path: string): TPromise<void> {
// Complete when wait marker file is deleted
return new TPromise<void>(c => {
let running = false;
const interval = setInterval(() => {
if (!running) {
running = true;
fs.exists(path, exists => {
running = false;
if (!exists) {
clearInterval(interval);
c(null);
}
});
}
}, 1000);
});
}
export function copy(source: string, target: string): TPromise<void> {
return nfcall(extfs.copy, source, target);
}