-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.js
304 lines (263 loc) · 10.5 KB
/
extension.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"use strict";
const vscode = require('vscode');
const thiccify = require('./thiccify.js');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const removeEmptyLines = require("remove-blank-lines");
const ob = require('./HTML.json');
//register on activation
function activate(context) {
let sendFileOut = function (fileName, data, stats) {
fs.writeFile(fileName, data, "utf8", () => {
let status = "Thiccified: " + stats.files + " files";
if (stats.length) status = "Thiccified: " + (((data.length / stats.length) * 10000) | 0) / 100 +
"% of original" + (stats.errors ? " but with errors." : (stats.warnings ? " but with warnings." : "."));
vscode.window.setStatusBarMessage(status, 10000);
});
};
let doThiccify = function (document) {
let outName = document.fileName.split('.');
const ext = outName.pop();
outName.push("max");
outName.push(ext);
outName = outName.join('.');
let data = document.getText();
//if the document is empty here, we output an empty file to the max point
if (!data.length) return sendFileOut(outName, "", {
length: 1
});
//what are we Thiccifying?
const isJS = ext.toLocaleLowerCase() === 'js';
const isCSS = ext.toLocaleLowerCase() === 'css';
const isHTML = ext.toLocaleLowerCase() === 'html' || ext.toLocaleLowerCase() === 'htm';
if (isJS) {
// let opts = settings.js;
// opts.fromString = true;
try {
console.log(chalk.green("Input", data));
let results = thiccify.run(data);
let unrolled = thiccify.unRoll(results.code);
//THIS IS JUST TO MAKE THE CODE INTENDTAION INVERTED
let canvas = "";
let sizes = [];
let largest = 0;
let ls = unrolled.code.split("\n");
console.log(ls);
for (var i = 0; i < ls.length; i++) {
let re = /^[\s]*/gm;
let spaces = re.exec(ls[i]);
let size = Math.floor(spaces[0].length / 4);
ls[i] = ls[i].replace(spaces, '');
sizes.push(size);
}
console.log("1");
let max = Math.max.apply(null, sizes);
let newSize = []
for (var i = 0; i < ls.length; i++) {
newSize.push(max - sizes[i]);
}
console.log("1");
for (var i = 0; i < ls.length; i++) {
var sp = "";
for (var b = 0; b < newSize[i]; b++) {
sp = sp + " ";
}
ls[i] = sp + ls[i];
}
console.log("1");
let shished = "";
for (var i = 0; i < ls.length; i++) {
shished = shished + ls[i] + "\n";
}
2
unrolled.code = shished;
//FINISHED INVERTING INDENTATION.
console.log(chalk.red("Output", unrolled.code));
// console.log(chalk.red("Mapping", results.map));
sendFileOut(outName, unrolled.code, {
length: data.length
});
} catch (e) {
vscode.window.setStatusBarMessage("Thiccify failed: " + e.message, 5000);
}
} else if (isCSS) {
vscode.window.setStatusBarMessage("Thiccify failed: That's css", 5000);
try {
//Does all the css crap that nobody should every have to read.
data = removeEmptyLines(data);
data = data.replace(new RegExp('{', 'g'), '{\n');
data = data.replace(new RegExp('}', 'g'), '\n}\n');
var obj = require('./cssNames.json');
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
var comment = "\n\t/*" + obj[property] + "*/\n\t" + property + ":";
data = data.replace(new RegExp(" " + property + ":", 'g'), comment);
}
}
let results = data;
sendFileOut(outName, results, {
length: data.length
});
console.log(results);
/*while(b){
let re = new RegExp('\{([^}]+)\}');
let gayre = new RegExp('^[.#a-z0-9A-Z ]*');
let results = gayre.exec(data)[0] + re.exec(data)[0];
console.log(result);
data = data.replace(results, '');
attr.push(results);
sendFileOut(outName, results, {
length: data.length
});
if(data.length == 0){
b = false;
}
console.log("memes " + data );
}
*/
} catch (e) {
vscode.window.setStatusBarMessage("Thiccify failed: " + e.message, 5000);
}
// let base = settings.css.root.slice();
// settings.css.root = settings.css.root.replace("${workspaceRoot}", vscode.workspace.rootPath || "");
// // let cleanCSS = new mincss(settings.css);
// let cleanCSS = settings.css;
// cleanCSS.minify(data, (error, results) => {
// settings.css.root = base;
// if (results && results.styles) sendFileOut(outName, results.styles, {
// length: data.length,
// warnings: results.warnings.length,
// errors: results.errors.length
// });
// else if (error) vscode.window.setStatusBarMessage("Minify failed: " + error.length + " error(s).", 5000);
// let cleanCSS = new mincss(settings.css);
cleanCSS.minify(data, (error, results) => {
if (results && results.styles) sendFileOut(outName, results.styles, {
length: data.length,
warnings: results.warnings.length,
errors: results.errors.length
});
else if (error) vscode.window.setStatusBarMessage("Minify failed: " + error.length + " error(s).", 5000);
});
} else if (isHTML) {
vscode.window.setStatusBarMessage("Thiccify failed: That's html", 5000);
//let results = data;
data = data.replace(/<!--[\S ]*-->/g, '');
console.log(ob);
for (var property in ob) {
if (ob.hasOwnProperty(property)) {
console.log(prop);
var prop = property.slice(0, -1);
var comment = "\n<!-- " + ob[property] + "-->\n" + prop;
//data = data.replace(new RegExp(prop+'[> ]', 'g'), comment);
data = data.replace(new RegExp(prop + '>', 'g'), comment + '>');
data = data.replace(new RegExp(prop + ' ', 'g'), comment + ' ');
}
}
let results = data;
sendFileOut(outName, results, {
length: data.length
});
//Does all the HTML crap
}
//otherwise, we don't care ...
};
let doThiccifyDir = function (folder, ext) {
fs.readdir(folder, (err, files) => {
//keep just our extension, drop all pre min'ed
files = files.sort()
.filter(f => path.extname(f)
.slice(1) === ext)
.filter(f => !f.endsWith(".max." + ext))
.map(f => path.join(folder, f));
if (files.length === 0) return vscode.window.setStatusBarMessage("No files for directory thiccify (", ext, ")",
5000);
let outName = folder + ".max." + ext;
if (ext === 'js') {
// let opts = settings.js;
// opts.fromString = false;
try {
// let results = files
let results = thiccify.run(data);
let unrolled = thiccify.unRoll(results.code);
// let results = minjs.minify(files, opts);
sendFileOut(outName, results.code, {
files: files.length
});
} catch (e) {
vscode.window.setStatusBarMessage("Thiccify failed: " + e.message, 5000);
}
} else {
vscode.window.setStatusBarMessage("Thiccify failed: css", 5000);
// let base = settings.css.root.slice();
// settings.css.root = settings.css.root.replace("${workspaceRoot}", vscode.workspace.rootPath || "");
// //strip the root dir from the whole file set
// files = files.map(f => f.replace(settings.css.root, ""));
// // let cleanCSS = new mincss(settings.css);
// let cleanCSS = settings.css;
// cleanCSS.minify(files, (error, results) => {
// settings.css.root = base;
// if (results && results.styles && results.styles.length) sendFileOut(outName, results.styles, {
// files: files.length,
// warnings: results.warnings.length,
// errors: results.errors.length
// });
// else if (error) vscode.window.setStatusBarMessage("Minify failed: " + error.length + " error(s).",
// 5000);
// });
}
});
};
let disposable = vscode.commands.registerCommand('extension.thiccify', function () {
const active = vscode.window.activeTextEditor;
if (!active || !active.document) return;
if (active.document.isUntitled) return vscode.window.setStatusBarMessage(
"File must be saved before Thiccify can run",
5000);
return doThiccify(active.document);
});
context.subscriptions.push(disposable);
disposable = vscode.commands.registerCommand('extension.thiccifyDir', function () {
const active = vscode.window.activeTextEditor;
if (!active || !active.document) return;
if (active.document.isUntitled) return vscode.window.setStatusBarMessage(
"File must be saved before thiccify can run",
5000);
let ext = active.document.fileName.split('.')
.pop()
.toLowerCase();
if (ext === 'js' || ext === 'css') doThiccifyDir(path.dirname(active.document.fileName), ext);
else vscode.window.setStatusBarMessage("Active file must be .js to thiccify parent directory.",
5000);
});
context.subscriptions.push(disposable);
// disposable = vscode.workspace.onDidChangeConfiguration(() => {
// settings = cleanSettings(vscode.workspace.getConfiguration()
// .get("minify"));
// });
// context.subscriptions.push(disposable);
disposable = vscode.workspace.onDidSaveTextDocument(function (doc) {
//check if the user wants to do a minify here
// if (!vscode.workspace.getConfiguration('minify')
// .minifyExistingOnSave) return;
//check if there is a *.min.* file
let n = doc.fileName.split('.');
const ext = n.pop();
n.push("max");
n.push(ext);
n = n.join(".");
//see if there is a file, if there is, run min
fs.exists(n, exists => exists ? doThiccify(doc) : false);
const isJS = (ext.toLowerCase() === "js");
const isCSS = (ext.toLowerCase() === "css");
if (isJS || isCSS) {
//check if the directory has a max
n = path.dirname(doc.fileName);
n += ".max." + (isJS ? "js" : "css");
fs.exists(n, exists => exists ? doThiccifyDir(path.dirname(doc.fileName), isJS ? "js" : "css") : false);
}
});
context.subscriptions.push(disposable);
}
exports.activate = activate;