Skip to content

Commit 78fcd12

Browse files
committed
moved methods to lib
1 parent eec693f commit 78fcd12

File tree

10 files changed

+479
-383
lines changed

10 files changed

+479
-383
lines changed

index.js

Lines changed: 13 additions & 383 deletions
Original file line numberDiff line numberDiff line change
@@ -15,386 +15,16 @@ const _ = require('lodash');
1515
*/
1616
const renameCssSelectors = module.exports = {};
1717

18-
/**
19-
* The synchronous method for process
20-
*/
21-
renameCssSelectors.processSync = (pathString, options) => {
22-
const optionsDefault = {
23-
collectSelectors: false,
24-
overwrite: false,
25-
cwd: process.cwd(),
26-
newPath: 'rcs',
27-
flatten: false
28-
};
29-
30-
let globString = pathString;
31-
32-
if (Object.prototype.toString.call(pathString) === '[object Array]') {
33-
globString = `{${ pathString.join(',') }}`;
34-
}
35-
36-
const globArray = glob.sync(globString, {
37-
cwd: options.cwd
38-
});
39-
40-
// call replaceCss if options.collectSelectors is set to true
41-
for (let filePath of globArray) {
42-
let data;
43-
let joinedPath = path.join(options.newPath, filePath);
44-
let shouldOverwrite = options.overwrite;
45-
46-
if (!options.overwrite) {
47-
shouldOverwrite = joinedPath !== path.join(options.cwd, filePath);
48-
}
49-
50-
if (options.collectSelectors) {
51-
data = rcs.replace.fileCssSync(path.join(options.cwd, filePath), options);
52-
} else {
53-
data = rcs.replace.fileSync(path.join(options.cwd, filePath), options);
54-
55-
}
56-
57-
rcs.helper.saveSync(joinedPath, data.data, { overwrite: shouldOverwrite });
58-
}
59-
} // /processSync
60-
61-
/**
62-
* @typedef {Object} processOptions
63-
* @property {Boolean} [collectSelectors=false] if the triggered files are css files
64-
* @property {Boolean} [overwrite=false] overwrite the same file
65-
* @property {String} [cwd=process.cwd()] the current working directory in which to search
66-
* @property {String} [newPath='rcs'] in which folder the new files should go
67-
* @property {Boolean} [flatten=false] flatten the hierarchy - no subfolders
68-
*/
69-
/**
70-
* process over all files - set and replace
71-
*
72-
* @param {pathString} pathString this pathString can be either an expression for `glob` or a filepath
73-
* @param {processOptions} options
74-
* @param {Function} cb the callback
75-
* @return {Function} cb
76-
*/
77-
renameCssSelectors.process = (pathString, options, cb) => {
78-
const optionsDefault = {
79-
collectSelectors: false,
80-
overwrite: false,
81-
cwd: process.cwd(),
82-
newPath: 'rcs',
83-
flatten: false
84-
};
85-
86-
let globString = pathString;
87-
88-
// @todo add flatten files
89-
90-
// set cb if options are not set
91-
if (typeof cb !== 'function') {
92-
cb = options;
93-
options = {};
94-
}
95-
96-
options = _.merge(optionsDefault, options);
97-
98-
if (Object.prototype.toString.call(pathString) === '[object Array]') {
99-
globString = `{${ pathString.join(',') }}`;
100-
}
101-
102-
glob(globString, {
103-
cwd: options.cwd
104-
}, (err, filesArray) => {
105-
if (err) return cb(err);
106-
107-
// fail if nothing is found
108-
if (filesArray.length <= 0) {
109-
return cb({
110-
message: 'No files found',
111-
error: 'ENOENT'
112-
});
113-
}
114-
115-
// call replaceCss if options.collectSelectors is set to true
116-
if (options.collectSelectors) {
117-
// call in series
118-
// not all selectors are stored, maybe some selectors are duplicated in different files
119-
async.eachSeries(filesArray, (filePath, callback) => {
120-
rcs.replace.fileCss(path.join(options.cwd, filePath), options, (err, data) => {
121-
let joinedPath;
122-
let shouldOverwrite = options.overwrite;
123-
124-
if (err) callback(err);
125-
126-
joinedPath = path.join(options.newPath, filePath);
127-
128-
if (!options.overwrite) {
129-
shouldOverwrite = joinedPath !== path.join(options.cwd, filePath);
130-
}
131-
132-
rcs.helper.save(joinedPath, data.data, { overwrite: shouldOverwrite }, (err) => {
133-
if (err) callback(err);
134-
135-
callback();
136-
});
137-
});
138-
}, err => {
139-
if (err) {
140-
cb(err);
141-
} else {
142-
cb(null, true);
143-
}
144-
});
145-
} else {
146-
// can be fired asynchronous
147-
// all selectors are collected
148-
// ⚡️ speed it up with async ⚡️
149-
async.each(filesArray, (filePath, callback) => {
150-
rcs.replace.file(path.join(options.cwd, filePath), (err, data) => {
151-
let joinedPath;
152-
let shouldOverwrite = options.overwrite;
153-
154-
if (err) callback(err);
155-
156-
joinedPath = path.join(options.newPath, filePath);
157-
158-
if (!options.overwrite) {
159-
shouldOverwrite = joinedPath !== path.join(options.cwd, filePath);
160-
}
161-
162-
rcs.helper.save(joinedPath, data.data, { overwrite: shouldOverwrite }, (err) => {
163-
if (err) callback(err);
164-
165-
callback();
166-
});
167-
});
168-
}, err => {
169-
if (err) {
170-
cb(err);
171-
} else {
172-
cb(null, true);
173-
}
174-
});
175-
}
176-
});
177-
}; // /process
178-
179-
/**
180-
* The ansynchronous method for processCss
181-
*/
182-
renameCssSelectors.processCssSync = (pathString, options) => {
183-
options = options || {};
184-
185-
// set the css power for renameCssSelectors.process
186-
options.collectSelectors = true;
187-
188-
renameCssSelectors.processSync(pathString, options);
189-
} // /processCssSync
190-
191-
/**
192-
* process over all css files - set and replace
193-
* does exactly the same as renameCssSelectors.process, but set the collectSelectors option to true
194-
*
195-
* @param {pathString} pathString this pathString can be either an expression for `glob` or a filepath
196-
* @param {processOptions} options
197-
* @param {Function} cb the callback
198-
* @return {Function} cb
199-
*/
200-
renameCssSelectors.processCss = (pathString, options, cb) => {
201-
// set cb if options are not set
202-
if (typeof cb !== 'function') {
203-
cb = options;
204-
options = {};
205-
}
206-
207-
// set the css power for renameCssSelectors.process
208-
options.collectSelectors = true;
209-
210-
renameCssSelectors.process(pathString, options, cb);
211-
} // /processCss
212-
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-
267-
/**
268-
* @typedef {Object} generateMappingOptions
269-
* @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
270-
* @property {Boolean | String} [cssMappingMin=false] like the property cssMapping
271-
* @property {Boolean} [extended=false] defines if metadata should be added to the selector
272-
* @property {Boolean} [json=true] defines if metadata should be added to the selector
273-
* @property {Boolean} [isSelectors=true] if it should write the selector type into the key (# | .)
274-
*/
275-
/**
276-
* generates a file including all old and new selectors/names
277-
* includes also unused class selectors
278-
*
279-
* @param {String} pathString where it should get saved
280-
* @param {generateMappingOptions} [options]
281-
*/
282-
renameCssSelectors.generateMapping = (pathString, options, cb) => {
283-
let fileName = 'renaming_map';
284-
let fileNameExt = '.json';
285-
let mappingName = 'CSS_NAME_MAPPING';
286-
287-
const optionsDefault = {
288-
cssMapping: true,
289-
cssMappingMin: false,
290-
extended: false,
291-
json: true,
292-
origValues: true,
293-
isSelectors: true,
294-
overwrite: false
295-
}
296-
297-
// set cb if options are not set
298-
if (typeof cb !== 'function') {
299-
cb = options;
300-
options = {};
301-
}
302-
303-
options = _.merge(optionsDefault, options);
304-
305-
if (options.cssMappingMin) {
306-
options.origValues = false;
307-
mappingName = 'CSS_NAME_MAPPING_MIN';
308-
fileName = fileName + '_min';
309-
}
310-
311-
if (typeof options.cssMappingMin === 'string') {
312-
mappingName = options.cssMappingMin;
313-
fileName = options.cssMappingMin;
314-
}
315-
316-
if (typeof options.cssMapping === 'string') {
317-
fileName = options.cssMapping;
318-
}
319-
320-
const cssMappingArray = rcs.selectorLibrary.getAll({
321-
extended: options.extended,
322-
origValues: options.origValues,
323-
isSelectors: options.isSelectors
324-
});
325-
326-
let cssMappingJsonString = rcs.helper.objectToJson(cssMappingArray);
327-
let writeData = cssMappingJsonString;
328-
let newPath = path.join(pathString, fileName);
329-
330-
// no json
331-
if (!options.json) {
332-
writeData = `var ${ mappingName } = ${ cssMappingJsonString };`
333-
fileNameExt = '.js';
334-
}
335-
336-
rcs.helper.save(`${ newPath }${ fileNameExt }`, writeData, { overwrite: options.overwrite }, (err, data) => {
337-
if (err) cb(err);
338-
339-
cb(null, data);
340-
});
341-
}; // /generateMapping
342-
343-
/**
344-
* load the mapping file
345-
*
346-
* @param {Object | String} pathString could be either the path or the mapping object
347-
* @param {Object} [options]
348-
*/
349-
renameCssSelectors.loadMapping = (pathString, options) => {
350-
let selectors = pathString;
351-
352-
const optionsDefault = {
353-
origValues: true
354-
}
355-
356-
options = options || {};
357-
options = _.merge(optionsDefault, options);
358-
359-
if (typeof pathString === 'string') {
360-
selectors = json.readToObjSync(pathString, 'utf8');
361-
}
362-
363-
if (!options.origValues) {
364-
let tempSelectors = {};
365-
366-
for (let key in selectors) {
367-
let value = selectors[key];
368-
let modKey = key.slice(1, key.length);
369-
370-
tempSelectors[key.charAt(0) + value] = modKey;
371-
}
372-
373-
selectors = tempSelectors;
374-
}
375-
376-
if (!selectors || typeof selectors !== 'object') {
377-
return;
378-
}
379-
380-
rcs.selectorLibrary.setValues(selectors);
381-
}; // /loadMapping
382-
383-
/**
384-
* includes .rcsrc - if not found it will include "rcs" in package.json
385-
*/
386-
renameCssSelectors.includeConfig = (pathString) => {
387-
let configObject;
388-
389-
pathString = pathString || path.join(process.cwd(), '.rcsrc');
390-
configObject = json.readToObjSync(pathString);
391-
392-
if (!configObject) {
393-
// package.json .rcs if no other config is found
394-
configObject = json.readToObjSync(path.join(process.cwd(), 'package.json')).rcs;
395-
}
396-
397-
if (configObject.exclude) {
398-
rcs.selectorLibrary.setExclude(configObject.exclude);
399-
}
400-
}; // /includeConfig
18+
// PROCESS
19+
renameCssSelectors.processSync = require('./lib/process/processSync');
20+
renameCssSelectors.process = require('./lib/process/process');
21+
renameCssSelectors.processCssSync = require('./lib/processCss/processCssSync');
22+
renameCssSelectors.processCss = require('./lib/processCss/processCss');
23+
24+
// MAPPING
25+
renameCssSelectors.generateMappingSync = require('./lib/mapping/generateSync');
26+
renameCssSelectors.generateMapping = require('./lib/mapping/generate');
27+
renameCssSelectors.loadMapping = require('./lib/mapping/load');
28+
29+
// CONFIG
30+
renameCssSelectors.includeConfig = require('./lib/config/include');

0 commit comments

Comments
 (0)