forked from jdiehl/brackets-less-autocompile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
132 lines (111 loc) · 4.24 KB
/
main.js
File metadata and controls
132 lines (111 loc) · 4.24 KB
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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4,
maxerr: 50, browser: true */
/*global $, define, brackets */
define(function (require, exports, module) {
"use strict";
var AppInit = brackets.getModule("utils/AppInit"),
ProjectManager = brackets.getModule("project/ProjectManager"),
ExtensionUtils = brackets.getModule("utils/ExtensionUtils"),
NodeConnection = brackets.getModule("utils/NodeConnection"),
DocumentManager = brackets.getModule("document/DocumentManager"),
PanelManager = brackets.getModule("view/PanelManager"),
FileSystem = brackets.getModule("filesystem/FileSystem"),
FileUtils = brackets.getModule("file/FileUtils"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager");
var panel, $panel;
// connect to the node server
function connect(callback) {
var connection = new NodeConnection();
var promise = connection.connect(true);
promise.fail(function (err) {
callback("Could not connect to node server: " + err);
});
promise.done(function () {
callback(null, connection);
});
}
function loadNodeModule(moduleName, callback) {
connect(function (err, connection) {
if (err) {
callback(err);
return;
}
var path = ExtensionUtils.getModulePath(module, "node/" + moduleName);
var promise = connection.loadDomains([path], true);
promise.fail(function (err) {
callback("Could not load node module " + moduleName + ": " + err);
});
promise.done(function () {
callback(null, connection.domains[moduleName]);
});
});
}
function onCompileSuccess(result) {
if (panel) {
panel.hide();
}
// todo: update live preview
}
function onCompileError(err) {
if (!panel) {
$panel = $("<div id='less-parser-error' class='bottom-panel'>");
panel = PanelManager.createBottomPanel("jdiehl.less-autocompile", $panel);
}
$panel.html("<p>" + err.message + " at line " + err.line + "</p>");
panel.show();
}
// a document was saved
function onDocumentSaved(event, document) {
// check if the document was truly saved
if (document.file.isDirty) {
return;
}
// check if it was a .less document
var path = document.file.fullPath;
if (path.substr(path.length - 5, 5) === ".less") {
// connect to the node server
loadNodeModule("LessCompiler", function (err, compiler) {
if (err) {
console.error(err);
return;
}
// Compile the files listed in the compile config file.
// Defaults to "compile.json" in the root of project.
// If no compilation config found, defaults to normal behavior.
getCompileConfigPath(function(configPath) {
var configFile = FileSystem.getFileForPath(configPath),
projectPath = ProjectManager.getProjectRoot().fullPath,
promise, paths;
FileUtils.readAsText(configFile).done(function(text) {
paths = JSON.parse(text).less;
paths.forEach(function(path) {
promise = compiler.compile(projectPath + path);
promise.done(onCompileSuccess).fail(onCompileError);
});
}).fail(function() {
compiler.compile(path).done(onCompileSuccess).fail(onCompileError);
});
});
});
}
}
function getCompileConfigPath(callback) {
var defaultPath = 'compile.json',
projectPath = ProjectManager.getProjectRoot().fullPath,
prefs;
getExtensionName(function(name) {
prefs = PreferencesManager.getExtensionPrefs(name);
callback(projectPath + (prefs.get('compilePath') || defaultPath));
});
}
function getExtensionName(callback) {
var packagePath = ExtensionUtils.getModulePath(module, 'package.json'),
packageFile = FileSystem.getFileForPath(packagePath);
FileUtils.readAsText(packageFile).done(function(text) {
callback(JSON.parse(text).name);
});
}
AppInit.appReady(function () {
$(DocumentManager).on("documentSaved", onDocumentSaved);
});
});