Skip to content

Commit

Permalink
Feat: rcs.process.pug (closes #22) (#29)
Browse files Browse the repository at this point in the history
(ref: #22)
  • Loading branch information
JPeer264 authored Aug 28, 2018
1 parent b1347de commit 368693c
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ try {
- [rcs.process.css](docs/api/processCss.md)
- [rcs.process.js](docs/api/processJs.md)
- [rcs.process.html](docs/api/processHtml.md)
- [rcs.process.pug](docs/api/processPug.md)
- [rcs.process.any](docs/api/processAny.md)
- [rcs.generateMapping](docs/api/generateMapping.md)
- [rcs.loadMapping](docs/api/loadMapping.md)
Expand Down
47 changes: 47 additions & 0 deletions docs/api/processPug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# rcs.process.pug

**rcs.process.pug(src[, options][, callback])**

> **Important!** process.css should run first, otherwise there are no minified selectors
Sync: `process.pugSync`

Parameters:
- src `<String | Array>`
- options `<Object>` *optional*
- callback `<Function>` *optional*

Options:

- *all options of [rcs.process.html](processHtml.md)*

Example:

```js
const rcs = require('rename-css-selectors');

// callback
rcs.process.pug('**/*.pug', options, (err) => {
if (err) {
return console.error(err);
}

console.log('Successfully wrote new pug files');
});

// promise
rcs.process.pug('**/*.pug', options)
.then(() => console.log('Successfully wrote new pug files'))
.catch(console.error);

// async/await
(async () => {
try {
await rcs.process.pug('**/*.pug', options);

console.log('Successfully wrote new pug files');
} catch (err) {
console.error(err);
}
})();
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = {
js: typeChooser('js'),
htmlSync: typeChooserSync('html'),
html: typeChooser('html'),
pugSync: typeChooserSync('pug'),
pug: typeChooser('pug'),
anySync: typeChooserSync('any'),
any: typeChooser('any'),
autoSync: typeChooserSync('auto'),
Expand Down
3 changes: 2 additions & 1 deletion lib/process/defaults.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module.exports = {
availableTypes: ['auto', 'js', 'html', 'css', 'any'],
availableTypes: ['auto', 'js', 'html', 'pug', 'css', 'any'],
fileExt: {
js: ['.js', '.jsx'],
css: ['.css', '.scss', '.sass', '.less'],
html: ['.html', '.htm'],
pug: ['.pug'],
},
optionsDefault: {
type: 'auto',
Expand Down
8 changes: 8 additions & 0 deletions lib/process/replaceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const replaceData = (filePath, fileData, options) => {
)
) {
data = rcs.replace.html(fileData);
} else if (
options.type === 'pug' ||
(
options.type === 'auto' &&
fileExt.pug.includes(path.extname(filePath))
)
) {
data = rcs.replace.pug(fileData);
} else {
data = rcs.replace.any(fileData);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"glob": "^7.1.1",
"json-extra": "^0.5.0",
"lodash.merge": "^4.6.1",
"rcs-core": "^2.3.1",
"rcs-core": "^2.4.0",
"universalify": "^0.1.2"
},
"devDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions test/files/fixtures/pug/index.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
doctype html
head
meta(charset='UTF-8')
title Test Document
.jp-block
.jp-block__element
6 changes: 6 additions & 0 deletions test/files/results/pug/index.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
doctype html
head
meta(charset!='UTF-8')
title Test Document
.a
.b
39 changes: 39 additions & 0 deletions test/processPug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava';
import path from 'path';
import fs from 'fs-extra';
import rcsCore from 'rcs-core';

import rcs from '../';

const testCwd = 'test/files/testCache';
const fixturesCwd = 'test/files/fixtures';
const resultsCwd = 'test/files/results';


test.before(() => {
rcsCore.nameGenerator.setAlphabet('#abcdefghijklmnopqrstuvwxyz');
rcsCore.nameGenerator.reset();
rcsCore.selectorLibrary.reset();
rcsCore.keyframesLibrary.reset();

rcsCore.selectorLibrary.fillLibrary(fs.readFileSync(path.join(fixturesCwd, '/css/style.css'), 'utf8'));
});

test.afterEach(() => {
fs.removeSync(testCwd);
});

test.cb('should process pug files', (t) => {
rcs.process.pug('pug/index.pug', {
newPath: testCwd,
cwd: fixturesCwd,
}, (err) => {
const newFile = fs.readFileSync(path.join(testCwd, '/pug/index.pug'), 'utf8');
const expectedFile = fs.readFileSync(path.join(resultsCwd, '/pug/index.pug'), 'utf8');

t.falsy(err);
t.is(newFile.trim(), expectedFile.trim());

t.end();
});
});
36 changes: 36 additions & 0 deletions test/processPugSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import test from 'ava';
import path from 'path';
import fs from 'fs-extra';
import rcsCore from 'rcs-core';

import rcs from '../';

const testCwd = 'test/files/testCache';
const fixturesCwd = 'test/files/fixtures';
const resultsCwd = 'test/files/results';


test.before(() => {
rcsCore.nameGenerator.setAlphabet('#abcdefghijklmnopqrstuvwxyz');
rcsCore.nameGenerator.reset();
rcsCore.selectorLibrary.reset();
rcsCore.keyframesLibrary.reset();

rcsCore.selectorLibrary.fillLibrary(fs.readFileSync(path.join(fixturesCwd, '/css/style.css'), 'utf8'));
});

test.afterEach(() => {
fs.removeSync(testCwd);
});

test('should process pug files', (t) => {
rcs.process.pugSync('pug/index.pug', {
newPath: testCwd,
cwd: fixturesCwd,
});

const newFile = fs.readFileSync(path.join(testCwd, '/pug/index.pug'), 'utf8');
const expectedFile = fs.readFileSync(path.join(resultsCwd, '/pug/index.pug'), 'utf8');

t.is(newFile.trim(), expectedFile.trim());
});
Loading

0 comments on commit 368693c

Please sign in to comment.