Skip to content

Commit eee3696

Browse files
committed
feat change generateLibraryFile to generateMapping
1 parent b9e9bc9 commit eee3696

File tree

2 files changed

+59
-107
lines changed

2 files changed

+59
-107
lines changed

index.js

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ renameCssSelectors.processCss = (pathString, options, cb) => {
156156
} // /processCss
157157

158158
/**
159-
* @typedef {Object} generateLFOptions
159+
* @typedef {Object} generateMappingOptions
160160
* @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
161161
* @property {Boolean | String} [cssMappingMin=false] like the property cssMapping
162162
* @property {Boolean} [extended=false] defines if metadata should be added to the selector
@@ -169,16 +169,20 @@ renameCssSelectors.processCss = (pathString, options, cb) => {
169169
* @todo generrate a json config file
170170
*
171171
* @param {String} pathString where it should get saved
172-
* @param {generateLFOptions} [options]
172+
* @param {generateMappingOptions} [options]
173173
*/
174-
renameCssSelectors.generateLibraryFile = (pathString, options, cb) => {
174+
renameCssSelectors.generateMapping = (pathString, options, cb) => {
175+
let newPath = path.join(pathString, 'renaming_map');
175176
let mappingName = 'CSS_NAME_MAPPING';
176-
let mappingNameMin = 'CSS_NAME_MAPPING_MIN';
177+
let fileNameExt = '.json';
178+
177179
const optionsDefault = {
178180
cssMapping: true,
179181
cssMappingMin: false,
180182
extended: false,
181-
json: false
183+
json: true,
184+
origValues: true,
185+
isSelectors: true
182186
}
183187

184188
// set cb if options are not set
@@ -189,59 +193,45 @@ renameCssSelectors.generateLibraryFile = (pathString, options, cb) => {
189193

190194
options = _.merge(optionsDefault, options);
191195

192-
async.parallel([
193-
callback => {
194-
// normal classes
195-
if (options.cssMapping) {
196-
const newPath = path.join(pathString, 'renaming_map.js');
197-
const cssMappingArray = rcs.selectorLibrary.getAll({
198-
origValues: true,
199-
isSelectors: true,
200-
extended: options.extended,
201-
});
196+
if (options.cssMappingMin) {
197+
options.origValues = false;
198+
newPath = path.join(pathString, 'renaming_map_min');
199+
mappingName = 'CSS_NAME_MAPPING_MIN';
200+
}
202201

203-
if (typeof options.cssMapping === 'string') {
204-
mappingName = options.cssMapping;
205-
}
202+
if (typeof options.cssMappingMin === 'string') {
203+
mappingName = options.cssMappingMin;
204+
}
206205

207-
rcs.helper.save(newPath, `var ${mappingName} = ${rcs.helper.objectToJson(cssMappingArray)} ;`, (err, data) => {
208-
if (err) callback(err);
206+
if (typeof options.cssMapping === 'string') {
207+
mappingName = options.cssMapping;
208+
}
209209

210-
callback(null, data);
211-
});
212-
} else {
213-
callback(null);
214-
}
215-
},
216-
callback => {
217-
// compressed classes
218-
if (options.cssMappingMin) {
219-
const newPathMin = path.join(pathString, 'renaming_map_min.js');
220-
const cssMappingMinArray = rcs.selectorLibrary.getAll({
221-
origValues: false,
222-
isSelectors: true,
223-
extended: options.extended,
224-
});
210+
const cssMappingArray = rcs.selectorLibrary.getAll({
211+
extended: options.extended,
212+
origValues: options.origValues,
213+
isSelectors: options.isSelectors
214+
});
225215

226-
if (typeof options.cssMapping === 'string') {
227-
mappingName = options.cssMapping;
228-
}
216+
let cssMappingJsonString = rcs.helper.objectToJson(cssMappingArray);
217+
let writeData = cssMappingJsonString;
229218

230-
rcs.helper.save(newPathMin, `var ${mappingNameMin} = ${rcs.helper.objectToJson(cssMappingMinArray)} ;`, (err, data) => {
231-
if (err) callback(err);
219+
// no json
220+
if (!options.json) {
221+
writeData = `var ${ mappingName } = ${ cssMappingJsonString };`
222+
fileNameExt = '.js';
223+
}
232224

233-
callback(null, data);
234-
});
235-
} else {
236-
callback(null);
237-
}
238-
}
239-
], (err, results) => {
240-
if (err) return cb(err);
225+
rcs.helper.save(`${ newPath }${ fileNameExt }`, writeData, (err, data) => {
226+
if (err) cb(err);
241227

242-
return cb(null);
228+
cb(null, data);
243229
});
244-
}; // /generateLibraryFile
230+
}; // /generateMapping
231+
232+
renameCssSelectors.loadMapping = (pathString, options) => {
233+
234+
}; // /loadMapping
245235

246236
/**
247237
* includes .rcsrc - if not found it will include "rcs" in package.json

test/index.spec.js

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const app = require('../');
44
const rcs = require('rcs-core');
55
const fs = require('fs-extra');
6+
const json = require('json-extra');
67
const expect = require('chai').expect;
78

89
const testCwd = 'test/files/testCache';
@@ -194,101 +195,62 @@ describe('app.js', () => {
194195
});
195196

196197
it('should create the normal library file', done => {
197-
app.generateLibraryFile(testCwd, (err, data) => {
198-
const cssMapping = fs.readFileSync(testCwd + '/renaming_map.js', 'utf8');
198+
app.generateMapping(testCwd, (err, data) => {
199+
const cssMapping = json.readToObjSync(testCwd + '/renaming_map.json', 'utf8');
199200

200201
expect(err).to.not.exist;
201-
202-
// TODO read variable and check values
203-
// expect(cssMapping['jp-block']).to.equal('a');
202+
expect(cssMapping['.jp-block']).to.equal('a');
203+
expect(cssMapping['.jp-block__element']).to.equal('b');
204204

205205
done();
206206

207207
});
208208
});
209209

210210
it('should create the minified library file', done => {
211-
app.generateLibraryFile(testCwd, {
211+
app.generateMapping(testCwd, {
212212
cssMapping: false,
213213
cssMappingMin: true
214214
}, (err, data) => {
215-
const cssMapping = fs.readFileSync(testCwd + '/renaming_map_min.js', 'utf8');
215+
const cssMapping = json.readToObjSync(testCwd + '/renaming_map_min.json', 'utf8');
216216

217217
expect(err).to.not.exist;
218-
219-
// TODO read variable and check values
220-
// expect(cssMapping['jp-block']).to.equal('a');
218+
expect(cssMapping['.a']).to.equal('jp-block');
219+
expect(cssMapping['.b']).to.equal('jp-block__element');
221220

222221
done();
223222

224223
});
225224
});
226225

227226
it('should create the extended normal library file', done => {
228-
app.generateLibraryFile(testCwd, {
227+
app.generateMapping(testCwd, {
229228
extended: true
230229
}, (err, data) => {
231-
const cssMapping = fs.readFileSync(testCwd + '/renaming_map.js', 'utf8');
230+
const cssMapping = json.readToObjSync(testCwd + '/renaming_map.json', 'utf8');
232231

233232
expect(err).to.not.exist;
234-
235-
// TODO read variable and check values
236-
// expect(cssMapping['jp-block']).to.equal('a');
233+
expect(cssMapping['.jp-block']).to.be.an('object');
234+
expect(cssMapping['.jp-block']).to.have.any.keys('type', 'typeChar');
235+
expect(cssMapping['.jp-block']['type']).to.equal('class');
237236

238237
done();
239238

240239
});
241240
});
242241

243242
it('should create the minified library file', done => {
244-
app.generateLibraryFile(testCwd, {
243+
app.generateMapping(testCwd, {
245244
cssMapping: false,
246245
cssMappingMin: true,
247246
extended: true
248247
}, (err, data) => {
249-
const cssMapping = fs.readFileSync(testCwd + '/renaming_map_min.js', 'utf8');
250-
251-
expect(err).to.not.exist;
252-
253-
// TODO read variable and check values
254-
// expect(cssMapping['jp-block']).to.equal('a');
255-
256-
done();
257-
258-
});
259-
});
260-
261-
it('should create the both library files', done => {
262-
app.generateLibraryFile(testCwd, {
263-
cssMapping: true,
264-
cssMappingMin: true
265-
}, (err, data) => {
266-
const cssMapping = fs.readFileSync(testCwd + '/renaming_map.js', 'utf8');
267-
const cssMappingMin = fs.readFileSync(testCwd + '/renaming_map_min.js', 'utf8');
248+
const cssMappingMin = json.readToObjSync(testCwd + '/renaming_map_min.json', 'utf8');
268249

269250
expect(err).to.not.exist;
270-
271-
// TODO read variable and check values
272-
// expect(cssMapping['jp-block']).to.equal('a');
273-
274-
done();
275-
276-
});
277-
});
278-
279-
it('should create the both extended library files', done => {
280-
app.generateLibraryFile(testCwd, {
281-
extended: true,
282-
cssMapping: true,
283-
cssMappingMin: true
284-
}, (err, data) => {
285-
const cssMapping = fs.readFileSync(testCwd + '/renaming_map.js', 'utf8');
286-
const cssMappingMin = fs.readFileSync(testCwd + '/renaming_map_min.js', 'utf8');
287-
288-
expect(err).to.not.exist;
289-
290-
// TODO read variable and check values
291-
// expect(cssMapping['jp-block']).to.equal('a');
251+
expect(cssMappingMin['.a']).to.be.an('object');
252+
expect(cssMappingMin['.a']).to.have.any.keys('type', 'typeChar');
253+
expect(cssMappingMin['.a']['type']).to.equal('class');
292254

293255
done();
294256

0 commit comments

Comments
 (0)