-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
process.js
133 lines (110 loc) · 3.69 KB
/
process.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const { fromCallback } = require('universalify');
const merge = require('lodash.merge');
const fs = require('fs');
const rcs = require('rcs-core');
const path = require('path');
const glob = require('glob');
const async = require('async');
const assert = require('assert');
const save = require('../helper/save');
const replaceData = require('./replaceData');
const { fileExt, availableTypes, optionsDefault } = require('./defaults');
const rcsProcess = (pathString, opts, cb) => {
let globString = pathString;
let options = opts;
let callback = cb;
// set callback if options are not set
if (typeof callback !== 'function') {
callback = options;
options = {};
}
options = merge({}, optionsDefault, options);
assert(
availableTypes.includes(options.type),
`options.type must be one of the following: ${availableTypes}`,
);
if (Array.isArray(pathString)) {
globString = pathString.length > 1
? `{${pathString.join(',')}}`
: pathString[0];
}
glob(globString, {
cwd: options.cwd,
}, (errGlob, filesArray) => {
if (errGlob) {
return callback(errGlob);
}
// fail if nothing is found
if (filesArray.length <= 0) {
return callback({
message: 'No files found',
error: 'ENOENT',
});
}
// sort in case of 'auto'
const cssFiles = filesArray.filter(file => fileExt.css.includes(path.extname(file)));
const fillLibraryFiles = options.type === 'auto'
? cssFiles
: filesArray;
// call in series
// not all selectors are stored, maybe some selectors are duplicated in different files
return async.eachSeries(fillLibraryFiles, (filePath, asyncCb) => {
// skip if it is not meant to fill the library
if (options.type !== 'auto' && options.type !== 'css') {
return asyncCb();
}
return fs.readFile(path.join(options.cwd, filePath), (errReadFile, bufferData) => {
if (errReadFile) {
return asyncCb(errReadFile);
}
const availableOptions = {
prefix: options.prefix,
suffix: options.suffix,
replaceKeyframes: options.replaceKeyframes,
preventRandomName: options.preventRandomName,
ignoreAttributeSelectors: options.ignoreAttributeSelectors,
};
rcs.fillLibraries(bufferData.toString(), availableOptions);
return asyncCb();
});
}, (errEachSeries) => {
if (errEachSeries) {
return callback(errEachSeries);
}
// now all files can be renamed and saved
// can be fired parallel
// all selectors are collected
// ⚡️ speed it up with async.each ⚡️
return async.each(filesArray, (filePath, asyncEachCb) => {
fs.readFile(path.join(options.cwd, filePath), 'utf8', (err, bufferData) => {
let data;
let shouldOverwrite = options.overwrite;
if (err) {
return asyncEachCb(err);
}
try {
data = replaceData(filePath, bufferData, options);
} catch (e) {
return asyncEachCb(e);
}
const joinedPath = path.join(options.newPath, filePath);
if (!options.overwrite) {
shouldOverwrite = joinedPath !== path.join(options.cwd, filePath);
}
return save(joinedPath, data, { overwrite: shouldOverwrite }, (errSave) => {
if (errSave) {
return asyncEachCb(errSave);
}
return asyncEachCb(null);
});
});
}, (errProcess) => {
if (errProcess) {
return callback(errProcess);
}
return callback(null);
});
});
});
}; // /process
module.exports = fromCallback(rcsProcess);