forked from emmetio/brackets-emmet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.js
More file actions
121 lines (107 loc) · 3.28 KB
/
file.js
File metadata and controls
121 lines (107 loc) · 3.28 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
define(['emmet'], function(emmet) {
var FileUtils = brackets.getModule('file/FileUtils');
function isURL(path) {
var re = /^https?:\/\//;
return re.test(path);
}
function startsWith(str, chars) {
return str.indexOf(chars) === 0;
}
function getFileName(path) {
var re = /([\w\.\-]+)$/i;
var m = re.exec(path);
return m ? m[1] : '';
}
function getBasePath(path) {
return path.substring(0, path.length - getFileName(path).length);
}
return {
_parseParams: function(args) {
var params = {
path: args[0],
size: 0
};
args = _.rest(args);
params.callback = _.last(args);
args = _.initial(args);
if (args.length) {
params.size = args[0];
}
return params;
},
/**
* Read file content and return it
* @param {String} path File's relative or absolute path
* @return {String}
*/
read: function(path, size, callback) {
// TODO binary reading is not implemented yet
return this.readText.apply(this, arguments);
},
/**
* Read file content and return it
* @param {String} path File's relative or absolute path
* @return {String}
*/
readText: function(path, size, callback) {
var params = this._parseParams(arguments);
brackets.fs.readFile(params.path, 'utf8', function(err, content) {
params.callback(err, content);
});
},
/**
* Locate <code>file_name</code> file that relates to <code>editor_file</code>.
* File name may be absolute or relative path
*
* <b>Dealing with absolute path.</b>
* Many modern editors have a "project" support as information unit, but you
* should not rely on project path to find file with absolute path. First,
* it requires user to create a project before using this method (and this
* is not very convenient). Second, project path doesn't always points to
* to website's document root folder: it may point, for example, to an
* upper folder which contains server-side scripts.
*
* For better result, you should use the following algorithm in locating
* absolute resources:
* 1) Get parent folder for <code>editorFile</code> as a start point
* 2) Append required <code>fileName</code> to start point and test if
* file exists
* 3) If it doesn't exists, move start point one level up (to parent folder)
* and repeat step 2.
*
* @param {String} editorFile
* @param {String} fileName
* @return {String} Returns null if <code>fileName</code> cannot be located
*/
locateFile: function(editorFile, fileName) {
// TODO implement
},
/**
* Creates absolute path by concatenating <code>parent</code> and <code>fileName</code>.
* If <code>parent</code> points to file, its parent directory is used
* @param {String} parent
* @param {String} fileName
* @return {String}
*/
createPath: function(parent, fileName, callback) {
// TODO implement
},
/**
* Saves <code>content</code> as <code>file</code>
* @param {String} file File's absolute path
* @param {String} content File content
*/
save: function(file, content) {
brackets.fs.writeFile(file, content);
},
/**
* Returns file extension in lower case
* @param {String} file
* @return {String}
*/
getExt: function(file) {
var m = (file || '').match(/\.([\w\-]+)$/);
return m ? m[1].toLowerCase() : '';
}
}
});