From 368693cd1a63051b3b8cbe3950432403157302b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Peer=20St=C3=B6cklmair?= Date: Tue, 28 Aug 2018 09:21:33 +0200 Subject: [PATCH] Feat: rcs.process.pug (closes #22) (#29) (ref: #22) --- README.md | 1 + docs/api/processPug.md | 47 ++++++++++ index.js | 2 + lib/process/defaults.js | 3 +- lib/process/replaceData.js | 8 ++ package.json | 2 +- test/files/fixtures/pug/index.pug | 6 ++ test/files/results/pug/index.pug | 6 ++ test/processPug.js | 39 +++++++++ test/processPugSync.js | 36 ++++++++ yarn.lock | 140 +++++++++++++++++++++++++++++- 11 files changed, 284 insertions(+), 6 deletions(-) create mode 100644 docs/api/processPug.md create mode 100644 test/files/fixtures/pug/index.pug create mode 100644 test/files/results/pug/index.pug create mode 100644 test/processPug.js create mode 100644 test/processPugSync.js diff --git a/README.md b/README.md index e4c12fa..2572348 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/docs/api/processPug.md b/docs/api/processPug.md new file mode 100644 index 0000000..2954160 --- /dev/null +++ b/docs/api/processPug.md @@ -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 `` +- options `` *optional* +- callback `` *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); + } +})(); +``` diff --git a/index.js b/index.js index 261c0d5..af38807 100644 --- a/index.js +++ b/index.js @@ -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'), diff --git a/lib/process/defaults.js b/lib/process/defaults.js index 06042b0..02fe744 100644 --- a/lib/process/defaults.js +++ b/lib/process/defaults.js @@ -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', diff --git a/lib/process/replaceData.js b/lib/process/replaceData.js index 9cefbd3..3f3a90d 100644 --- a/lib/process/replaceData.js +++ b/lib/process/replaceData.js @@ -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); } diff --git a/package.json b/package.json index e6ff467..d60c69a 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/files/fixtures/pug/index.pug b/test/files/fixtures/pug/index.pug new file mode 100644 index 0000000..fcbc379 --- /dev/null +++ b/test/files/fixtures/pug/index.pug @@ -0,0 +1,6 @@ +doctype html +head + meta(charset='UTF-8') + title Test Document +.jp-block +.jp-block__element diff --git a/test/files/results/pug/index.pug b/test/files/results/pug/index.pug new file mode 100644 index 0000000..25553b1 --- /dev/null +++ b/test/files/results/pug/index.pug @@ -0,0 +1,6 @@ +doctype html +head + meta(charset!='UTF-8') + title Test Document +.a +.b diff --git a/test/processPug.js b/test/processPug.js new file mode 100644 index 0000000..3b38088 --- /dev/null +++ b/test/processPug.js @@ -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(); + }); +}); diff --git a/test/processPugSync.js b/test/processPugSync.js new file mode 100644 index 0000000..0a61c1d --- /dev/null +++ b/test/processPugSync.js @@ -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()); +}); diff --git a/yarn.lock b/yarn.lock index 65874f9..6a242cc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -133,20 +133,40 @@ pretty-ms "^0.2.1" text-table "^0.2.0" +"@types/babel-types@*", "@types/babel-types@^7.0.0": + version "7.0.4" + resolved "https://registry.yarnpkg.com/@types/babel-types/-/babel-types-7.0.4.tgz#bfd5b0d0d1ba13e351dff65b6e52783b816826c8" + +"@types/babylon@^6.16.2": + version "6.16.3" + resolved "https://registry.yarnpkg.com/@types/babylon/-/babylon-6.16.3.tgz#c2937813a89fcb5e79a00062fc4a8b143e7237bb" + dependencies: + "@types/babel-types" "*" + abbrev@1: version "1.0.9" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135" +acorn-globals@^3.0.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-3.1.0.tgz#fd8270f71fbb4996b004fa880ee5d46573a731bf" + dependencies: + acorn "^4.0.4" + acorn-jsx@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/acorn-jsx/-/acorn-jsx-3.0.1.tgz#afdf9488fb1ecefc8348f6fb22f464e32a58b36b" dependencies: acorn "^3.0.4" -acorn@^3.0.4: +acorn@^3.0.4, acorn@^3.1.0: version "3.3.0" resolved "https://registry.yarnpkg.com/acorn/-/acorn-3.3.0.tgz#45e37fb39e8da3f25baee3ff5369e2bb5f22017a" +acorn@^4.0.4, acorn@~4.0.2: + version "4.0.13" + resolved "https://registry.yarnpkg.com/acorn/-/acorn-4.0.13.tgz#105495ae5361d697bd195c825192e1ad7f253787" + acorn@^5.0.1: version "5.0.3" resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.0.3.tgz#c460df08491463f028ccb82eab3730bf01087b3d" @@ -1245,6 +1265,12 @@ chalk@^2.3.0: escape-string-regexp "^1.0.5" supports-color "^5.2.0" +character-parser@^2.1.1: + version "2.2.0" + resolved "https://registry.yarnpkg.com/character-parser/-/character-parser-2.2.0.tgz#c7ce28f36d4bcd9744e5ffc2c5fcde1c73261fc0" + dependencies: + is-regex "^1.0.3" + chokidar@^1.4.2, chokidar@^1.6.1: version "1.7.0" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.7.0.tgz#798e689778151c8076b4b360e5edd28cda2bb468" @@ -1450,6 +1476,15 @@ console-control-strings@^1.0.0, console-control-strings@~1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/console-control-strings/-/console-control-strings-1.1.0.tgz#3d7cf4464db6446ea644bf4b39507f9851008e8e" +constantinople@^3.0.1, constantinople@^3.0.2: + version "3.1.2" + resolved "https://registry.yarnpkg.com/constantinople/-/constantinople-3.1.2.tgz#d45ed724f57d3d10500017a7d3a889c1381ae647" + dependencies: + "@types/babel-types" "^7.0.0" + "@types/babylon" "^6.16.2" + babel-types "^6.26.0" + babylon "^6.18.0" + contains-path@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/contains-path/-/contains-path-0.1.0.tgz#fe8cf184ff6670b6baef01a9d4861a5cbec4120a" @@ -1670,6 +1705,10 @@ doctrine@^2.0.0: esutils "^2.0.2" isarray "^1.0.0" +doctypes@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/doctypes/-/doctypes-1.1.0.tgz#ea80b106a87538774e8a3a4a5afe293de489e0a9" + dot-prop@^4.1.0: version "4.2.0" resolved "https://registry.yarnpkg.com/dot-prop/-/dot-prop-4.2.0.tgz#1f19e0c2e1aa0e32797c49799f2837ac6af69c57" @@ -2616,6 +2655,13 @@ is-error@^2.2.0: version "2.2.1" resolved "https://registry.yarnpkg.com/is-error/-/is-error-2.2.1.tgz#684a96d84076577c98f4cdb40c6d26a5123bf19c" +is-expression@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/is-expression/-/is-expression-3.0.0.tgz#39acaa6be7fd1f3471dc42c7416e61c24317ac9f" + dependencies: + acorn "~4.0.2" + object-assign "^4.0.1" + is-extendable@^0.1.0, is-extendable@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/is-extendable/-/is-extendable-0.1.1.tgz#62b110e289a471418e3ec36a617d472e301dfc89" @@ -2875,6 +2921,10 @@ js-string-escape@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/js-string-escape/-/js-string-escape-1.0.1.tgz#e2625badbc0d67c7533e9edc1068c587ae4137ef" +js-stringify@^1.0.1: + version "1.0.2" + resolved "https://registry.yarnpkg.com/js-stringify/-/js-stringify-1.0.2.tgz#1736fddfd9724f28a3682adc6230ae7e4e9679db" + js-tokens@^3.0.0, js-tokens@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" @@ -3864,6 +3914,67 @@ pseudomap@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3" +pug-attrs@^2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/pug-attrs/-/pug-attrs-2.0.3.tgz#a3095f970e64151f7bdad957eef55fb5d7905d15" + dependencies: + constantinople "^3.0.1" + js-stringify "^1.0.1" + pug-runtime "^2.0.4" + +pug-code-gen@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/pug-code-gen/-/pug-code-gen-2.0.1.tgz#0951ec83225d74d8cfc476a7f99a259b5f7d050c" + dependencies: + constantinople "^3.0.1" + doctypes "^1.1.0" + js-stringify "^1.0.1" + pug-attrs "^2.0.3" + pug-error "^1.3.2" + pug-runtime "^2.0.4" + void-elements "^2.0.1" + with "^5.0.0" + +pug-error@^1.3.2: + version "1.3.2" + resolved "https://registry.yarnpkg.com/pug-error/-/pug-error-1.3.2.tgz#53ae7d9d29bb03cf564493a026109f54c47f5f26" + +pug-lexer@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/pug-lexer/-/pug-lexer-4.0.0.tgz#210c18457ef2e1760242740c5e647bd794cec278" + dependencies: + character-parser "^2.1.1" + is-expression "^3.0.0" + pug-error "^1.3.2" + +pug-parser@^5.0.0: + version "5.0.0" + resolved "https://registry.yarnpkg.com/pug-parser/-/pug-parser-5.0.0.tgz#e394ad9b3fca93123940aff885c06e44ab7e68e4" + dependencies: + pug-error "^1.3.2" + token-stream "0.0.1" + +pug-runtime@^2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/pug-runtime/-/pug-runtime-2.0.4.tgz#e178e1bda68ab2e8c0acfc9bced2c54fd88ceb58" + +pug-source-gen@^0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/pug-source-gen/-/pug-source-gen-0.0.2.tgz#76d7753d89693c6846578c80da1628b8c6fc6fdb" + dependencies: + constantinople "^3.0.2" + object-assign "^4.0.1" + pug-walk "0.0.3" + repeat-string "^1.5.2" + +pug-walk@0.0.3: + version "0.0.3" + resolved "https://registry.yarnpkg.com/pug-walk/-/pug-walk-0.0.3.tgz#c28be7bcc540f24b83d274472410827c84e31ac6" + +pug-walk@^1.1.7: + version "1.1.7" + resolved "https://registry.yarnpkg.com/pug-walk/-/pug-walk-1.1.7.tgz#c00d5c5128bac5806bec15d2b7e7cdabe42531f3" + punycode@^1.4.1: version "1.4.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e" @@ -3889,9 +4000,9 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.2.7: minimist "^1.2.0" strip-json-comments "~2.0.1" -rcs-core@^2.3.1: - version "2.3.1" - resolved "https://registry.yarnpkg.com/rcs-core/-/rcs-core-2.3.1.tgz#c9f6baf5bf41058246002d08918aafebe44a0fa4" +rcs-core@^2.4.0: + version "2.4.0" + resolved "https://registry.yarnpkg.com/rcs-core/-/rcs-core-2.4.0.tgz#760ff7833989985ddddd6c85aa866e0fb5b49eb5" dependencies: array-includes "^3.0.2" ast-traverse "^0.1.1" @@ -3905,6 +4016,12 @@ rcs-core@^2.3.1: parse5 "^5.0.0" parse5-traverse "^1.0.3" postcss "^6.0.17" + pug-code-gen "^2.0.1" + pug-lexer "^4.0.0" + pug-parser "^5.0.0" + pug-runtime "^2.0.4" + pug-source-gen "^0.0.2" + pug-walk "^1.1.7" recast "^0.15.0" read-pkg-up@^1.0.1: @@ -4682,6 +4799,10 @@ to-regex@^3.0.1, to-regex@^3.0.2: regex-not "^1.0.2" safe-regex "^1.1.0" +token-stream@0.0.1: + version "0.0.1" + resolved "https://registry.yarnpkg.com/token-stream/-/token-stream-0.0.1.tgz#ceeefc717a76c4316f126d0b9dbaa55d7e7df01a" + tough-cookie@~2.3.0: version "2.3.2" resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.2.tgz#f081f76e4c85720e6c37a5faced737150d84072a" @@ -4844,6 +4965,10 @@ verror@1.3.6: dependencies: extsprintf "1.0.2" +void-elements@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/void-elements/-/void-elements-2.0.1.tgz#c066afb582bb1cb4128d60ea92392e94d5e9dbec" + well-known-symbols@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/well-known-symbols/-/well-known-symbols-1.0.0.tgz#73c78ae81a7726a8fa598e2880801c8b16225518" @@ -4874,6 +4999,13 @@ window-size@0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d" +with@^5.0.0: + version "5.1.1" + resolved "https://registry.yarnpkg.com/with/-/with-5.1.1.tgz#fa4daa92daf32c4ea94ed453c81f04686b575dfe" + dependencies: + acorn "^3.1.0" + acorn-globals "^3.0.0" + wordwrap@0.0.2: version "0.0.2" resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"