-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
148 lines (127 loc) · 5.13 KB
/
main.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
/* global define, brackets, $ */
define(function (require, exports, module) {
"use strict";
var namespace = 'brackets-htmllint';
var _configFileName = ".htmllintrc";
var CodeInspection = brackets.getModule("language/CodeInspection"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
NodeDomain = brackets.getModule("utils/NodeDomain"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
FileSystemError = brackets.getModule("filesystem/FileSystemError"),
FileUtils = brackets.getModule("file/FileUtils"),
ProjectManager = brackets.getModule("project/ProjectManager");
var htmllintDomain = new NodeDomain("htmllint", ExtensionUtils.getModulePath(module, "node/htmllintDomain"));
function handleLinter(html, fullPath) {
var defer = new $.Deferred();
loadConfigs(fullPath).done(function (options) {
htmllintDomain.exec("lint", html, options)
.done(function (result) {
defer.resolve(result);
}).fail(function (err) {
console.error("Failed: ", err);
defer.reject(err);
});
});
return defer.promise();
}
/*
* Takes the full path to the file, provided via Brackets.
* Does a lookup starting from the project root down to the directory of the current file.
* When it finds a valid .htmllintrc file, it will parse the JSON and add it to the options,
* overwriting previous options.
* Returns the final options set.
*/
function loadConfigs(fullPath) {
var projectRootEntry = ProjectManager.getProjectRoot(),
result = new $.Deferred(),
relPath,
rootPath;
// if nothing is open, return {} as the option.
if (!projectRootEntry) {
return result.resolve({}).promise();
}
// get the full absolute path to this directory.
rootPath = projectRootEntry.fullPath;
// get the path relative to the root of the project.
relPath = FileUtils.getRelativeFilename(rootPath, fullPath);
// for files outside the root, use default config
if (!relPath) {
return result.resolve({}).promise();
}
// get the parent directory of our relative path (to remove the *.html part)
relPath = FileUtils.getDirectoryPath(relPath);
lookUpFiles(rootPath, relPath).done(function (configs) {
result.resolve(configs);
});
return result.promise();
}
function lookUpFiles(rootPath, relPath) {
var result = new $.Deferred(),
finalConfigs = {};
// any changes made here should also be changed below
// (too lazy to combine in yet another function)
getConfig(rootPath + relPath).done(function (configs) {
var keys = Object.keys(configs);
keys.forEach(function (key) {
if (configs.hasOwnProperty(key) && !finalConfigs[key]) {
finalConfigs[key] = configs[key];
}
});
if (!(relPath.length > 0)) {
result.resolve(finalConfigs);
}
});
while (relPath.length > 0) {
// take off the '/' end of the path to get the new parent directory
relPath = FileUtils.getDirectoryPath(relPath.substr(0, relPath.length - 1));
getConfig(rootPath + relPath).done(function (configs) { // eslint-disable-line no-loop-func
var keys = Object.keys(configs);
keys.forEach(function (key) {
if (configs.hasOwnProperty(key) && !finalConfigs[key]) {
finalConfigs[key] = configs[key];
}
});
if (!(relPath.length > 0)) {
result.resolve(finalConfigs);
}
});
}
return result.promise();
}
/*
* Takes a directory.
* Uses the directory to look for the ".htmllintrc" file
* and JSON parses it.
* Returns the object.
*/
function getConfig(directory) {
var result = new $.Deferred(),
filePath = directory + _configFileName,
file;
// get the file object for the filename.
file = FileSystem.getFileForPath(filePath);
file.read(function (err, content) {
if (err) {
if (err !== FileSystemError.NOT_FOUND) {
console.log("brackets-htmllint: error finding file " + filePath + ". Details: " + err);
}
result.resolve({});
return;
}
var config = {};
try {
config = JSON.parse(content);
} catch (e) {
console.log("brackets-htmllint: error parsing " + file.fullPath + ". Details: " + e);
result.resolve({});
return;
}
result.resolve(config);
});
return result.promise();
}
CodeInspection.register("html", {
name: namespace,
scanFileAsync: handleLinter
});
});