-
Notifications
You must be signed in to change notification settings - Fork 20
/
Settings.js
336 lines (278 loc) · 9.2 KB
/
Settings.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* Ternific Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require /*, exports, module*/) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash");
var Dialogs = brackets.getModule("widgets/Dialogs");
var ProjectManager = brackets.getModule("project/ProjectManager");
var FileSystem = brackets.getModule("filesystem/FileSystem");
var FileUtils = brackets.getModule("file/FileUtils");
var EventDispatcher = brackets.getModule("utils/EventDispatcher");
var Promise = require("node_modules/spromise/dist/spromise.min");
var TERN_ROOT = "node_modules/tern/";
var TERNIFIC_ROOT = "./";
function Settings(filePath, watchProject) {
this.project = ProjectManager.getProjectRoot();
this.data = null;
this.file = null;
this.filePath = filePath;
this.baseUrl = null;
watchChanges(this, watchProject);
}
EventDispatcher.makeEventDispatcher(Settings.prototype);
Settings.create = function(filePath, watchProject) {
return new Settings(filePath, watchProject);
};
/**
* Gets the full path to the current project
*
* @returns {string}
*/
Settings.prototype.projectPath = function() {
return this.project && this.project.fullPath || "";
};
/**
* Load settings file.
*
* @param {string} baseUrl - Path to start searching for settings file to
* be loaded. If one isn't provided, the current project path is used.
*
* @returns {Promise}
*/
Settings.prototype.load = function(baseUrl) {
return this._load(baseUrl, false);
};
/**
* Reload settings file.
*
* @returns {Promise}
*/
Settings.prototype.reload = function() {
return this._load(this.baseUrl, true);
};
/**
* Traverses directory structure merging settings it finds along the way.
*
* NOT IMPLEMENTED
*/
Settings.prototype.aggregate = function(/*baseUrl*/) {
throw new Error("Not implemented");
};
/**
* Load settings file.
*
* @private
*
* @param {string} baseUrl - Path to start searching for settings file to
* be loaded. If one isn't provided, the current project path is used.
* @param {boolean} reload - Forces the reload of the current file.
*
* @returns {Promise}
*/
Settings.prototype._load = function(baseUrl, reload) {
var _self = this;
var prevBaseUrl = this.baseUrl;
_self.baseUrl = joinPath(this.projectPath(), baseUrl);
if (!reload && _self.baseUrl === prevBaseUrl) {
return Promise.resolve({
changed: false
});
}
return load(this, reload).always(function(result) {
if (result.changed) {
_self.file = result.file;
_self.data = result.data;
_self.trigger("change", _self.data);
}
});
};
/**
* Tests whether or not the baseUrl is a subdirectory of the project. If it is,
* then we can traverse up the directory hierarchy.
*
* @returns {boolean}
*/
function canTraverse(baseUrl, projectPath) {
return projectPath !== baseUrl && baseUrl.indexOf(projectPath) !== -1;
}
/**
* Load settings starting from `settings.baseUrl`.
*
* @returns {{file: file, data: object, changed: boolean}}
*/
function load(settings, reload) {
function fileFound(file) {
var currentFile = settings.file;
if (!reload && (currentFile && file.fullPath === currentFile.fullPath)) {
return {
changed: false
};
}
else {
return readFile(file)
.then(configurePaths(settings), _.noop)
.then(parseSettings, _.noop)
.then(function(data) {
return {
file: file,
data: data,
changed: true
};
});
}
}
function fileNotFound() {
return {
file: null,
data: null,
changed: settings.data !== null || settings.file !== null
};
}
return findFile(settings, canTraverse(settings.baseUrl, settings.projectPath())).then(fileFound, fileNotFound);
}
function configurePaths(settings) {
return function(settingsString) {
if (/{\w+}/.test(settingsString)) {
settingsString = settingsString.replace(/{\w+}/g, function(w) {
if (w === "{project}") {
return settings.baseUrl;
}
else if (w === "{tern}") {
return TERN_ROOT;
}
else if (w === "{ternific}") {
return TERNIFIC_ROOT;
}
return w;
});
}
return normalizePath(settingsString);
}
}
/**
* Cleans out settings data. It removes all comments from the file before
* it is converted to a JSON structure.
*
* @param {string} settingsString - String settings. Generally read from
* directly from a file a text, and needs to be converted to JSON.
*
* @returns {object}
*/
function parseSettings(settingsString) {
if (!settingsString) {
throw new Error("Must provide settings");
}
try {
return JSON.parse(stripComments(settingsString));
}
catch(ex) {
Dialogs.showModalDialog(
"TernificErr",
"Ternific Error",
"Error processing ternific settings<br>" +
ex.toString()
);
}
}
function watchChanges(settings, watchProject) {
function fileChanged(evt, file) {
if (file && settings.file && file.fullPath === settings.file.fullPath) {
settings.reload();
}
}
function projectChanged(evt, project) {
settings.project = project;
settings.reload();
}
function dispose() {
FileSystem.off("change", fileChanged);
if (watchProject !== false) {
ProjectManager.off("change", projectChanged);
}
}
FileSystem.on("change", fileChanged);
if (watchProject !== false) {
ProjectManager.on("projectOpen", projectChanged);
}
return {
dispose: dispose
};
}
/**
* Finds the settings file starting from `baseUrl`.
*/
function findFile(settings, traverse) {
var filePath = settings.filePath;
var projectPath = settings.projectPath();
return new Promise(function(resolve, reject) {
(function find(baseUrl) {
if (!baseUrl || projectPath && baseUrl.indexOf(projectPath) === -1) {
reject(false);
return;
}
try {
var file = FileSystem.getFileForPath(baseUrl + filePath);
file.exists(function(err, exists) {
if (exists) {
resolve(file);
}
else if (err || !traverse) {
reject(false);
}
else {
find(FileUtils.getParentPath(baseUrl));
}
});
}
catch(ex) {
reject(false);
}
})(settings.baseUrl);
});
}
/**
* Reads file content and returns a promise that is resolve when the file is
* read from storage.
*
* @param {File} file - File object
*
* @returns {Promise}
*/
function readFile(file) {
return new Promise(function(resolve, reject) {
file.read(function(err, content /*, stat*/) {
if (err) {
reject(err);
}
else {
resolve(content);
}
});
});
}
function joinPath(projectPath, baseUrl) {
// TODO: Change to a path.join to consistently handle when settings are
// read from the project or from a subdirectory - always taking into
// account the project path.
return baseUrl ? normalizePath(baseUrl) : projectPath;
}
/**
* Make sure we only have forward slashes and we dont have any duplicate slashes
*/
function normalizePath(path) {
return path.replace(/[\/\\]+/g, "/");
}
/**
* Strips all commments from a json string.
*/
function stripComments(text) {
var string = text || "";
string = string.replace(/\/\*(?:[^\*\/])*\*\//g, "");
string = string.replace(/\/\/.*/g, "");
return string;
}
return Settings;
});