This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathFileUtils.js
More file actions
466 lines (411 loc) · 17.6 KB
/
FileUtils.js
File metadata and controls
466 lines (411 loc) · 17.6 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, FileError, brackets, unescape, window */
/**
* Set of utilites for working with files and text content.
*/
define(function (require, exports, module) {
"use strict";
require("utils/Global");
var FileSystemError = require("filesystem/FileSystemError"),
LanguageManager = require("language/LanguageManager"),
PerfUtils = require("utils/PerfUtils"),
Dialogs = require("widgets/Dialogs"),
DefaultDialogs = require("widgets/DefaultDialogs"),
Strings = require("strings"),
StringUtils = require("utils/StringUtils");
/**
* Asynchronously reads a file as UTF-8 encoded text.
* @param {!File} file File to read
* @return {$.Promise} a jQuery promise that will be resolved with the
* file's text content plus its timestamp, or rejected with a FileSystemError string
* constant if the file can not be read.
*/
function readAsText(file) {
var result = new $.Deferred();
// Measure performance
var perfTimerName = PerfUtils.markStart("readAsText:\t" + file.fullPath);
result.always(function () {
PerfUtils.addMeasurement(perfTimerName);
});
// Read file
file.read(function (err, data, stat) {
if (!err) {
result.resolve(data, stat.mtime);
} else {
result.reject(err);
}
});
return result.promise();
}
/**
* Asynchronously writes a file as UTF-8 encoded text.
* @param {!File} file File to write
* @param {!string} text
* @param {boolean=} allowBlindWrite Indicates whether or not CONTENTS_MODIFIED
* errors---which can be triggered if the actual file contents differ from
* the FileSystem's last-known contents---should be ignored.
* @return {$.Promise} a jQuery promise that will be resolved when
* file writing completes, or rejected with a FileSystemError string constant.
*/
function writeText(file, text, allowBlindWrite) {
var result = new $.Deferred(),
options = {};
if (allowBlindWrite) {
options.blind = true;
}
file.write(text, options, function (err) {
if (!err) {
result.resolve();
} else {
result.reject(err);
}
});
return result.promise();
}
/** @const */
var LINE_ENDINGS_CRLF = "CRLF";
/** @const */
var LINE_ENDINGS_LF = "LF";
/**
* Returns the standard line endings for the current platform
* @return {LINE_ENDINGS_CRLF|LINE_ENDINGS_LF}
*/
function getPlatformLineEndings() {
return brackets.platform === "win" ? LINE_ENDINGS_CRLF : LINE_ENDINGS_LF;
}
/**
* Scans the first 1000 chars of the text to determine how it encodes line endings. Returns
* null if usage is mixed or if no line endings found.
* @param {!string} text
* @return {null|LINE_ENDINGS_CRLF|LINE_ENDINGS_LF}
*/
function sniffLineEndings(text) {
var subset = text.substr(0, 1000); // (length is clipped to text.length)
var hasCRLF = /\r\n/.test(subset);
var hasLF = /[^\r]\n/.test(subset);
if ((hasCRLF && hasLF) || (!hasCRLF && !hasLF)) {
return null;
} else {
return hasCRLF ? LINE_ENDINGS_CRLF : LINE_ENDINGS_LF;
}
}
/**
* Translates any line ending types in the given text to the be the single form specified
* @param {!string} text
* @param {null|LINE_ENDINGS_CRLF|LINE_ENDINGS_LF} lineEndings
* @return {string}
*/
function translateLineEndings(text, lineEndings) {
if (lineEndings !== LINE_ENDINGS_CRLF && lineEndings !== LINE_ENDINGS_LF) {
lineEndings = getPlatformLineEndings();
}
var eolStr = (lineEndings === LINE_ENDINGS_CRLF ? "\r\n" : "\n");
var findAnyEol = /\r\n|\r|\n/g;
return text.replace(findAnyEol, eolStr);
}
function getFileErrorString(name) {
// There are a few error codes that we have specific error messages for. The rest are
// displayed with a generic "(error N)" message.
var result;
if (name === FileSystemError.NOT_FOUND) {
result = Strings.NOT_FOUND_ERR;
} else if (name === FileSystemError.NOT_READABLE) {
result = Strings.NOT_READABLE_ERR;
} else if (name === FileSystemError.NOT_WRITABLE) {
result = Strings.NO_MODIFICATION_ALLOWED_ERR_FILE;
} else if (name === FileSystemError.CONTENTS_MODIFIED) {
result = Strings.CONTENTS_MODIFIED_ERR;
} else {
result = StringUtils.format(Strings.GENERIC_ERROR, name);
}
return result;
}
function showFileOpenError(name, path) {
return Dialogs.showModalDialog(
DefaultDialogs.DIALOG_ID_ERROR,
Strings.ERROR_OPENING_FILE_TITLE,
StringUtils.format(
Strings.ERROR_OPENING_FILE,
StringUtils.breakableUrl(path),
getFileErrorString(name)
)
);
}
/**
* Convert a URI path to a native path.
* On both platforms, this unescapes the URI
* On windows, URI paths start with a "/", but have a drive letter ("C:"). In this
* case, remove the initial "/".
* @param {!string} path
* @return {string}
*/
function convertToNativePath(path) {
path = unescape(path);
if (path.indexOf(":") !== -1 && path[0] === "/") {
return path.substr(1);
}
return path;
}
/**
* Convert a Windows-native path to use Unix style slashes.
* On Windows, this converts "C:\foo\bar\baz.txt" to "C:/foo/bar/baz.txt".
* On Mac, this does nothing, since Mac paths are already in Unix syntax.
* (Note that this does not add an initial forward-slash. Internally, our
* APIs generally use the "C:/foo/bar/baz.txt" style for "native" paths.)
* @param {string} path A native-style path.
* @return {string} A Unix-style path.
*/
function convertWindowsPathToUnixPath(path) {
if (brackets.platform === "win") {
path = path.replace(/\\/g, "/");
}
return path;
}
/**
* Removes the trailing slash from a path, if it has one.
* Warning: this differs from the format of most paths used in Brackets! Use paths ending in "/"
* normally, as this is the format used by Directory.fullPath.
*
* @param {string} path
* @return {string}
*/
function stripTrailingSlash(path) {
if (path && path[path.length - 1] === "/") {
return path.slice(0, -1);
} else {
return path;
}
}
/**
* Warning: Contrary to the name, this does NOT return a canonical path. The canonical format
* used by Directory.fullPath actually DOES include the trailing "/"
* @deprecated
*
* @param {string} path
* @return {string}
*/
function canonicalizeFolderPath(path) {
console.error("Warning: FileUtils.canonicalizeFolderPath() is deprecated. Use paths ending in '/' if possible, like Directory.fullPath");
return stripTrailingSlash(path);
}
/**
* Get the name of a file or a directory, removing any preceding path.
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the base name of a file or the name of a
* directory
*/
function getBaseName(fullPath) {
var lastSlash = fullPath.lastIndexOf("/");
if (lastSlash === fullPath.length - 1) { // directory: exclude trailing "/" too
return fullPath.slice(fullPath.lastIndexOf("/", fullPath.length - 2) + 1, -1);
} else {
return fullPath.slice(lastSlash + 1);
}
}
/**
* Returns a native absolute path to the 'brackets' source directory.
* Note that this only works when run in brackets/src/index.html, so it does
* not work for unit tests (which is run from brackets/test/SpecRunner.html)
*
* WARNING: unlike most paths in Brackets, this path EXCLUDES the trailing "/".
* @return {string}
*/
function getNativeBracketsDirectoryPath() {
var pathname = decodeURI(window.location.pathname);
var directory = pathname.substr(0, pathname.lastIndexOf("/"));
return convertToNativePath(directory);
}
/**
* Given the module object passed to JS module define function,
* convert the path to a native absolute path.
* Returns a native absolute path to the module folder.
*
* WARNING: unlike most paths in Brackets, this path EXCLUDES the trailing "/".
* @return {string}
*/
function getNativeModuleDirectoryPath(module) {
var path;
if (module && module.uri) {
path = decodeURI(module.uri);
// Remove module name and trailing slash from path.
path = path.substr(0, path.lastIndexOf("/"));
}
return path;
}
/**
* Get the file extension (excluding ".") given a path OR a bare filename.
* Returns "" for names with no extension. If the name starts with ".", the
* full remaining text is considered the extension.
*
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the extension of a filename or empty string if
* the argument is a directory or a filename with no extension
*/
function getFileExtension(fullPath) {
var baseName = getBaseName(fullPath),
idx = baseName.lastIndexOf(".");
if (idx === -1) {
return "";
}
return baseName.substr(idx + 1);
}
/**
* Get the file extension (excluding ".") given a path OR a bare filename.
* Returns "" for names with no extension.
* If the only `.` in the file is the first character,
* returns "" as this is not considered an extension.
* This method considers known extensions which include `.` in them.
*
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the extension of a filename or empty string if
* the argument is a directory or a filename with no extension
*/
function getSmartFileExtension(fullPath) {
var baseName = getBaseName(fullPath),
parts = baseName.split(".");
// get rid of file name
parts.shift();
if (baseName[0] === ".") {
// if starts with a `.`, then still consider it as file name
parts.shift();
}
var extension = [parts.pop()], // last part is always an extension
i = parts.length;
while (i--) {
if (LanguageManager.getLanguageForExtension(parts[i])) {
extension.unshift(parts[i]);
} else {
break;
}
}
return extension.join(".");
}
/**
* Computes filename as relative to the basePath. For example:
* basePath: /foo/bar/, filename: /foo/bar/baz.txt
* returns: baz.txt
*
* The net effect is that the common prefix is stripped away. If basePath is not
* a prefix of filename, then undefined is returned.
*
* @param {string} basePath Path against which we're computing the relative path
* @param {string} filename Full path to the file for which we are computing a relative path
* @return {string} relative path
*/
function getRelativeFilename(basePath, filename) {
if (!filename || filename.substr(0, basePath.length) !== basePath) {
return;
}
return filename.substr(basePath.length);
}
/** @const - hard-coded for now, but may want to make these preferences */
var _staticHtmlFileExts = ["htm", "html"],
_serverHtmlFileExts = ["php", "php3", "php4", "php5", "phtm", "phtml", "cfm", "cfml", "asp", "aspx", "jsp", "jspx", "shtm", "shtml"];
/**
* Determine if file extension is a static html file extension.
* @param {string} filePath could be a path, a file name or just a file extension
* @return {boolean} Returns true if fileExt is in the list
*/
function isStaticHtmlFileExt(filePath) {
if (!filePath) {
return false;
}
return (_staticHtmlFileExts.indexOf(getFileExtension(filePath).toLowerCase()) !== -1);
}
/**
* Determine if file extension is a server html file extension.
* @param {string} filePath could be a path, a file name or just a file extension
* @return {boolean} Returns true if fileExt is in the list
*/
function isServerHtmlFileExt(filePath) {
if (!filePath) {
return false;
}
return (_serverHtmlFileExts.indexOf(getFileExtension(filePath).toLowerCase()) !== -1);
}
/**
* Get the parent directory of a file. If a directory is passed in the directory is returned.
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the path to the parent directory of a file or the path of a directory,
* including trailing "/"
*/
function getDirectoryPath(fullPath) {
return fullPath.substr(0, fullPath.lastIndexOf("/") + 1);
}
/**
* @private
* Get the file name without the extension.
* @param {string} filename File name of a file or directory
* @return {string} Returns the file name without the extension
*/
function _getFilenameWithoutExtension(filename) {
var index = filename.lastIndexOf(".");
return index === -1 ? filename : filename.slice(0, index);
}
/**
* Compares 2 filenames in lowercases. In Windows it compares the names without the
* extension first and then the extensions to fix issue #4409
* @param {string} filename1
* @param {string} filename2
* @param {boolean} extFirst If true it compares the extensions first and then the file names.
* @return {number} The result of the local compare function
*/
function compareFilenames(filename1, filename2, extFirst) {
var ext1 = getFileExtension(filename1),
ext2 = getFileExtension(filename2),
cmpExt = ext1.toLocaleLowerCase().localeCompare(ext2.toLocaleLowerCase(), undefined, {numeric: true}),
cmpNames;
if (brackets.platform === "win") {
filename1 = _getFilenameWithoutExtension(filename1);
filename2 = _getFilenameWithoutExtension(filename2);
}
cmpNames = filename1.toLocaleLowerCase().localeCompare(filename2.toLocaleLowerCase(), undefined, {numeric: true});
return extFirst ? (cmpExt || cmpNames) : (cmpNames || cmpExt);
}
// Define public API
exports.LINE_ENDINGS_CRLF = LINE_ENDINGS_CRLF;
exports.LINE_ENDINGS_LF = LINE_ENDINGS_LF;
exports.getPlatformLineEndings = getPlatformLineEndings;
exports.sniffLineEndings = sniffLineEndings;
exports.translateLineEndings = translateLineEndings;
exports.showFileOpenError = showFileOpenError;
exports.getFileErrorString = getFileErrorString;
exports.readAsText = readAsText;
exports.writeText = writeText;
exports.convertToNativePath = convertToNativePath;
exports.convertWindowsPathToUnixPath = convertWindowsPathToUnixPath;
exports.getNativeBracketsDirectoryPath = getNativeBracketsDirectoryPath;
exports.getNativeModuleDirectoryPath = getNativeModuleDirectoryPath;
exports.canonicalizeFolderPath = canonicalizeFolderPath;
exports.stripTrailingSlash = stripTrailingSlash;
exports.isStaticHtmlFileExt = isStaticHtmlFileExt;
exports.isServerHtmlFileExt = isServerHtmlFileExt;
exports.getDirectoryPath = getDirectoryPath;
exports.getBaseName = getBaseName;
exports.getRelativeFilename = getRelativeFilename;
exports.getFileExtension = getFileExtension;
exports.getSmartFileExtension = getSmartFileExtension;
exports.compareFilenames = compareFilenames;
});