forked from codecombat/codecombat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractViewSassFiles.js
60 lines (53 loc) · 1.86 KB
/
extractViewSassFiles.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
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
const readdirSync = require('recursive-readdir-sync');
const fs = require('fs');
const _ = require('lodash');
const sassPaths = readdirSync('./app/styles');
const sassMap = {};
const usedSassFile = {};
sassPaths.forEach(path => {
const code = fs.readFileSync(path).toString();
return code.split('\n').forEach(line => {
const regex = /^#([^\., \(\)]+)/;
if (regex.test(line)) {
return line.split(/ *, */).forEach(subline => {
const id = subline.match(regex)[1];
// console.log path, id
if (sassMap[id] == null) { sassMap[id] = []; }
usedSassFile[path] = false;
if (!_.contains(sassMap[id], path)) {
return sassMap[id].push(path);
}
});
}
});
});
console.log("this many sass IDs:", Object.keys(sassMap).length);
const viewPaths = readdirSync('./app/views');
viewPaths.forEach(path => {
const code = fs.readFileSync(path).toString();
return code.split('\n').forEach(line => {
const regex = / id: ['"](.+)['"]/;
if (regex.test(line)) {
const id = line.match(regex)[1];
return (sassMap[id] || []).forEach(sassPath => {
usedSassFile[sassPath] = true;
const requireLine = `require('${sassPath}')\n`;
const newCode = requireLine + code;
if (!_.contains(code, requireLine)) {
// fs.writeFileSync(path, newCode)
return console.log(`Will add ${sassPath} to ${path}`);
}
});
}
});
});
// process.exit()
console.log(usedSassFile);
console.log("These sass files don't have IDs:");
console.log(_.difference(sassPaths, Object.keys(usedSassFile)).join('\n'));