forked from MiguelCastillo/Brackets-Ternific
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.js
182 lines (139 loc) · 4.95 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
/**
* Interactive Linter Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require, exports, module) {
"use strict";
var Dialogs = brackets.getModule("widgets/Dialogs"),
ProjectManager = brackets.getModule("project/ProjectManager"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
spromise = require("libs/js/spromise"),
currentProject = {};
function Settings( ) {
var instance = {},
currentSettings = {};
// Monitor file changes
FileSystem.on("change", function(evt, file) {
if ( currentSettings.file && currentSettings.fileObject && file && file.fullPath === currentSettings.fileObject.fullPath ) {
loadFile().always(function() {
$(instance).trigger("change", [currentSettings.settings]);
});
}
});
function setSettings( settings ) {
var deferred = spromise.defer();
settings = stripComments(settings);
try {
settings = JSON.parse(settings);
deferred.resolve(settings);
}
catch( ex ) {
if ( !settings ) {
deferred.resolve();
return;
}
Dialogs.showModalDialog(
"TernificErr",
"Ternific Error",
"Error processing ternific settings<br>" +
ex.toString());
deferred.reject("Error processing ternific settings");
}
return deferred.promise;
}
function loadFile( ) {
var traverse = currentSettings.path.indexOf(currentProject.fullPath) !== -1;
return findFile(currentSettings.file, currentSettings.path, traverse)
.always(function(file) {
currentSettings.fileObject = file;
})
.then(readFile, $.noop)
.then(setSettings, $.noop)
.then(function(settings) {
return (currentSettings.settings = settings);
});
}
function loadSettings(file, path) {
if ( !file ) {
return spromise.resolved();
}
// Cache so that we are not loading up the same file when navigating in the same directory...
if ( path === currentSettings.path && currentSettings.path) {
return spromise.resolved(currentSettings.settings);
}
currentSettings.path = normalizePath(path || currentProject.fullPath);
currentSettings.file = file;
return loadFile().promise;
}
instance.load = loadSettings;
return instance;
}
function getParentPath( path ) {
var result = stripTrailingSlashes( path );
return result.substr(0, result.lastIndexOf("/") + 1);
}
function findFile( fileName, filePath, traverse ) {
var deferred = spromise.defer();
function find( filePath ) {
if ( !filePath ) {
return deferred.reject(false);
}
try {
var file = FileSystem.getFileForPath (filePath + "/" + fileName);
file.exists(function( err, exists ) {
if ( exists ) {
deferred.resolve(file);
}
else if ( err || !traverse || filePath.indexOf( currentProject.fullPath ) === -1 ) {
deferred.reject(false);
}
else {
find( getParentPath(filePath) );
}
});
}
catch(ex) {
deferred.reject(false);
}
}
find( filePath );
return deferred.promise;
}
function readFile( file ) {
var deferred = spromise.defer();
file.read(function( err, content /*, stat*/ ) {
if ( err ) {
deferred.reject(err);
return;
}
deferred.resolve(content);
});
return deferred.promise;
}
/**
* Make sure we only have forward slashes and we dont have any duplicate slashes
*/
function normalizePath( path ) {
return path.replace(/\/+|\\+/g, "/");
}
/**
* Lets get rid of the trailing slash
*/
function stripTrailingSlashes(path) {
return path.replace(/\/$/, "");
}
/**
* Strips all commments from a json string.
*/
function stripComments( text ) {
var string = text || '';
string = string.replace(/\/\*(?:[^\*\/])*\*\//g, '');
string = string.replace(/\/\/.*/g, '');
return string;
}
$(ProjectManager).on("projectOpen", function(e, project) {
currentProject = project;
});
return Settings;
});