This repository has been archived by the owner on May 19, 2020. It is now read-only.
/
lessc.wsf
246 lines (223 loc) · 8.73 KB
/
lessc.wsf
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
<!--
Less.js compiler for Windows Script Host
http://blog.dotsmart.net/
Modified by Van Kichline (vank), Microsoft. 10/2012:
Emit errors in imported files.
Copyright (c) 2013, Duncan Smart
Licensed under the Apache 2.0 License.
-->
<job>
<script language="jscript">
// Stub out globals
var window = this;
var location = window.location = {
port: 0,
href: '',
protocol: 'http:', // vank: added to support development env setting
hostname: 'localhost' // vank: added to support development env setting
};
var fso = new ActiveXObject("Scripting.FileSystemObject");
var input = null;
var util = {
readText: function (filename) {
var stream = WScript.CreateObject("ADODB.Stream"); // this should solve issue with OpenTextFile
stream.Charset = 'utf-8';
stream.Open();
stream.LoadFromFile(filename);
var text = stream.ReadText();
stream.close();
return text;
}
};
// XMLHttpRequest that just gets local files. Used when processing "@import"
function XMLHttpRequest(){}
XMLHttpRequest.prototype = {
open: function (method, url, async) {
this.url = url;
},
send: function () {
// get the file path relative to the input less file/directory
var currDir = fso.folderExists(input) ? input : fso.getParentFolderName(input);
var filename = this.url.replace(/\//g, '\\');
if (!/^([a-zA-Z]+:|[\/\\])/.test(filename)) // SLaks: If the filename is not already an absolute path, normalize it.
filename = fso.BuildPath(currDir, filename);
//WScript.StdErr.WriteLine("XHR.send " + filename);
// Little hack so *.less will resolve to *.less.css also. Helps with Visual Studio
// ensuring that file BuildAction is set to Content and you get rudimentary syntax highlighting with no set up.
if (filename.match(/.less$/i) && !fso.FileExists(filename)) {
filename = filename.replace(/.less$/i, '.less.css');
}
try {
this.status = 200;
this.responseText = util.readText(filename);
}
catch (e) {
this.status = 404;
this.responseText = e.description;
}
},
setRequestHeader: function () {},
getResponseHeader: function () {}
};
// Fake document
var document = {
_dummyElement: {
childNodes: [],
appendChild: function(){},
style: {}
},
getElementsByTagName: function(){ return []; },
getElementById: function(){ return this._dummyElement; },
createElement: function(){ return this._dummyElement; },
createTextNode: function () { return this._dummyElement; },
// vank: added 'body' to capture errors in @imported files
body: {
replaceChild: function (elem) {
if (!elem.innerHTML.match(/null or not an object/i)) {
document._dummyElement.error = elem.innerHTML;
}
}
}
};
// vank: added 'setInterval' and 'clearInterval' to support development env setting
function setInterval(f, int) {
f();
return 'x';
}
function clearInterval(t) { }
</script>
<!-- less.js from https://github.com/less/less.js/tree/master/dist/ -->
<script language="jscript" src="es5-shim.min.js" />
<script language="jscript" src="less-1.4.2.min.js" />
<script language="jscript">
// Parse args
var args = {};
for (var i = 0; i < WScript.Arguments.Length; i++) {
var arg = WScript.Arguments.Item(i);
// Handle "-switch" and "--switch"
var match = arg.match(/^--?([a-z][0-9a-z-]*)$/i);
if (match) {
i = match[1];
arg = true;
}
args[i] = arg;
}
input = args[0];
var output = args[1];
if (fso.folderExists(input)) {
input = fso.getAbsolutePathName(input);
var files = getFiles(input, /\.less$/i);
for (var i = 0; i < files.length; i++) {
var file = files[i];
convert(file.path, output + '\\' + file.name.replace( /\.less$/i, '.css'));
}
}
else {
if (fso.folderexists(output)) {
output = fso.getAbsolutePathName(output) + '\\' + fso.getfile(input).name.replace(/\.less$/i, '.css');
}
convert(input, output);
}
// Returns array of {name:'foo.bar', path:'c:\baz\foo.bar'} for given directory and pattern
function getFiles(dir, regex) {
var e = new Enumerator(fso.getFolder(dir).files);
var files = []
for (; !e.atEnd(); e.moveNext()) {
if (regex.test(e.item().path)) {
files.push({
name: e.item().name,
path: e.item().path
});
}
}
return files;
}
function convert(input, output) {
if (!input) {
WScript.StdErr.WriteLine("lessc.wsf: no input files");
WScript.StdErr.WriteLine("Usage:");
WScript.StdErr.WriteLine(" Single file: cscript //nologo lessc.wsf input.less [output.css] [-compress]");
WScript.StdErr.WriteLine(" Directory: cscript //nologo lessc.wsf inputdir outputdir [-compress]");
WScript.Quit(1);
}
var data;
if (input == '-') {
var chunks = [];
while (!WScript.StdIn.AtEndOfStream)
chunks.push(WScript.StdIn.ReadAll());
data = chunks.join('');
}
else {
data = util.readText(input);
}
// SLaks: Make sure LESS can resolve relative import URLs.
// LESS.js tries to normalize \ to /, but they forgot that
// replace() with a non-regex only replaces the 1st match.
// It also tries to detect drive letters but only supports
// lowercase.
var safeFilename = input.replace(/\\/g, '/').replace(/^[A-Z]+:/, function (m) { return m.toLowerCase(); });
var parser = new less.Parser({
filename: safeFilename
});
try {
parser.parse(data, function (err, tree) {
if (err) {
WScript.StdErr.WriteLine("ERR: ");
for (var i in err) {
if (err[i]) {
WScript.StdErr.WriteLine(" " + i + ': ' + err[i]);
}
}
WScript.Quit(2);
}
else {
var css = tree.toCSS({
compress: args.compress
});
if (output) {
if (fso.FileExists(output)) {
var checkfile = fso.GetFile(output);
if (checkfile.Attributes & 1) {
checkfile.Attributes = checkfile.Attributes ^ 1
}
}
var outputfile = fso.CreateTextFile(output, true, true); // filename, overwrite, writeUTF16
outputfile.Write(css);
outputfile.Close();
}
else {
WScript.StdOut.Write(css);
}
}
});
}
catch (e) {
WScript.StdErr.WriteLine("ERROR:");
for (var i in e) {
if (e[i]) {
WScript.StdErr.WriteLine(" " + i + ': ' + e[i]);
}
}
WScript.Quit(3);
}
// Sometimes less will return errors inside the fake HTML element
if (document._dummyElement.innerHTML && document._dummyElement.innerHTML.match(/Syntax Error/i)) {
var s = document._dummyElement.innerHTML;
s = s.replace(/<[^>]+(\/?)>/g, function (m) { return m.indexOf('/') > 0 && m !== '</label>' ? "\n" : '' });
s = s.replace(/\n+/g, '\n');
WScript.StdErr.WriteLine("ERR: ");
WScript.StdErr.WriteLine(s);
WScript.Quit(2);
}
// vank: added to display output from errors in @imported files.
if (document._dummyElement.error && document._dummyElement.error.match(/Syntax Error/i)) {
var s = document._dummyElement.error;
s = s.replace(/<[^>]+(\/?)>/g, function (m) { return m.indexOf('/') > 0 && m !== '</label>' ? "\n" : '' });
s = s.replace(/\n+/g, '\n');
WScript.StdErr.WriteLine("ERR: ");
WScript.StdErr.WriteLine(s);
WScript.Quit(2);
}
}
</script>
</job>