Skip to content

Commit 8d52c17

Browse files
committed
add processCssSync and processSync
1 parent e6085a5 commit 8d52c17

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,49 @@ 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+
1861
/**
1962
* @typedef {Object} processOptions
2063
* @property {Boolean} [collectSelectors=false] if the triggered files are css files
@@ -133,6 +176,18 @@ renameCssSelectors.process = (pathString, options, cb) => {
133176
});
134177
}; // /process
135178

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+
136191
/**
137192
* process over all css files - set and replace
138193
* does exactly the same as renameCssSelectors.process, but set the collectSelectors option to true

test/index.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ describe('app.js', () => {
4848
});
4949
});
5050

51+
it('should process synchronously css files', () => {
52+
app.processCssSync('**/style*.css', {
53+
newPath: testCwd,
54+
cwd: fixturesCwd
55+
});
56+
57+
let newFile = fs.readFileSync(testCwd + '/style.css', 'utf8');
58+
let newFile2 = fs.readFileSync(testCwd + '/style2.css', 'utf8');
59+
let newFile3 = fs.readFileSync(testCwd + '/subdirectory/style.css', 'utf8');
60+
let expectedFile = fs.readFileSync(resultsCwd + '/style.css', 'utf8');
61+
let expectedFile2 = fs.readFileSync(resultsCwd + '/style2.css', 'utf8');
62+
let expectedFile3 = fs.readFileSync(resultsCwd + '/subdirectory/style.css', 'utf8');
63+
64+
expect(newFile).to.equal(expectedFile);
65+
expect(newFile2).to.equal(expectedFile2);
66+
expect(newFile3).to.equal(expectedFile3);
67+
});
68+
5169
it('should process css files as arrays', done => {
5270
app.process(['**/style.css', '**/style2.css'], {
5371
collectSelectors: true,
@@ -70,6 +88,24 @@ describe('app.js', () => {
7088
});
7189
});
7290

91+
it('should process synchornously css files as arrays', () => {
92+
app.processCssSync(['**/style.css', '**/style2.css'], {
93+
newPath: testCwd,
94+
cwd: fixturesCwd
95+
})
96+
97+
let newFile = fs.readFileSync(testCwd + '/style.css', 'utf8');
98+
let newFile2 = fs.readFileSync(testCwd + '/style2.css', 'utf8');
99+
let newFile3 = fs.readFileSync(testCwd + '/subdirectory/style.css', 'utf8');
100+
let expectedFile = fs.readFileSync(resultsCwd + '/style.css', 'utf8');
101+
let expectedFile2 = fs.readFileSync(resultsCwd + '/style2.css', 'utf8');
102+
let expectedFile3 = fs.readFileSync(resultsCwd + '/subdirectory/style.css', 'utf8');
103+
104+
expect(newFile).to.equal(expectedFile);
105+
expect(newFile2).to.equal(expectedFile2);
106+
expect(newFile3).to.equal(expectedFile3);
107+
});
108+
73109
// duplicated code from the test before
74110
// but another function - especially for css
75111
it('should process css files and prefix them', done => {
@@ -215,6 +251,31 @@ describe('app.js', () => {
215251
});
216252
});
217253

254+
it('should process all files synchronously', () => {
255+
let newFile;
256+
let expectedFile;
257+
258+
app.processCssSync('style.css', {
259+
newPath: testCwd,
260+
cwd: fixturesCwd
261+
});
262+
263+
app.processSync('**/*.txt', {
264+
newPath: testCwd,
265+
cwd: fixturesCwd
266+
});
267+
268+
newFile = fs.readFileSync(testCwd + '/main.txt', 'utf8');
269+
expectedFile = fs.readFileSync(resultsCwd + '/main.txt', 'utf8');
270+
271+
expect(newFile).to.equal(expectedFile);
272+
273+
newFile = fs.readFileSync(testCwd + '/style.css', 'utf8');
274+
expectedFile = fs.readFileSync(resultsCwd + '/style.css', 'utf8');
275+
276+
expect(newFile).to.equal(expectedFile);
277+
});
278+
218279
it('should not overwrite original files', done => {
219280
app.process(['**/style.css', '**/style2.css'], {
220281
collectSelectors: true,

0 commit comments

Comments
 (0)