Skip to content

Commit 5e5f153

Browse files
committed
added:
* include config * tests * excluding classes and ids
1 parent 76895ed commit 5e5f153

10 files changed

+115
-25
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ What if you are using something such as Modernizr and you do not want to minify
4747

4848
Just create a `.rcsrc` or you can add everything in your `package.json` with the value `rcs`
4949

50-
The `.rcsrc`:
50+
The `.rcsrc` or the a own config file:
5151

5252
```json
5353
{
@@ -81,6 +81,7 @@ The `package.json`:
8181
- [processCss](#processcss)
8282
- [process](#process)
8383
- [generateLibraryFile](#generatelibraryfile)
84+
- [includeConfig](#includeconfig)
8485

8586
### processCss
8687

@@ -171,3 +172,15 @@ var CSS_NAME_MAPPING_MIN = {
171172
't': 'another-class'
172173
};
173174
```
175+
176+
**includeConfig([pathLocation])**
177+
178+
> All available configs [here](#rcs-config)
179+
180+
Includes all configs. This function is synchronous.
181+
182+
```js
183+
const rcs = require('rename-css-selectors')
184+
185+
rcs.includeConfig()
186+
```

lib/cli.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,19 @@ cli.generateLibraryFile = (pathString, options, cb) => {
229229
/**
230230
* includes .rcsrc - if not found it will include "rcs" in package.json
231231
*/
232-
cli.includeConfig = () {
232+
cli.includeConfig = (pathString) => {
233+
let configObject;
233234

235+
pathString = pathString || path.join(process.cwd(), '.rcsrc');
236+
configObject = rcs.helper.readJsonToObjSync(pathString);
237+
238+
if (!configObject) {
239+
// package.json .rcs if no other config is found
240+
configObject = rcs.helper.readJsonToObjSync(path.join(process.cwd(), 'package.json')).rcs;
241+
}
242+
243+
if (configObject.exclude) {
244+
rcs.selectorLibrary.setExclude(configObject.exclude);
245+
}
246+
// rcs.selectorLibrary.setExclude(configObject);
234247
};

lib/utils/options/selectorLibrary.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ class SelectorLibrary {
7777

7878
options = _.merge(optionsDefault, options);
7979

80-
if (this.contains.call(this.excludes, matchedSelector)) {
81-
return matchedSelector;
82-
}
83-
8480
// change the objects if origValues are set to false - to get information about the compressed selectors
8581
if (!options.origValues) {
8682
result = this.compressedSelectors[matchedSelector];
@@ -226,7 +222,11 @@ class SelectorLibrary {
226222
continue;
227223
}
228224

229-
selectorLibrarySelector = value[i].slice(1, value[i].length);
225+
if (this.contains.call(this.excludes, value[i])) {
226+
continue;
227+
}
228+
229+
selectorLibrarySelector = value[i].replace(/(\.|#)/g, '');
230230

231231
// save into this.selectors and this.compressedSelectors
232232
this.selectors[selectorLibrarySelector] = this.setValue(value[i]);
@@ -241,6 +241,10 @@ class SelectorLibrary {
241241
return;
242242
}
243243

244+
if (this.contains.call(this.excludes, value)) {
245+
return;
246+
}
247+
244248
selectorLibrarySelector = value.slice(1, value.length);
245249

246250
// save css selector into this.selectors and this.compressedSelectors
@@ -256,14 +260,16 @@ class SelectorLibrary {
256260
* @param {String | Array} toExclude a string or array with string to exclude specific css selectors
257261
*/
258262
setExclude(toExclude) {
263+
if (!toExclude) return;
264+
259265
if (typeof toExclude === 'string') {
260-
this.excludes.append(toExclude);
266+
(this.excludes).push(toExclude);
261267

262268
return;
263269
}
264270

265271
for (let e of toExclude) {
266-
this.excludes.append(e);
272+
(this.excludes).push(e);
267273
}
268274
}
269275

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"coverage": "istanbul cover _mocha -- --recursive",
99
"coveralls": "npm run coverage && coveralls < coverage/lcov.info"
1010
},
11+
"rcs": {
12+
"exclude": [
13+
"js",
14+
"no-js"
15+
]
16+
},
1117
"repository": {
1218
"url": "https://www.github.com/JPeer264/rename-css-selectors",
1319
"type": "git"

test/cli.spec.js

+57-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const resultsCwd = 'test/files/results';
1212
describe('cli.js', () => {
1313
beforeEach(() => {
1414
// reset counter and selectors for tests
15-
rcs.selectorLibrary.selectors = {};
15+
rcs.selectorLibrary.selectors = {};
16+
rcs.selectorLibrary.compressedSelectors = {};
17+
rcs.selectorLibrary.excludes = [];
1618
rcs.nameGenerator.resetCountForTests();
1719
});
1820

@@ -243,4 +245,58 @@ describe('cli.js', () => {
243245
});
244246
});
245247
});
248+
249+
describe('include config', () => {
250+
it('should set the config with package.json', done => {
251+
// include config
252+
cli.includeConfig();
253+
254+
// include new settings
255+
rcs.selectorLibrary.set(['js', 'any-value']);
256+
257+
expect(rcs.selectorLibrary.get('js')).to.equal('js');
258+
expect(rcs.selectorLibrary.get('any-value')).to.equal('a');
259+
260+
done();
261+
});
262+
263+
it('should set the config with .rcsrc', done => {
264+
const file = '.rcsrc';
265+
266+
fs.writeFileSync(file, `{
267+
"exclude": [
268+
"flexbox",
269+
"no-js"
270+
]
271+
}`, {
272+
encoding: 'utf8'
273+
});
274+
275+
// include config
276+
cli.includeConfig();
277+
278+
// include new settings
279+
rcs.selectorLibrary.set(['flexbox', 'any-value']);
280+
281+
expect(rcs.selectorLibrary.get('flexbox')).to.equal('flexbox');
282+
expect(rcs.selectorLibrary.get('any-value')).to.equal('a');
283+
284+
fs.removeSync(file);
285+
286+
done();
287+
});
288+
289+
it('should set the config with package.json', done => {
290+
// include config
291+
cli.includeConfig('test/files/config.json');
292+
293+
// include new settings
294+
rcs.selectorLibrary.set(['own-file', 'any-value']);
295+
296+
expect(rcs.selectorLibrary.get('own-file')).to.equal('own-file');
297+
expect(rcs.selectorLibrary.get('any-value')).to.equal('a');
298+
299+
done();
300+
});
301+
});
246302
});
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"exclude": [
3-
"js",
3+
"own-file",
44
"no-js"
55
]
66
}

test/files/package.json

-9
This file was deleted.

test/rcs/helper.spec.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,15 @@ describe('helper.js', () => {
3636
});
3737

3838
it('should read a json file and turn it to an object', done => {
39-
const obj = rcs.helper.readJsonToObjSync('test/files/package.json');
39+
const obj = rcs.helper.readJsonToObjSync('test/files/config.json');
4040

4141
expect(obj).to.be.an('object');
42-
expect(obj.rcs).to.be.an('object');
4342

4443
done();
4544
});
4645

4746
it('should fail to read a file', done => {
48-
const obj = rcs.helper.readJsonToObjSync('not/existing/path/package.json');
47+
const obj = rcs.helper.readJsonToObjSync('not/existing/path/config.json');
4948

5049
expect(obj).to.be.false;
5150

test/rcs/replace.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const resultsCwd = 'test/files/results';
1010
describe('rcs file replace', () => {
1111
beforeEach(() => {
1212
// reset counter and selectors for tests
13-
rcs.selectorLibrary.selectors = {};
13+
rcs.selectorLibrary.selectors = {};
14+
rcs.selectorLibrary.compressedSelectors = {};
15+
rcs.selectorLibrary.excludes = [];
16+
1417
rcs.nameGenerator.resetCountForTests();
1518
});
1619

test/rcs/selectorLibrary.spec.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ describe('rcs selector library', () => {
77
describe('set new values', () => {
88
beforeEach(() => {
99
// reset counter and selectors for tests
10-
rcs.selectorLibrary.selectors = {};
10+
rcs.selectorLibrary.selectors = {};
11+
rcs.selectorLibrary.compressedSelectors = {};
12+
rcs.selectorLibrary.excludes = [];
13+
1114
rcs.nameGenerator.resetCountForTests();
1215
});
1316

0 commit comments

Comments
 (0)