Skip to content

Commit c88760b

Browse files
committed
Put CSSDiff back into gulpfile
1 parent 34aa22c commit c88760b

File tree

2 files changed

+84
-86
lines changed

2 files changed

+84
-86
lines changed

CSSDiff.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

gulpfile.babel.js

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const Readable = require("stream").Readable;
2121
const createHash = require("crypto").createHash;
2222
const fs = require("fs");
2323
const basename = require("path").basename;
24-
const CSSDiff = require("./CSSDiff").default;
2524

2625
// constants
2726
const isProduction = () => process.env.NODE_ENV === "production";
@@ -256,3 +255,87 @@ function diffTheme(theme) {
256255
function getThemeFromPath(filename) {
257256
return filename.match(/_(\w+)\.scss/)[1];
258257
}
258+
259+
class CSSDiff {
260+
diff(base, targ, pretty = false) {
261+
let diff = this.compare(this.parse(base), this.parse(targ));
262+
return this._writeDiff(diff, pretty);
263+
}
264+
265+
parse(s, o = {}) {
266+
this._parse(s, /([^\n\r\{\}]+?)\s*\{\s*/g, /\}/g, o);
267+
for (let n in o) {
268+
if (n === " keys") { continue; }
269+
o[n] = this.parseBlock(o[n]);
270+
}
271+
return o;
272+
}
273+
274+
parseBlock(s, o = {}) {
275+
return this._parse(s, /([^\s:]+)\s*:/g, /(?:;|$)/g, o);
276+
}
277+
278+
compare(o0, o1, o = {}) {
279+
let keys = o1[" keys"], l=keys.length, arr=[];
280+
for (let i=0; i<l; i++) {
281+
let n = keys[i];
282+
if (!o0[n]) { o[n] = o1[n]; arr.push(n); continue; }
283+
let diff = this._compareBlock(o0[n], o1[n]);
284+
if (diff) { o[n] = diff; arr.push(n); }
285+
}
286+
o[" keys"] = arr;
287+
return o;
288+
}
289+
290+
_compareBlock(o0, o1) {
291+
let keys = o1[" keys"], l=keys.length, arr=[], o;
292+
for (let i=0; i<l; i++) {
293+
let n = keys[i];
294+
if (o0[n] === o1[n]) { continue; }
295+
if (!o) { o = {}; }
296+
o[n] = o1[n];
297+
arr.push(n);
298+
}
299+
if (o) { o[" keys"] = arr; }
300+
return o;
301+
}
302+
303+
_parse(s, keyRE, closeRE, o) {
304+
let i, match, arr=[];
305+
while (match = keyRE.exec(s)) {
306+
let key = match[1];
307+
i = closeRE.lastIndex = keyRE.lastIndex;
308+
if (!(match = closeRE.exec(s))) { console.log("couldn't find close", key); break; }
309+
o[key] = s.substring(i, closeRE.lastIndex-match[0].length).trim();
310+
i = keyRE.lastIndex = closeRE.lastIndex;
311+
arr.push(key);
312+
}
313+
o[" keys"] = arr;
314+
return o;
315+
}
316+
317+
_writeDiff(o, pretty = false) {
318+
let diff = "", ln="\n", s=" ";
319+
if (!pretty) { ln = s = ""; }
320+
let keys = o[" keys"], l=keys.length;
321+
for (let i=0; i<l; i++) {
322+
let n = keys[i];
323+
if (diff) { diff += ln + ln; }
324+
diff += n + s + "{" + ln;
325+
diff += this._writeBlock(o[n], pretty);
326+
diff += "}";
327+
}
328+
return diff;
329+
}
330+
331+
_writeBlock(o, pretty = false) {
332+
let diff = "", ln="\n", t="\t", s=" ";
333+
if (!pretty) { ln = t = s = ""; }
334+
let keys = o[" keys"], l=keys.length;
335+
for (let i=0; i<l; i++) {
336+
let n = keys[i];
337+
diff += t + n + ":" + s + o[n] + ";" + ln;
338+
}
339+
return diff;
340+
}
341+
}

0 commit comments

Comments
 (0)