forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFolderAdapter.ts
More file actions
177 lines (164 loc) · 5.08 KB
/
FolderAdapter.ts
File metadata and controls
177 lines (164 loc) · 5.08 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
import {BaseFileSystem, FileSystem, BFSCallback, FileSystemOptions} from '../core/file_system';
import * as path from 'path';
import {ApiError} from '../core/api_error';
/**
* Configuration options for a FolderAdapter file system.
*/
export interface FolderAdapterOptions {
// The folder to use as the root directory.
folder: string;
// The file system to wrap.
wrapped: FileSystem;
}
/**
* The FolderAdapter file system wraps a file system, and scopes all interactions to a subfolder of that file system.
*
* Example: Given a file system `foo` with folder `bar` and file `bar/baz`...
*
* ```javascript
* BrowserFS.configure({
* fs: "FolderAdapter",
* options: {
* folder: "bar",
* wrapped: foo
* }
* }, function(e) {
* var fs = BrowserFS.BFSRequire('fs');
* fs.readdirSync('/'); // ['baz']
* });
* ```
*/
export default class FolderAdapter extends BaseFileSystem implements FileSystem {
public static readonly Name = "FolderAdapter";
public static readonly Options: FileSystemOptions = {
folder: {
type: "string",
description: "The folder to use as the root directory"
},
wrapped: {
type: "object",
description: "The file system to wrap"
}
};
/**
* Creates a FolderAdapter instance with the given options.
*/
public static Create(opts: FolderAdapterOptions, cb: BFSCallback<FolderAdapter>): void {
const fa = new FolderAdapter(opts.folder, opts.wrapped);
fa._initialize(function(e?) {
if (e) {
cb(e);
} else {
cb(null, fa);
}
});
}
public static isAvailable(): boolean {
return true;
}
public _wrapped: FileSystem;
public _folder: string;
private constructor(folder: string, wrapped: FileSystem) {
super();
this._folder = folder;
this._wrapped = wrapped;
}
public getName(): string { return this._wrapped.getName(); }
public isReadOnly(): boolean { return this._wrapped.isReadOnly(); }
public supportsProps(): boolean { return this._wrapped.supportsProps(); }
public supportsSynch(): boolean { return this._wrapped.supportsSynch(); }
public supportsLinks(): boolean { return false; }
/**
* Initialize the file system. Ensures that the wrapped file system
* has the given folder.
*/
private _initialize(cb: (e?: ApiError) => void) {
this._wrapped.exists(this._folder, (exists: boolean) => {
if (exists) {
cb();
} else if (this._wrapped.isReadOnly()) {
cb(ApiError.ENOENT(this._folder));
} else {
this._wrapped.mkdir(this._folder, 0x1ff, cb);
}
});
}
}
/**
* @hidden
*/
function translateError(folder: string, e: any): any {
if (e !== null && typeof e === 'object') {
const err = <ApiError> e;
let p = err.path;
if (p) {
p = '/' + path.relative(folder, p);
err.message = err.message.replace(err.path!, p);
err.path = p;
}
}
return e;
}
/**
* @hidden
*/
function wrapCallback(folder: string, cb: any): any {
if (typeof cb === 'function') {
return function(err: ApiError) {
if (arguments.length > 0) {
arguments[0] = translateError(folder, err);
}
(<Function> cb).apply(null, arguments);
};
} else {
return cb;
}
}
/**
* @hidden
*/
function wrapFunction(name: string, wrapFirst: boolean, wrapSecond: boolean): Function {
if (name.slice(name.length - 4) !== 'Sync') {
// Async function. Translate error in callback.
return function(this: FolderAdapter) {
if (arguments.length > 0) {
if (wrapFirst) {
arguments[0] = path.join(this._folder, arguments[0]);
}
if (wrapSecond) {
arguments[1] = path.join(this._folder, arguments[1]);
}
arguments[arguments.length - 1] = wrapCallback(this._folder, arguments[arguments.length - 1]);
}
return (<any> this._wrapped)[name].apply(this._wrapped, arguments);
};
} else {
// Sync function. Translate error in catch.
return function(this: FolderAdapter) {
try {
if (wrapFirst) {
arguments[0] = path.join(this._folder, arguments[0]);
}
if (wrapSecond) {
arguments[1] = path.join(this._folder, arguments[1]);
}
return (<any> this._wrapped)[name].apply(this._wrapped, arguments);
} catch (e) {
throw translateError(this._folder, e);
}
};
}
}
// First argument is a path.
['diskSpace', 'stat', 'statSync', 'open', 'openSync', 'unlink', 'unlinkSync',
'rmdir', 'rmdirSync', 'mkdir', 'mkdirSync', 'readdir', 'readdirSync', 'exists',
'existsSync', 'realpath', 'realpathSync', 'truncate', 'truncateSync', 'readFile',
'readFileSync', 'writeFile', 'writeFileSync', 'appendFile', 'appendFileSync',
'chmod', 'chmodSync', 'chown', 'chownSync', 'utimes', 'utimesSync', 'readlink',
'readlinkSync'].forEach((name: string) => {
(<any> FolderAdapter.prototype)[name] = wrapFunction(name, true, false);
});
// First and second arguments are paths.
['rename', 'renameSync', 'link', 'linkSync', 'symlink', 'symlinkSync'].forEach((name: string) => {
(<any> FolderAdapter.prototype)[name] = wrapFunction(name, true, true);
});