-
Notifications
You must be signed in to change notification settings - Fork 73
/
fs-boot.js
318 lines (290 loc) · 8.76 KB
/
fs-boot.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* global process:false, FS_BOOT:true */
(function (exports) {
// -- kriskowal Kris Kowal Copyright (C) 2009-2010 MIT License
// -- tlrobinson Tom Robinson TODO
/**
* Pure JavaScript implementations of file system path
* manipulation.
*/
// NOTE: this file may be used is the engine bootstrapping
// process, so any "requires" must be accounted for in
// narwhal.js
/*whatsupdoc*/
/*markup markdown*/
var ESCAPE_REG_EXP = /[-[\]{}()*+?.\\^$|,#\s]/g;
var regExpEscape = function (str) {
return str.replace(ESCAPE_REG_EXP, "\\$&");
};
var path = require("path");
/**
* @name ROOT
* * `/` on Unix
* * `\` on Windows
*/
/**
* @name SEPARATOR
* * `/` on Unix
* * `\` on Windows
*/
/**
* @name ALT_SEPARATOR
* * undefined on Unix
* * `/` on Windows
*/
exports.ROOT = exports.SEPARATOR = path.sep || (process.platform === "win32" ? "\\": "/");
if (path.sep === "\\") {
exports.ALT_SEPARATOR = "/";
} else {
exports.ALT_SEPARATOR = undefined;
}
// we need to make sure the separator regex is always in sync with the separators.
// this caches the generated regex and rebuild if either separator changes.
var separatorCached, altSeparatorCached, separatorReCached;
/**
* @function
*/
exports.SEPARATORS_RE = function () {
if (
separatorCached !== exports.SEPARATOR ||
altSeparatorCached !== exports.ALT_SEPARATOR
) {
separatorCached = exports.SEPARATOR;
altSeparatorCached = exports.ALT_SEPARATOR;
separatorReCached = new RegExp("[" +
(separatorCached || "").replace(ESCAPE_REG_EXP, "\\$&") +
(altSeparatorCached || "").replace(ESCAPE_REG_EXP, "\\$&") +
"]", "g");
}
return separatorReCached;
}
/**
* separates a path into components. If the path is
* absolute, the first path component is the root of the
* file system, indicated by an empty string on Unix, and a
* drive letter followed by a colon on Windows.
* @returns {Array * String}
*/
exports.split = function (path) {
var parts;
try {
parts = String(path).split(exports.SEPARATORS_RE());
} catch (exception) {
throw new Error("Cannot split " + (typeof path) + ", " + JSON.stringify(path));
}
// this special case helps isAbsolute
// distinguish an empty path from an absolute path
// "" -> [] NOT [""]
if (parts.length === 1 && parts[0] === "")
return [];
// "a" -> ["a"]
// "/a" -> ["", "a"]
return parts;
};
/**
* Takes file system paths as variadic arguments and treats
* each as a file or directory path and returns the path
* arrived by traversing into the those paths. All
* arguments except for the last must be paths to
* directories for the result to be meaningful.
* @returns {String} path
*/
exports.join = function () {
if (arguments.length === 1 && Array.isArray(arguments[0]))
return exports.normal.apply(exports, arguments[0]);
return exports.normal.apply(exports, arguments);
};
/**
* Takes file system paths as variadic arguments and treats
* each path as a location, in the URL sense, resolving each
* new location based on the previous. For example, if the
* first argument is the absolute path of a JSON file, and
* the second argument is a path mentioned in that JSON
* file, `resolve` returns the absolute path of the
* mentioned file.
* @returns {String} path
*/
exports.resolve = function () {
var root = "";
var parents = [];
var children = [];
var leaf = "";
var path;
for (var i = 0; i < arguments.length; i++) {
path = String(arguments[i]);
if (path == "")
continue;
var parts = path.split(exports.SEPARATORS_RE());
if (exports.isAbsolute(path)) {
root = parts.shift() + exports.SEPARATOR;
parents = [];
children = [];
}
leaf = parts.pop();
if (leaf == "." || leaf == "..") {
parts.push(leaf);
leaf = "";
}
for (var j = 0; j < parts.length; j++) {
var part = parts[j];
if (part == "." || part == "") {
} else if (part == "..") {
if (children.length) {
children.pop();
} else {
if (root) {
} else {
parents.push("..");
}
}
} else {
children.push(part);
}
}
}
path = parents.concat(children).join(exports.SEPARATOR);
if (path) {
leaf = exports.SEPARATOR + leaf;
}
return root + path + leaf;
};
/**
* Takes paths as any number of arguments and reduces them
* into a single path in normal form, removing all "." path
* components, and reducing ".." path components by removing
* the previous path component if possible.
* @returns {String} path
*/
exports.normal = function () {
var root = "";
var parents = [];
var children = [];
var path;
for (var i = 0, ii = arguments.length; i < ii; i++) {
path = String(arguments[i]);
// empty paths have no affect
if (path === "")
continue;
var parts = path.split(exports.SEPARATORS_RE());
if (exports.isAbsolute(path)) {
root = parts.shift() + exports.SEPARATOR;
parents = [];
children = [];
}
for (var j = 0, jj = parts.length; j < jj; j++) {
var part = parts[j];
if (part === "." || part === "") {
} else if (part == "..") {
if (children.length) {
children.pop();
} else {
if (root) {
} else {
parents.push("..");
}
}
} else {
children.push(part);
}
}
}
path = parents.concat(children).join(exports.SEPARATOR);
return root + path;
};
/***
* @returns {Boolean} whether the given path begins at the
* root of the file system or a drive letter.
*/
exports.isAbsolute = function (path) {
// for absolute paths on any operating system,
// the first path component always determines
// whether it is relative or absolute. On Unix,
// it is empty, so ["", "foo"].join("/") == "/foo",
// "/foo".split("/") == ["", "foo"].
var parts = exports.split(path);
// split("") == []. "" is not absolute.
// split("/") == ["", ""] is absolute.
// split(?) == [""] does not occur.
if (parts.length == 0)
return false;
return exports.isRoot(parts[0]);
};
/**
* @returns {Boolean} whether the given path does not begin
* at the root of the file system or a drive letter.
*/
exports.isRelative = function (path) {
return !exports.isAbsolute(path);
};
/**
* @returns {Boolean} whether the given path component
* corresponds to the root of the file system or a drive
* letter, as applicable.
*/
exports.isRoot = function (first) {
if (exports.SEPARATOR === "\\") {
return /[a-zA-Z]:$/.test(first);
} else {
return first == "";
}
};
/**
* @returns {String} the Unix root path or corresponding
* Windows drive for a given path.
*/
exports.root = function (path) {
if (!exports.isAbsolute(path))
path = require("./fs").absolute(path);
var parts = exports.split(path);
return exports.join(parts[0], "");
};
/**
* @returns {String} the parent directory of the given path.
*/
exports.directory = function (path) {
path = exports.normal(path);
var absolute = exports.isAbsolute(path);
var parts = exports.split(path);
// XXX needs to be sensitive to the root for
// Windows compatibility
if (parts.length) {
if (parts[parts.length - 1] == "..") {
parts.push("..");
} else {
parts.pop();
}
} else {
parts.unshift("..");
}
return parts.join(exports.SEPARATOR) || (
exports.isRelative(path) ?
"" : exports.ROOT
);
};
/**
* @returns {String} the last component of a path, without
* the given extension if the extension is provided and
* matches the given file.
* @param {String} path
* @param {String} extention an optional extention to detect
* and remove if it exists.
*/
exports.base = function (path, extension) {
var base = path.split(exports.SEPARATORS_RE()).pop();
if (extension)
base = base.replace(
new RegExp(regExpEscape(extension) + "$"),
""
);
return base;
};
/**
* @returns {String} the extension (e.g., `txt`) of the file
* at the given path.
*/
exports.extension = function (path) {
path = exports.base(path);
path = path.replace(/^\.*/, "");
var index = path.lastIndexOf(".");
return index <= 0 ? "" : path.substring(index);
};
})(typeof exports !== "undefined" ? exports : FS_BOOT = {});