forked from JDillon522/brei-assemble-helpers
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdateScss.js
178 lines (131 loc) · 3.78 KB
/
updateScss.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
'use strict';
const fs = require('fs');
const _ = require('lodash');
const collection = [
{
'name': 'atoms',
'searchName': 'atoms',
'dir': './app/assemble/atoms',
'recursive': true
},
{
'name': 'molecules',
'searchName': 'molecules',
'dir': './app/assemble/molecules',
'recursive': true
},
{
'name': 'organisms',
'searchName': 'organisms',
'dir': './app/assemble/organisms',
'recursive': true
},
{
'name': 'templates',
'searchName': 'assemble',
'dir': './app/assemble/.',
'recursive': false
}
];
const ignored = [
'.git',
'node_modules',
'.DS_Store',
'thumbs.db',
'Thumbs.db'
];
/*
This script runs through each directory in the collection object above and generates an appropriate .scss file in the
respective directory under /app/scss/.
The recursive parameter is important. It is set to false on templates so that the templates folder isn't filled with
all the sass files for molecules and organisms.
Ideas for the future for this include:
- Scanning the handlebars files for a flag that prevents a sass file from being generated.
- Scanning for a flag that determines a different composite sass file for the intention of including in a different
master css file other than main.css (for example, level.css) https://github.com/BarkleyREI/generator-brei-app/issues/64
*/
collection.forEach(function (data) {
let ff = fs.readdirSync(data.dir);
ff = _.difference(ff, ignored);
ff = ff.filter(hbs => hbs.indexOf('.hbs') !== -1);
var names = [];
var finalScssFile = '';
var finalPath = './app/scss/' + data.name + '/_assemble-' + data.name + '.scss';
ff.forEach(function (entry) {
// Add names to be added to .scss file
var regex = new RegExp('^.+' + data.searchName + '/');
if (!/^_+/.test(entry)/* && data.name !== 'templates'*/) {
entry = '_' + entry;
}
if (data.name === 'templates') {
if (entry !== '_index.hbs') {
names.push(entry);
}
} else {
names.push(entry);
}
});
names.sort();
let rmf = writeMissingFiles(data, names);
rmf.then(function () {
names.forEach(function (name) {
var importPath = '@import \'';
name = name.replace('.hbs', '');
importPath = importPath + name;
finalScssFile = finalScssFile + importPath + '\';\n';
});
if (finalScssFile !== '') {
fs.writeFileSync(finalPath, finalScssFile, function (err) {
if (err) {
throw err;
}
});
}
});
});
// Check to see if same name .scss file exists. If not, create one
function writeMissingFiles(data, names) {
// console.log("\n====\nwriteMissingFiles()\n\n" + data + "\n" + entry + "\n====\n");
// var name = entry.replace('.hbs', '');
// var filename = data.name + '/' + name + '.scss';
// var readPath = './app/scss/' + filename;
let fle = fs.readdirSync('./app/scss/' + data.name);
const exclu = [
'_assemble-organisms.scss',
'_assemble-molecules.scss',
'_assemble-templates.scss'
];
fle = _.difference(fle, exclu);
fle = _.difference(fle, ignored);
fle = fle.map(item => item.replace('.scss', ''));
names = names.map(item => item.replace('.hbs', ''));
let diff = _.difference(names, fle);
return new Promise(function (resolve, reject) {
let errored = false;
diff.forEach(function (x) {
var cleanName = x.replace('_', '');
var content = '.' + cleanName + ' {\n\n}\n';
var filename = x + '.scss';
if (cleanName.length >= 1) {
fs.writeFile('./app/scss/' + data.name + '/' + filename, content, function (err) {
if (err) {
errored = err;
}
console.log(properName(data.name) + ' not found. Creating ' + filename);
});
}
});
if (errored) {
reject(errored);
} else {
resolve();
}
});
};
function singular(str) {
return str.replace(/s$/, '');
}
function properName(str) {
let name = str[0].toUpperCase() + str.substring(1);
return singular(name);
}