Skip to content

Commit e0a7acf

Browse files
committed
feat overwrite setting in generateMapping
update readme due to loadMapping and generateMapping
1 parent ed07f45 commit e0a7acf

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ yarn add rename-css-selectors
3030
## Usage
3131

3232
```js
33-
const rcs = require('rename-css-selectors')
33+
const rcs = require('rename-css-selectors');
34+
35+
// if you have some generated mappings - load them!
36+
// you can also specify the string although it does not exist yet.
37+
rcs.loadMapping('./renaming_map.json');
3438

3539
// first you have to process the css files
3640
// to get a list of variables you want to minify
@@ -41,6 +45,12 @@ rcs.processCss('**/*.css', options, err => {
4145
// now it is time to process all other files
4246
rcs.process([ '**/*.js', '**/*.html' ], options, err => {
4347
// that's it
48+
49+
// maybe you want to add the new selectors to your previous generated mappings
50+
// do not worry, your old settings are still here, in case you used `loadMapping`
51+
rcs.generateMapping('./', { overwrite: true }, err => {
52+
// the mapping file is now saved
53+
});
4454
});
4555
});
4656
```
@@ -99,10 +109,6 @@ Let's exclude 4 classes and id's: `js`, `flexbox`, `canvas` and `svg`
99109
}
100110
```
101111

102-
### Include renamed classes from other project
103-
104-
**todo**
105-
106112
## Methods
107113

108114
- [processCss](#processcss)
@@ -173,16 +179,17 @@ rcs.process('**/*.js', options, err => {
173179
174180
**generateMapping(pathLocation[, options], callback)**
175181
176-
> *Note:* if you are using the options either `cssMapping` or `cssMappingMin must be set to true. Both to `true` at the same time are not valid.
182+
> *Note:* if you are using the options either `cssMapping` or `cssMappingMin` must be set to true. Both to `true` at the same time are not valid.
177183
178-
Generates mapping files: all minified, all original selectors or both. They are stored as object in a variable. The file is named as `renaming_map.js` or `renaming_map_min.js`.
184+
Generates mapping files: all minified, all original selectors or both. They are stored as object in a variable. The file is named as `renaming_map.json` or `renaming_map_min.json`.
179185
180186
Options:
181187
182-
- cssMapping (string | boolean): writes `renaming_map.js`. If it is a string, the string is the new file name. Default is `true`
183-
- cssMappingMin (string | boolean): writes `renaming_map_min.js`. If it is a string, the string is the new file name. Default is `false`
188+
- cssMapping (string | boolean): writes `renaming_map.json`. If it is a string, the string is the new file name. Default is `true`
189+
- cssMappingMin (string | boolean): writes `renaming_map_min.json`. If it is a string, the string is the new file name. Default is `false`
184190
- extended (boolean): instead of a string it writes an object with meta information. Default is `false`
185-
- json (boolean): writes an json instead of a js. Default is `true`
191+
- json (boolean): writes a `json` instead of a `js`. Default is `true`
192+
- overwrite (boolean): if it should overwrite the existing mapping. Default is `false`
186193
187194
```js
188195
const rcs = require('rename-css-selectors')
@@ -222,7 +229,7 @@ const rcs = require('rename-css-selectors')
222229
// loadMapping is synchronous
223230
// the first parameter can be either a string to the file
224231
// or the json object directly
225-
rcs.loadMapping('./renaming_map_min.json', options);
232+
rcs.loadMapping('./renaming_map.json', options);
226233

227234
rcs.process('**/*.html', err => {
228235
...

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ renameCssSelectors.generateMapping = (pathString, options, cb) => {
182182
extended: false,
183183
json: true,
184184
origValues: true,
185-
isSelectors: true
185+
isSelectors: true,
186+
overwrite: false
186187
}
187188

188189
// set cb if options are not set
@@ -222,7 +223,7 @@ renameCssSelectors.generateMapping = (pathString, options, cb) => {
222223
fileNameExt = '.js';
223224
}
224225

225-
rcs.helper.save(`${ newPath }${ fileNameExt }`, writeData, (err, data) => {
226+
rcs.helper.save(`${ newPath }${ fileNameExt }`, writeData, { overwrite: options.overwrite }, (err, data) => {
226227
if (err) cb(err);
227228

228229
cb(null, data);

test/index.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,29 @@ describe('app.js', () => {
256256

257257
});
258258
});
259+
260+
it('should overwrite mapping files', done => {
261+
app.generateMapping(testCwd, (err, data) => {
262+
app.generateMapping(testCwd, { overwrite: true }, (err2, data) => {
263+
expect(err).to.not.exist;
264+
expect(err2).to.not.exist;
265+
266+
done();
267+
});
268+
});
269+
});
270+
271+
it('should not overwrite mapping files', done => {
272+
app.generateMapping(testCwd, (err, data) => {
273+
app.generateMapping(testCwd, (err2, data) => {
274+
expect(err).to.not.exist;
275+
expect(err2).to.exist;
276+
expect(err2.message).to.equal('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.');
277+
278+
done();
279+
});
280+
});
281+
});
259282
});
260283

261284
describe('load mapping', () => {

0 commit comments

Comments
 (0)