forked from ionic-team/stencil
/
fs.ts
180 lines (161 loc) · 4.83 KB
/
fs.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
import type * as d from '../../../declarations';
import { basename } from 'path';
import { promisify } from './util';
import { normalizePath } from '@utils';
export interface FsObj {
__sys: d.CompilerSystem;
[key: string]: any;
}
class FsError extends Error {
constructor(public syscall: string, public path: string, public code: string = 'ENOENT', public errno: number = -2) {
super(`ENOENT: no such file or directory, ${syscall} '${path}'`);
}
}
const fs: FsObj = {
__sys: {} as any,
};
export const exists = (fs.exists = (p: string, cb: any) => {
fs.__sys
.access(p)
.then(cb)
.catch(() => cb(false));
});
// https://nodejs.org/api/util.html#util_custom_promisified_functions
(exists as any)[promisify.custom] = (p: string) => fs.__sys.access(p);
export const existsSync = (fs.existsSync = (p: string) => {
// https://nodejs.org/api/fs.html#fs_fs_existssync_path
return fs.__sys.accessSync(p);
});
export const mkdir = (fs.mkdir = (p: string, opts: any, cb: any) => {
cb = typeof cb === 'function' ? cb : typeof opts === 'function' ? opts : null;
opts = typeof opts === 'function' ? undefined : opts;
fs.__sys
.createDir(p, opts)
.then(results => {
if (cb) {
if (results.error) {
cb(new FsError('mkdir', p));
} else {
cb(null);
}
}
})
.catch(e => {
cb && cb(e);
});
});
export const mkdirSync = (fs.mkdirSync = (p: string, opts: any) => {
const results = fs.__sys.createDirSync(p, opts);
if (results.error) {
throw new FsError('mkdir', p);
}
});
export const readdirSync = (fs.readdirSync = (p: string) => {
// sys.readdirSync includes full paths
// but if fs.readdirSync was called, the expected
// nodejs results are of just the basename for each dir item
const dirItems = fs.__sys.readDirSync(p);
return dirItems.map(dirItem => basename(dirItem));
});
export const readFile = (fs.readFile = async (p: string, opts: any, cb: (err: any, data?: string) => void) => {
const encoding = typeof opts === 'object' ? opts.encoding : typeof opts === 'string' ? opts : 'utf-8';
cb = typeof cb === 'function' ? cb : typeof opts === 'function' ? opts : null;
fs.__sys
.readFile(p, encoding)
.then(data => {
if (cb) {
if (typeof data === 'string') {
cb(null, data);
} else {
cb(new FsError('open', p), data);
}
}
})
.catch(e => {
cb && cb(e);
});
});
export const readFileSync = (fs.readFileSync = (p: string, opts: any) => {
const encoding = typeof opts === 'object' ? opts.encoding : typeof opts === 'string' ? opts : 'utf-8';
const data = fs.__sys.readFileSync(p, encoding);
if (typeof data !== 'string') {
throw new FsError('open', p);
}
return data;
});
export const realpath = (fs.realpath = (p: string, opts: any, cb: (err: any, data?: string) => void) => {
cb = typeof cb === 'function' ? cb : typeof opts === 'function' ? opts : null;
fs.__sys
.realpath(p)
.then(results => {
cb && cb(results.error, results.path);
})
.catch(e => {
cb && cb(e);
});
});
export const realpathSync = (fs.realpathSync = (p: string) => {
const results = fs.__sys.realpathSync(p);
if (results.error) {
throw results.error;
}
return normalizePath(results.path);
});
export const statSync = (fs.statSync = (p: string) => {
const fsStats = fs.__sys.statSync(p);
if (fsStats.error) {
throw new FsError('statSync', p);
}
return {
isDirectory: () => fsStats.isDirectory,
isFile: () => fsStats.isFile,
isSymbolicLink: () => fsStats.isSymbolicLink,
size: fsStats.size,
mtimeMs: fsStats.mtimeMs,
};
});
export const lstatSync = (fs.lstatSync = statSync);
export const stat = (fs.stat = (p: string, opts: any, cb: any) => {
cb = typeof cb === 'function' ? cb : typeof opts === 'function' ? opts : null;
fs.__sys
.stat(p)
.then(fsStats => {
if (cb) {
if (fsStats.error) {
cb(new FsError('stat', p));
} else {
cb({
isDirectory: () => fsStats.isDirectory,
isFile: () => fsStats.isFile,
isSymbolicLink: () => fsStats.isSymbolicLink,
size: fsStats.size,
mtimeMs: fsStats.mtimeMs,
});
}
}
})
.catch(e => {
cb && cb(e);
});
});
export const watch = (fs.watch = () => {
throw new Error(`fs.watch() not implemented`);
});
export const writeFile = (fs.writeFile = (p: string, data: string, opts: any, cb: any) => {
cb = typeof cb === 'function' ? cb : typeof opts === 'function' ? opts : null;
fs.__sys
.writeFile(p, data)
.then(writeResults => {
if (cb) {
if (writeResults.error) {
cb(new FsError('writeFile', p));
} else {
cb(null);
}
}
})
.catch(e => {
cb && cb(e);
});
});
export default fs;