-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NodeFS.js
147 lines (127 loc) Β· 3.46 KB
/
NodeFS.js
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
// @flow
import type {WriteStream} from 'fs';
import type {FileOptions, FileSystem} from './types';
import type {FilePath} from '@parcel/types';
import type {
Event,
Options as WatcherOptions,
AsyncSubscription,
} from '@parcel/watcher';
import fs from 'fs';
import ncp from 'ncp';
import mkdirp from 'mkdirp';
import rimraf from 'rimraf';
import {promisify} from '@parcel/utils';
import {registerSerializableClass} from '@parcel/core';
import watcher from '@parcel/watcher';
import packageJSON from '../package.json';
// Most of this can go away once we only support Node 10+, which includes
// require('fs').promises
const realpath = promisify(fs.realpath);
export class NodeFS implements FileSystem {
readFile = promisify(fs.readFile);
copyFile = promisify(fs.copyFile);
stat = promisify(fs.stat);
readdir = promisify(fs.readdir);
unlink = promisify(fs.unlink);
utimes = promisify(fs.utimes);
mkdirp = promisify(mkdirp);
rimraf = promisify(rimraf);
ncp = promisify(ncp);
createReadStream = fs.createReadStream;
cwd = process.cwd;
chdir = process.chdir;
statSync = fs.statSync;
realpathSync = fs.realpathSync;
existsSync = fs.existsSync;
readdirSync = (fs.readdirSync: any);
createWriteStream(filePath: string, options: any): WriteStream {
let tmpFilePath = getTempFilePath(filePath);
let stream = fs.createWriteStream(tmpFilePath, {
...options,
emitClose: true,
});
stream.on('close', () => fs.renameSync(tmpFilePath, filePath));
return stream;
}
async writeFile(
filePath: FilePath,
contents: Buffer | string,
options: ?FileOptions,
): Promise<void> {
let tmpFilePath = getTempFilePath(filePath);
await fs.promises.writeFile(
tmpFilePath,
contents,
// $FlowFixMe
options,
);
await fs.promises.rename(tmpFilePath, filePath);
}
readFileSync(filePath: FilePath, encoding?: buffer$Encoding): any {
if (encoding != null) {
return fs.readFileSync(filePath, encoding);
}
return fs.readFileSync(filePath);
}
async realpath(originalPath: string): Promise<string> {
try {
return await realpath(originalPath, 'utf8');
} catch (e) {
// do nothing
}
return originalPath;
}
exists(filePath: FilePath): Promise<boolean> {
return new Promise(resolve => {
fs.exists(filePath, resolve);
});
}
watch(
dir: FilePath,
fn: (err: ?Error, events: Array<Event>) => mixed,
opts: WatcherOptions,
): Promise<AsyncSubscription> {
return watcher.subscribe(dir, fn, opts);
}
getEventsSince(
dir: FilePath,
snapshot: FilePath,
opts: WatcherOptions,
): Promise<Array<Event>> {
return watcher.getEventsSince(dir, snapshot, opts);
}
async writeSnapshot(
dir: FilePath,
snapshot: FilePath,
opts: WatcherOptions,
): Promise<void> {
await watcher.writeSnapshot(dir, snapshot, opts);
}
static deserialize() {
return new NodeFS();
}
serialize() {
return null;
}
}
registerSerializableClass(`${packageJSON.version}:NodeFS`, NodeFS);
let writeStreamCalls = 0;
let threadId;
try {
({threadId} = require('worker_threads'));
} catch {
//
}
// Generate a temporary file path used for atomic writing of files.
function getTempFilePath(filePath: FilePath) {
writeStreamCalls = writeStreamCalls % Number.MAX_SAFE_INTEGER;
return (
filePath +
'.' +
process.pid +
(threadId != null ? '.' + threadId : '') +
'.' +
(writeStreamCalls++).toString(36)
);
}