forked from adobe/brackets-app
/
brackets_extensions.js
244 lines (225 loc) · 8.89 KB
/
brackets_extensions.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
// This is the JavaScript code for bridging to native functionality
// See brackets_extentions.mm for implementation of native methods.
//
// Note: All file native file i/o functions are synchronous, but are exposed
// here as asynchronous calls.
var brackets;
if (!brackets)
brackets = {};
if (!brackets.fs)
brackets.fs = {};
(function() {
// Internal function to get the last error code.
function getLastError() {
native function GetLastError();
return GetLastError();
}
// Error values. These MUST be in sync with the error values
// at the top of brackets_extensions.mm.
/**
* @constant No error.
*/
brackets.fs.NO_ERROR = 0;
/**
* @constant Unknown error occurred.
*/
brackets.fs.ERR_UNKNOWN = 1;
/**
* @constant Invalid parameters passed to function.
*/
brackets.fs.ERR_INVALID_PARAMS = 2;
/**
* @constant File or directory was not found.
*/
brackets.fs.ERR_NOT_FOUND = 3;
/**
* @constant File or directory could not be read.
*/
brackets.fs.ERR_CANT_READ = 4;
/**
* @constant An unsupported encoding value was specified.
*/
brackets.fs.ERR_UNSUPPORTED_ENCODING = 5;
/**
* @constant File could not be written.
*/
brackets.fs.ERR_CANT_WRITE = 6;
/**
* @constant Target directory is out of space. File could not be written.
*/
brackets.fs.ERR_OUT_OF_SPACE = 7;
/**
* @constant Specified path does not point to a file.
*/
brackets.fs.ERR_NOT_FILE = 8;
/**
* @constant Specified path does not point to a directory.
*/
brackets.fs.ERR_NOT_DIRECTORY = 9;
/**
* Display the OS File Open dialog, allowing the user to select
* files or directories.
*
* @param {boolean} allowMultipleSelection If true, multiple files/directories can be selected.
* @param {boolean} chooseDirectory If true, only directories can be selected. If false, only
* files can be selected.
* @param {string} title Tile of the open dialog.
* @param {string} initialPath Initial path to display in the dialog. Pass NULL or "" to
* display the last path chosen.
* @param {Array.<string>} fileTypes Array of strings specifying the selectable file extensions.
* These strings should not contain '.'. This parameter is ignored when
* chooseDirectory=true.
* @param {function(err, selection)} callback Asynchronous callback function. The callback gets two arguments
* (err, selection) where selection is an array of the names of the selected files.
* Possible error values:
* NO_ERROR
* ERR_INVALID_PARAMS
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
brackets.fs.showOpenDialog = function(allowMultipleSelection, chooseDirectory, title, initialPath, fileTypes, callback) {
native function ShowOpenDialog();
var resultString = ShowOpenDialog(allowMultipleSelection, chooseDirectory,
title || 'Open', initialPath || '',
fileTypes ? fileTypes.join(' ') : '');
var result = JSON.parse(resultString || '[]');
callback(getLastError(), result);
};
/**
* Reads the contents of a directory.
*
* @param {string} path The path of the directory to read.
* @param {function(err, files)} callback Asynchronous callback function. The callback gets two arguments
* (err, files) where files is an array of the names of the files
* in the directory excluding '.' and '..'.
* Possible error values:
* NO_ERROR
* ERR_UNKNOWN
* ERR_INVALID_PARAMS
* ERR_NOT_FOUND
* ERR_CANT_READ
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
brackets.fs.readdir = function(path, callback) {
native function ReadDir();
var resultString = ReadDir(path);
var result = JSON.parse(resultString || '[]');
callback(getLastError(), result);
};
/**
* Get information for the selected file or directory.
*
* @param {string} path The path of the file or directory to read.
* @param {function(err, stats)} callback Asynchronous callback function. The callback gets two arguments
* (err, stats) where stats is an object with isFile() and isDirectory() functions.
* Possible error values:
* NO_ERROR
* ERR_UNKNOWN
* ERR_INVALID_PARAMS
* ERR_NOT_FOUND
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
brackets.fs.stat = function(path, callback) {
native function IsDirectory();
var isDir = IsDirectory(path);
callback(getLastError(), {
isFile: function() {
return !isDir;
},
isDirectory: function() {
return isDir;
}
});
};
/**
* Reads the entire contents of a file.
*
* @param {string} path The path of the file to read.
* @param {string} encoding The encoding for the file. The only supported encoding is 'utf8'.
* @param {function(err, data)} callback Asynchronous callback function. The callback gets two arguments
* (err, data) where data is the contents of the file.
* Possible error values:
* NO_ERROR
* ERR_UNKNOWN
* ERR_INVALID_PARAMS
* ERR_NOT_FOUND
* ERR_CANT_READ
* ERR_UNSUPPORTED_ENCODING
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
brackets.fs.readFile = function(path, encoding, callback) {
native function ReadFile();
var contents = ReadFile(path, encoding);
callback(getLastError(), contents);
};
/**
* Write data to a file, replacing the file if it already exists.
*
* @param {string} path The path of the file to write.
* @param {string} data The data to write to the file.
* @param {string} encoding The encoding for the file. The only supported encoding is 'utf8'.
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
* Possible error values:
* NO_ERROR
* ERR_UNKNOWN
* ERR_INVALID_PARAMS
* ERR_UNSUPPORTED_ENCODING
* ERR_CANT_WRITE
* ERR_OUT_OF_SPACE
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
brackets.fs.writeFile = function(path, data, encoding, callback) {
native function WriteFile();
WriteFile(path, data, encoding);
if (callback)
callback(getLastError());
};
/**
* Set permissions for a file or directory.
*
* @param {string} path The path of the file or directory
* @param {number} mode The permissions for the file or directory, in numeric format (ie 0777)
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
* Possible error values:
* NO_ERROR
* ERR_UNKNOWN
* ERR_INVALID_PARAMS
* ERR_CANT_WRITE
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
brackets.fs.chmod = function(path, mode, callback) {
native function SetPosixPermissions();
SetPosixPermissions(path, mode);
callback(getLastError());
};
/**
* Delete a file.
*
* @param {string} path The path of the file to delete
* @param {function(err)} callback Asynchronous callback function. The callback gets one argument (err).
* Possible error values:
* NO_ERROR
* ERR_UNKNOWN
* ERR_INVALID_PARAMS
* ERR_NOT_FOUND
* ERR_NOT_FILE
*
* @return None. This is an asynchronous call that sends all return information to the callback.
*/
brackets.fs.unlink = function(path, callback) {
native function DeleteFileOrDirectory();
native function IsDirectory();
// Unlink can only delete files
if (IsDirectory(path)) {
callback(brackets.fs.ERR_NOT_FILE);
return;
}
DeleteFileOrDirectory(path);
callback(getLastError());
};
})();;