This repository has been archived by the owner on May 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
domain.js
191 lines (172 loc) · 4.93 KB
/
domain.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
'use strict';
(function () {
var fs = require('fs'),
path = require('path'),
findup = require('findup'),
domainName = 'globexdesigns.brackets-jscs',
configFiles = ['.jscsrc', '.jscs.json'],
Checker = require('jscs'),
jscsConfig = require('jscs/lib/cli-config');
/**
* Does a recursive scan of the directories at and above the given
* fullPath until it finds a valid JSCS config file.
*
* @param {string} fullPath - The full path to the directory where to start the scan
* @param {function} callback - Callback (returns the full path to the directory where
* the configuration file was found, or null)
*
*/
var _findConfig = function (fullPath, callback) {
findup(fullPath, function (dir, cb) {
var found = false,
configpath;
for (var i = 0, l = configFiles.length; i < l; i++) {
configpath = path.join(dir, configFiles[i]);
if (fs.existsSync(configpath)) {
found = true;
break;
}
}
cb(found);
}, function (err, dir) {
if (err) return callback(null);
callback(dir);
});
};
/**
* Given a directory, returns the value of the JSCS configuration file found in that
* directory. Will first look for a .jscsrc file, if not found, will try .jscs.json
*
* @param {string} fullPath - The full path to the directory where the config
* file should be
*
* @returns {object} A valid JSCS configuration object
*/
var _getConfigFile = function (fullPath) {
var config = null,
configpath;
for (var i = 0, l = configFiles.length; i < l; i++) {
configpath = path.join(fullPath, configFiles[i]);
if (fs.existsSync(configpath)) {
config = jscsConfig.load(configpath);
break;
}
}
return config || {};
};
/**
* Given a JSCS object and a path to the directory where the configuration file is,
* sets up the JSCS instance with the given configuration
*
* @param {object} JSCS - Instance of the JSCS checker
* @param {string} configPath - The full path to the directory where the config
* file should be
*/
var _setConfig = function (JSCS, configPath) {
var config = _getConfigFile(configPath);
// Load JSCS configuration
JSCS.registerDefaultRules();
JSCS.configure(config);
};
/**
* Given the path to the current file, executes the callback onces a JSCS instance has been
* loaded and setup using the correct configuration file. The callback will return an
* instance of the JSCS checker.
*
* @param {string} fullPath - Path to the current file
* @param {function} callback - Callback method which returns a JSCS instance
*
* @returns {void}
*/
var _prepareJSCS = function (fullPath, callback) {
// Initialize JSCS
var JSCS = new Checker();
_findConfig(path.dirname(fullPath), function (result) {
// If no config file found - let users know
if (!result) {
return callback(null, [{
message: "Unable to find a JSCS configuration file."
}]);
}
// Set config filea
_setConfig(JSCS, result);
callback(JSCS);
});
};
/**
* This method will be exposed to the extension. Given a path to a file, and a callback,
* will run the file at the path through the JSCS
* checker and return back the errors via the callback.
*
* @param {string} fullPath - Path to the file to scan
* @param {function} callback - Callback method
*
* @returns {void}
*/
var lintFile = function (fullPath, callback) {
return _prepareJSCS(fullPath, function (JSCS, err) {
if (err) return callback(err);
JSCS.checkPath(fullPath).then(function (response) {
callback(response[0]._errorList);
}).catch(function (err) {
callback(err);
});
});
};
/**
* This method will be exposed to the extension. Given a path to a file, and a callback,
* will run the file at the path through the JSCS
* auto-fixer.
*
* @param {string} code - The code source to fix
* @param {string} fullPath - Path to the file to fix
* @param {function} callback - Callback method
*
* @returns {void}
*/
var fixFile = function (code, fullPath, callback) {
return _prepareJSCS(fullPath, function (JSCS, err) {
if (err) throw new Error(err);
var result = JSCS.fixString(code, fullPath);
callback(null, result.output);
});
};
exports.init = function (domainManager) {
if (!domainManager.hasDomain(domainName)) {
domainManager.registerDomain(domainName, {
major: 0,
minor: 1
});
}
// Registered Command: lintFile
domainManager.registerCommand(
domainName,
'lintFile',
lintFile,
true,
'Returns the results of linting the given file with JSCS',
[{
name: 'fullPath',
type: 'string'
}], [{
name: "errors",
type: "array"
}]
);
// Registered Command: fixFile
domainManager.registerCommand(
domainName,
'fixFile',
fixFile,
true,
'Fixes the current file using the JSCS auto-fixing feature',
[{
name: 'code',
type: 'string'
}, {
name: 'fullPath',
type: 'string'
}]
);
};
}());