Skip to content

Commit edb0bc7

Browse files
committed
updated:
* to helper function * docblock added save config JS file
1 parent f8bdc91 commit edb0bc7

File tree

3 files changed

+158
-80
lines changed

3 files changed

+158
-80
lines changed

lib/cli.js

+91-16
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@ const _ = require('lodash');
1414
*/
1515
const cli = module.exports = {};
1616

17-
cli.save = (newFilePath, data, cb) => {
18-
// @todo check if the filepath has an .ext
19-
// @todo check if the new filename is the same .ext
20-
// @todo do not overwrite file! use for that an flag
21-
fs.mkdirs(path.dirname(newFilePath), (err) => {
22-
fs.writeFile(newFilePath, data, (err, data) => {
23-
if (err) return cb(err);
24-
25-
cb(null, "Successfully wrote " + newFilePath);
26-
});
27-
});
28-
};
29-
3017
/**
3118
* @typedef {Object} processOptions
3219
* @property {Boolean} [collectSelectors=false] git dif the triggered files are css files
@@ -87,7 +74,7 @@ cli.process = (pathString, options, cb) => {
8774

8875
joinedPath = path.join(options.newPath, filePath);
8976

90-
cli.save(joinedPath, data.data, (err) => {
77+
rcs.helper.save(joinedPath, data.data, (err) => {
9178
if (err) callback(err);
9279

9380
callback();
@@ -112,7 +99,7 @@ cli.process = (pathString, options, cb) => {
11299

113100
joinedPath = path.join(options.newPath, filePath);
114101

115-
cli.save(joinedPath, data.data, (err) => {
102+
rcs.helper.save(joinedPath, data.data, (err) => {
116103
if (err) callback(err);
117104

118105
callback();
@@ -149,4 +136,92 @@ cli.processCss = (pathString, options, cb) => {
149136
options.collectSelectors = true;
150137

151138
cli.process(pathString, options, cb);
152-
}
139+
} // /processCss
140+
141+
/**
142+
* @typedef {Object} generateLFOptions
143+
* @property {Boolean | String} [cssMapping=true] true will generate the css mapping. A string will generate the css mapping file and the object is called like the string
144+
* @property {Boolean | String} [cssMappingMin=false] like the property cssMapping
145+
* @property {Boolean} [extended=false] defines if metadata should be added to the selector
146+
* @property {Boolean} [json=false] defines if metadata should be added to the selector
147+
*/
148+
/**
149+
* generates a file including all old and new selectors/names
150+
* includes also unused class selectors
151+
*
152+
* @todo generrate a json config file
153+
*
154+
* @param {String} pathString where it should get saved
155+
* @param {generateLFOptions} [options]
156+
*/
157+
cli.generateLibraryFile = (pathString, options, cb) => {
158+
let mappingName = 'CSS_NAME_MAPPING';
159+
let mappingNameMin = 'CSS_NAME_MAPPING_MIN';
160+
const optionsDefault = {
161+
cssMapping: true,
162+
cssMappingMin: false,
163+
extended: false,
164+
json: false
165+
}
166+
167+
// set cb if options are not set
168+
if (typeof cb !== 'function') {
169+
cb = options;
170+
options = {};
171+
}
172+
173+
options = _.merge(optionsDefault, options);
174+
175+
async.parallel([
176+
callback => {
177+
// normal classes
178+
if (options.cssMapping) {
179+
const newPath = path.join(pathString, 'renaming_map.js');
180+
const cssMappingArray = rcs.selectorLibrary.getAll({
181+
origValues: true,
182+
isSelectors: true,
183+
extended: options.extended,
184+
});
185+
186+
if (typeof options.cssMapping === 'string') {
187+
mappingName = options.cssMapping;
188+
}
189+
190+
rcs.helper.save(newPath, 'var ' + mappingName + ' = ' + rcs.helper.objectToJson(cssMappingArray) + ';', (err, data) => {
191+
if (err) callback(err);
192+
193+
callback(null, data);
194+
});
195+
} else {
196+
callback(null);
197+
}
198+
},
199+
callback => {
200+
// compressed classes
201+
if (options.cssMappingMin) {
202+
const newPathMin = path.join(pathString, 'renaming_map_min.js');
203+
const cssMappingMinArray = rcs.selectorLibrary.getAll({
204+
origValues: false,
205+
isSelectors: true,
206+
extended: options.extended,
207+
});
208+
209+
if (typeof options.cssMapping === 'string') {
210+
mappingName = options.cssMapping;
211+
}
212+
213+
rcs.helper.save(newPathMin, 'var ' + mappingNameMin + ' = ' + rcs.helper.objectToJson(cssMappingMinArray) + ';', (err, data) => {
214+
if (err) callback(err);
215+
216+
callback(null, data);
217+
});
218+
} else {
219+
callback(null);
220+
}
221+
}
222+
], (err, results) => {
223+
if (err) return cb(err);
224+
225+
return cb(null);
226+
});
227+
}; // /generateLibraryFile

lib/utils/options/selectorLibrary.js

+66-64
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const rcs = require('../rcs');
4+
const _ = require('lodash');
45

56
/**
67
* a library holding all information about the selectors including its old states
@@ -9,7 +10,6 @@ class SelectorLibrary {
910
constructor() {
1011
this.selectors = {};
1112
this.compressedSelectors = {};
12-
this.created
1313
}
1414

1515
/**
@@ -21,16 +21,15 @@ class SelectorLibrary {
2121
get(selector, options) {
2222
// @todo set options with isSelectors
2323
let matchedSelector = selector.replace(/(\.|#)/, ''); // replaces the first character from a css selector `#test` and `.test` into `test`
24-
let result = this.selectors[matchedSelector];;
25-
26-
// set options if its is not set or is not an object
27-
options = !options && typeof options !== 'object' ? {} : options;
24+
let result = this.selectors[matchedSelector];
2825

26+
const optionsDefault = {
27+
origValues: true,
28+
isSelectors: false,
29+
isCompressed: true
30+
}
2931

30-
// set default parameters if they are not set in the options
31-
options.isCompressed = typeof options.isCompressed !== 'undefined' ? options.isCompressed : true; // default true
32-
options.isSelectors = typeof options.isSelectors !== 'undefined' ? options.isSelectors : false; // default false
33-
options.origValues = typeof options.origValues !== 'undefined' ? options.origValues : true; // default true
32+
options = _.merge(optionsDefault, options);
3433

3534
// change the objects if origValues are set to false - to get information about the compressed selectors
3635
if (!options.origValues) {
@@ -52,72 +51,88 @@ class SelectorLibrary {
5251
return result;
5352
}
5453

54+
/**
55+
* @typedef {Object} getAllOptions
56+
* @property {Boolean} [origValues=true] if it should return the original values or the compressed one
57+
* @property {Boolean} [regex=false] if it should return a regex string
58+
* @property {Boolean} [isSelectors=false] true appends appends the # for IDs or . for Classes
59+
* @property {Boolean} [extended=false] extend the normal return value with metadata - has NO EFFECT in combination with REGEX
60+
*/
5561
/**
5662
* gets all selectors
5763
*
58-
* @param {Boolean} [origValues=true]
59-
* @param {Boolean} [makeRegex=false] should return a regex string
60-
* @return {Object} returns this.selectors
64+
* @todo add combination with isSelectors and extended
65+
*
66+
* @param {getAllOptions} [options]
67+
* @return {String | Object} returns either a regex string or an object with elements, depends on the setted options
6168
*/
6269
getAll(options) {
6370
let regex;
6471
let selector;
65-
let selectors;
72+
let selectors = this.selectors;
6673
let compressedSelector;
6774
let originalSelector;
6875
let result = {};
6976
let resultArray = [];
7077

71-
// set options if its is not set or is not an object
72-
options = !options && typeof options !== 'object' ? {} : options;
78+
const optionsDefault = {
79+
origValues: true,
80+
regex: false,
81+
isSelectors: false,
82+
extended: false
83+
}
7384

74-
// set default parameters if they are not set in the options
75-
options.origValues = typeof options.origValues !== 'undefined' ? options.origValues : true; // default true
76-
options.regex = typeof options.regex !== 'undefined' ? options.regex : false; // default false
77-
options.isSelectors = typeof options.isSelectors !== 'undefined' ? options.isSelectors : false; // default false
85+
options = _.merge(optionsDefault, options);
7886

79-
selectors = this.selectors;
87+
if (!options.extended) {
88+
for (selector in selectors) {
89+
compressedSelector = selectors[selector].compressedSelector;
90+
originalSelector = selector;
8091

81-
for (selector in selectors) {
82-
compressedSelector = selectors[selector].compressedSelector;
83-
originalSelector = selector;
92+
if (options.origValues) { // save originalSelectors
93+
result[selector] = compressedSelector
94+
resultArray.push(originalSelector);
95+
} else { // save compressedSelectors
96+
result[compressedSelector] = originalSelector;
97+
resultArray.push(compressedSelector);
98+
}
99+
}
84100

85-
if (options.origValues) { // save originalSelectors
86-
result[selector] = compressedSelector
87-
resultArray.push(originalSelector);
88-
} else { // save compressedSelectors
89-
result[compressedSelector] = originalSelector;
90-
resultArray.push(compressedSelector);
101+
// sort array by it's length to avoid e.g. BEM syntax
102+
if (options.regex) {
103+
resultArray = resultArray.sort((a, b) => {
104+
return b.length - a.length;
105+
});
91106
}
92-
}
93107

94-
// sort array by it's length to avoid e.g. BEM syntax
95-
resultArray = resultArray.sort((a, b) => {
96-
return b.length - a.length;
97-
});
98-
99-
if (options.isSelectors) {
100-
resultArray = resultArray.map(value => {
101-
let selectorMap = this.get(value, {
102-
origValues: options.origValues,
103-
isSelectors: options.isSelectors,
104-
isCompressed: false
108+
if (options.isSelectors) {
109+
resultArray = resultArray.map(value => {
110+
let selectorMap = this.get(value, {
111+
origValues: options.origValues,
112+
isSelectors: options.isSelectors,
113+
isCompressed: false
114+
});
115+
116+
return selectorMap.typeChar + value;
105117
});
118+
}
106119

107-
return selectorMap.typeChar + value;
108-
});
109-
}
120+
// return a new regex
121+
if (options.regex) {
122+
regex = resultArray.length === 0 ? undefined : new RegExp(resultArray.join("|"), 'g');
110123

111-
// console.log(resultArray);
124+
return regex;
125+
}
112126

113-
// return a new regex
114-
if (options.regex) {
115-
regex = resultArray.length === 0 ? undefined : new RegExp(resultArray.join("|"), 'g');
127+
return result;
128+
}
116129

117-
return regex;
130+
// if it is extended
131+
if (options.origValues) {
132+
return this.selectors;
118133
}
119134

120-
return result;
135+
return this.compressedSelectors;
121136
}
122137

123138
/**
@@ -154,7 +169,7 @@ class SelectorLibrary {
154169

155170
selectorLibrarySelector = value.slice(1, value.length);
156171

157-
// save into this.selectors and this.compressedSelectors
172+
// save css selector into this.selectors and this.compressedSelectors
158173
this.selectors[selectorLibrarySelector] = this.setValue(value);
159174
this.compressedSelectors[this.selectors[selectorLibrarySelector].compressedSelector] = this.selectors[selectorLibrarySelector];
160175

@@ -194,19 +209,6 @@ class SelectorLibrary {
194209
compressedSelector: compressedSelector
195210
};
196211
}
197-
198-
/**
199-
* generates a file including all old and new selectors/names
200-
* includes also unused class selectors
201-
*/
202-
generateLibraryFile() {
203-
// @todo available options
204-
// savePath
205-
// justIds?
206-
// justClasses?
207-
// @todo get all selectors
208-
// @todo save file into path
209-
}
210212
};
211213

212214
/**

lib/utils/rcs.js

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const doRequire = name => {
1010
return rcs[name] = require('./options/' + name);
1111
};
1212

13+
doRequire('helper');
1314
doRequire('nameGenerator');
1415
doRequire('selectorLibrary');
1516
doRequire('fileReplace');

0 commit comments

Comments
 (0)