Skip to content

Commit eec693f

Browse files
committed
add generateMappingSync
1 parent 8d52c17 commit eec693f

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

index.js

+54
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,60 @@ renameCssSelectors.processCss = (pathString, options, cb) => {
210210
renameCssSelectors.process(pathString, options, cb);
211211
} // /processCss
212212

213+
/**
214+
* The synchronous method of generateMapping
215+
*/
216+
renameCssSelectors.generateMappingSync = (pathString, options) => {
217+
let fileName = 'renaming_map';
218+
let fileNameExt = '.json';
219+
let mappingName = 'CSS_NAME_MAPPING';
220+
221+
const optionsDefault = {
222+
cssMapping: true,
223+
cssMappingMin: false,
224+
extended: false,
225+
json: true,
226+
origValues: true,
227+
isSelectors: true,
228+
overwrite: false
229+
}
230+
231+
options = _.merge(optionsDefault, options);
232+
233+
if (options.cssMappingMin) {
234+
options.origValues = false;
235+
mappingName = 'CSS_NAME_MAPPING_MIN';
236+
fileName = fileName + '_min';
237+
}
238+
239+
if (typeof options.cssMappingMin === 'string') {
240+
mappingName = options.cssMappingMin;
241+
fileName = options.cssMappingMin;
242+
}
243+
244+
if (typeof options.cssMapping === 'string') {
245+
fileName = options.cssMapping;
246+
}
247+
248+
const cssMappingArray = rcs.selectorLibrary.getAll({
249+
extended: options.extended,
250+
origValues: options.origValues,
251+
isSelectors: options.isSelectors
252+
});
253+
254+
let cssMappingJsonString = rcs.helper.objectToJson(cssMappingArray);
255+
let writeData = cssMappingJsonString;
256+
let newPath = path.join(pathString, fileName);
257+
258+
// no json
259+
if (!options.json) {
260+
writeData = `var ${ mappingName } = ${ cssMappingJsonString };`
261+
fileNameExt = '.js';
262+
}
263+
264+
rcs.helper.saveSync(`${ newPath }${ fileNameExt }`, writeData, { overwrite: options.overwrite });
265+
} // /generateMappingSync
266+
213267
/**
214268
* @typedef {Object} generateMappingOptions
215269
* @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

test/index.spec.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,18 @@ describe('app.js', () => {
317317
expect(cssMapping['.jp-block__element']).to.equal('b');
318318

319319
done();
320-
321320
});
322321
});
323322

323+
it('should create the normal mapping file synchornously', () => {
324+
app.generateMappingSync(testCwd);
325+
326+
const cssMapping = json.readToObjSync(testCwd + '/renaming_map.json', 'utf8');
327+
328+
expect(cssMapping['.jp-block']).to.equal('a');
329+
expect(cssMapping['.jp-block__element']).to.equal('b');
330+
});
331+
324332
it('should create the minified mapping file', done => {
325333
app.generateMapping(testCwd, {
326334
cssMapping: false,

0 commit comments

Comments
 (0)