Skip to content

Commit 368693c

Browse files
authored
Feat: rcs.process.pug (closes #22) (#29)
(ref: #22)
1 parent b1347de commit 368693c

File tree

11 files changed

+284
-6
lines changed

11 files changed

+284
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ try {
9191
- [rcs.process.css](docs/api/processCss.md)
9292
- [rcs.process.js](docs/api/processJs.md)
9393
- [rcs.process.html](docs/api/processHtml.md)
94+
- [rcs.process.pug](docs/api/processPug.md)
9495
- [rcs.process.any](docs/api/processAny.md)
9596
- [rcs.generateMapping](docs/api/generateMapping.md)
9697
- [rcs.loadMapping](docs/api/loadMapping.md)

docs/api/processPug.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# rcs.process.pug
2+
3+
**rcs.process.pug(src[, options][, callback])**
4+
5+
> **Important!** process.css should run first, otherwise there are no minified selectors
6+
7+
Sync: `process.pugSync`
8+
9+
Parameters:
10+
- src `<String | Array>`
11+
- options `<Object>` *optional*
12+
- callback `<Function>` *optional*
13+
14+
Options:
15+
16+
- *all options of [rcs.process.html](processHtml.md)*
17+
18+
Example:
19+
20+
```js
21+
const rcs = require('rename-css-selectors');
22+
23+
// callback
24+
rcs.process.pug('**/*.pug', options, (err) => {
25+
if (err) {
26+
return console.error(err);
27+
}
28+
29+
console.log('Successfully wrote new pug files');
30+
});
31+
32+
// promise
33+
rcs.process.pug('**/*.pug', options)
34+
.then(() => console.log('Successfully wrote new pug files'))
35+
.catch(console.error);
36+
37+
// async/await
38+
(async () => {
39+
try {
40+
await rcs.process.pug('**/*.pug', options);
41+
42+
console.log('Successfully wrote new pug files');
43+
} catch (err) {
44+
console.error(err);
45+
}
46+
})();
47+
```

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module.exports = {
1818
js: typeChooser('js'),
1919
htmlSync: typeChooserSync('html'),
2020
html: typeChooser('html'),
21+
pugSync: typeChooserSync('pug'),
22+
pug: typeChooser('pug'),
2123
anySync: typeChooserSync('any'),
2224
any: typeChooser('any'),
2325
autoSync: typeChooserSync('auto'),

lib/process/defaults.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module.exports = {
2-
availableTypes: ['auto', 'js', 'html', 'css', 'any'],
2+
availableTypes: ['auto', 'js', 'html', 'pug', 'css', 'any'],
33
fileExt: {
44
js: ['.js', '.jsx'],
55
css: ['.css', '.scss', '.sass', '.less'],
66
html: ['.html', '.htm'],
7+
pug: ['.pug'],
78
},
89
optionsDefault: {
910
type: 'auto',

lib/process/replaceData.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ const replaceData = (filePath, fileData, options) => {
2929
)
3030
) {
3131
data = rcs.replace.html(fileData);
32+
} else if (
33+
options.type === 'pug' ||
34+
(
35+
options.type === 'auto' &&
36+
fileExt.pug.includes(path.extname(filePath))
37+
)
38+
) {
39+
data = rcs.replace.pug(fileData);
3240
} else {
3341
data = rcs.replace.any(fileData);
3442
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"glob": "^7.1.1",
4949
"json-extra": "^0.5.0",
5050
"lodash.merge": "^4.6.1",
51-
"rcs-core": "^2.3.1",
51+
"rcs-core": "^2.4.0",
5252
"universalify": "^0.1.2"
5353
},
5454
"devDependencies": {

test/files/fixtures/pug/index.pug

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
doctype html
2+
head
3+
meta(charset='UTF-8')
4+
title Test Document
5+
.jp-block
6+
.jp-block__element

test/files/results/pug/index.pug

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
doctype html
2+
head
3+
meta(charset!='UTF-8')
4+
title Test Document
5+
.a
6+
.b

test/processPug.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import test from 'ava';
2+
import path from 'path';
3+
import fs from 'fs-extra';
4+
import rcsCore from 'rcs-core';
5+
6+
import rcs from '../';
7+
8+
const testCwd = 'test/files/testCache';
9+
const fixturesCwd = 'test/files/fixtures';
10+
const resultsCwd = 'test/files/results';
11+
12+
13+
test.before(() => {
14+
rcsCore.nameGenerator.setAlphabet('#abcdefghijklmnopqrstuvwxyz');
15+
rcsCore.nameGenerator.reset();
16+
rcsCore.selectorLibrary.reset();
17+
rcsCore.keyframesLibrary.reset();
18+
19+
rcsCore.selectorLibrary.fillLibrary(fs.readFileSync(path.join(fixturesCwd, '/css/style.css'), 'utf8'));
20+
});
21+
22+
test.afterEach(() => {
23+
fs.removeSync(testCwd);
24+
});
25+
26+
test.cb('should process pug files', (t) => {
27+
rcs.process.pug('pug/index.pug', {
28+
newPath: testCwd,
29+
cwd: fixturesCwd,
30+
}, (err) => {
31+
const newFile = fs.readFileSync(path.join(testCwd, '/pug/index.pug'), 'utf8');
32+
const expectedFile = fs.readFileSync(path.join(resultsCwd, '/pug/index.pug'), 'utf8');
33+
34+
t.falsy(err);
35+
t.is(newFile.trim(), expectedFile.trim());
36+
37+
t.end();
38+
});
39+
});

test/processPugSync.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import test from 'ava';
2+
import path from 'path';
3+
import fs from 'fs-extra';
4+
import rcsCore from 'rcs-core';
5+
6+
import rcs from '../';
7+
8+
const testCwd = 'test/files/testCache';
9+
const fixturesCwd = 'test/files/fixtures';
10+
const resultsCwd = 'test/files/results';
11+
12+
13+
test.before(() => {
14+
rcsCore.nameGenerator.setAlphabet('#abcdefghijklmnopqrstuvwxyz');
15+
rcsCore.nameGenerator.reset();
16+
rcsCore.selectorLibrary.reset();
17+
rcsCore.keyframesLibrary.reset();
18+
19+
rcsCore.selectorLibrary.fillLibrary(fs.readFileSync(path.join(fixturesCwd, '/css/style.css'), 'utf8'));
20+
});
21+
22+
test.afterEach(() => {
23+
fs.removeSync(testCwd);
24+
});
25+
26+
test('should process pug files', (t) => {
27+
rcs.process.pugSync('pug/index.pug', {
28+
newPath: testCwd,
29+
cwd: fixturesCwd,
30+
});
31+
32+
const newFile = fs.readFileSync(path.join(testCwd, '/pug/index.pug'), 'utf8');
33+
const expectedFile = fs.readFileSync(path.join(resultsCwd, '/pug/index.pug'), 'utf8');
34+
35+
t.is(newFile.trim(), expectedFile.trim());
36+
});

0 commit comments

Comments
 (0)