Skip to content

Update rcs core #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Async:
const rcs = require('rename-css-selectors')

// if you want to include the .rcsrc config
rcs.includeConfig();
rcs.config.load();

// if you have some generated mappings - load them!
// you can also specify the string although it does not exist yet.
Expand Down Expand Up @@ -98,7 +98,7 @@ try {
- [rcs.process.any](docs/api/processAny.md)
- [rcs.generateMapping](docs/api/generateMapping.md)
- [rcs.loadMapping](docs/api/loadMapping.md)
- [rcs.includeConfig](docs/api/includeConfig.md)
- [rcs.config](docs/api/config.md)

# LICENSE

Expand Down
22 changes: 20 additions & 2 deletions docs/api/includeConfig.md → docs/api/config.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# includeConfig
# Config

**includeConfig([pathLocation])**
**Config.load([pathLocation])**

> All available configs [here](#rcs-config)

Expand All @@ -23,6 +23,7 @@ rcs.includeConfig();

- [Example](#example)
- [Exclude](#exclude-classes-and-ids)
- [Ignore](#ignore-files)
- [Include from other projects](#include-renamed-classes-from-other-project)

### Example
Expand Down Expand Up @@ -70,3 +71,20 @@ Let's exclude 4 classes and id's: `js`, `flexbox`, `canvas` and `svg`
]
}
```

### Ignore files

`ignore`

If you need to ignore some file or some file pattern from processing, this is how to do it using minimatch pattern (glob)
Please notice that filepathes are matched absolutely.

```json
{
"ignore": [
"relativeFile.js",
"**/*.min.js",
]
}
```

4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const generateMapping = require('./lib/mapping/generateMapping');
const loadMapping = require('./lib/mapping/loadMapping');

// config
const includeConfig = require('./lib/config/includeConfig');
const config = require('./lib/config/config');

module.exports = {
process: {
Expand All @@ -28,5 +28,5 @@ module.exports = {
generateMappingSync,
generateMapping,
loadMapping,
includeConfig,
config,
};
39 changes: 39 additions & 0 deletions lib/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const rcs = require('rcs-core');
const path = require('path');
const json = require('json-extra');
const minimatch = require('minimatch');

class Config {
constructor() {
this.ignorePatterns = [];
}

load(pathString = path.join(process.cwd(), '.rcsrc')) {
let configObject;

configObject = json.readToObjSync(pathString);

if (!configObject) {
// package.json .rcs if no other config is found
configObject = json.readToObjSync(path.join(process.cwd(), 'package.json')).rcs;
}

if (configObject.exclude) {
rcs.selectorsLibrary.setExclude(configObject.exclude);
}

if (configObject.reserve) {
rcs.selectorsLibrary.setReserved(configObject.reserve);
}

if (configObject.ignore) {
this.ignorePatterns = configObject.ignore;
}
}

isIgnored(filePath) {
return this.ignorePatterns.some(pattern => minimatch(filePath, pattern));
}
}

module.exports = new Config();
20 changes: 0 additions & 20 deletions lib/config/includeConfig.js

This file was deleted.

8 changes: 7 additions & 1 deletion lib/mapping/generateMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ const generateMapping = (pathString, opts, cb) => {
fileName = options.cssMapping;
}

const cssMappingArray = rcs.selectorLibrary.getAll({
const cssClassMappingArray = rcs.selectorsLibrary.getClassSelector().getAll({
extend: options.extended,
getRenamedValues: !options.origValues,
addSelectorType: options.isSelectors,
});
const cssIdMappingArray = rcs.selectorsLibrary.getIdSelector().getAll({
extend: options.extended,
getRenamedValues: !options.origValues,
addSelectorType: options.isSelectors,
});
const cssMappingArray = { class: cssClassMappingArray, id: cssIdMappingArray };

const cssMappingJsonString = json.check(cssMappingArray) ? cssMappingArray : json.stringify(cssMappingArray, null, '\t');
let writeData = cssMappingJsonString;
Expand Down
10 changes: 9 additions & 1 deletion lib/mapping/generateMappingSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ const generateMappingSync = (pathString, options) => {
overwrite: false,
};

// eslint-disable-next-line no-param-reassign
options = merge({}, optionsDefault, options);

if (options.cssMappingMin) {
// eslint-disable-next-line no-param-reassign
options.origValues = false;
mappingName = 'CSS_NAME_MAPPING_MIN';
fileName = `${fileName}_min`;
Expand All @@ -37,11 +39,17 @@ const generateMappingSync = (pathString, options) => {
fileName = options.cssMapping;
}

const cssMappingArray = rcs.selectorLibrary.getAll({
const cssClassMappingArray = rcs.selectorsLibrary.getClassSelector().getAll({
extend: options.extended,
getRenamedValues: !options.origValues,
addSelectorType: options.isSelectors,
});
const cssIdMappingArray = rcs.selectorsLibrary.getIdSelector().getAll({
extend: options.extended,
getRenamedValues: !options.origValues,
addSelectorType: options.isSelectors,
});
const cssMappingArray = { class: cssClassMappingArray, id: cssIdMappingArray };

const cssMappingJsonString = json.check(cssMappingArray) ? cssMappingArray : json.stringify(cssMappingArray, null, '\t');
let writeData = cssMappingJsonString;
Expand Down
27 changes: 14 additions & 13 deletions lib/mapping/loadMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@ const merge = require('lodash.merge');
const rcs = require('rcs-core');
const json = require('json-extra');

const loadMapping = (pathString, options) => {
const loadReversed = selectors =>
Object.entries(selectors).reduce(
(prev, [key, value]) => merge(prev, { [key.charAt(0) + value]: key.slice(1, key.length) })
, {});

const loadMapping = (pathString, options = {}) => {
let selectors = pathString;

const optionsDefault = {
origValues: true,
};

options = options || {};
// eslint-disable-next-line no-param-reassign
options = merge({}, optionsDefault, options);

if (typeof pathString === 'string') {
selectors = json.readToObjSync(pathString, 'utf8');
}

if (!options.origValues) {
const tempSelectors = {};

Object.keys(selectors).forEach((key) => {
const value = selectors[key];
const modKey = key.slice(1, key.length);
let classes = selectors.class || {};
let ids = selectors.id || {};

tempSelectors[key.charAt(0) + value] = modKey;
});

selectors = tempSelectors;
if (!options.origValues) {
classes = loadReversed(classes);
ids = loadReversed(ids);
}

if (!selectors || typeof selectors !== 'object') {
return;
}

rcs.selectorLibrary.setMultiple(selectors);
rcs.selectorsLibrary.getClassSelector().setMultiple(classes);
rcs.selectorsLibrary.getIdSelector().setMultiple(ids);
}; // /loadMapping

module.exports = loadMapping;
2 changes: 2 additions & 0 deletions lib/process/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const assert = require('assert');
const save = require('../helper/save');
const replaceData = require('./replaceData');
const { fileExt, availableTypes, optionsDefault } = require('./defaults');
const config = require('../config/config');

const rcsProcess = (pathString, opts, cb) => {
let globString = pathString;
Expand Down Expand Up @@ -37,6 +38,7 @@ const rcsProcess = (pathString, opts, cb) => {

glob(globString, {
cwd: options.cwd,
ignore: config.ignorePatterns,
}, (errGlob, filesArray) => {
if (errGlob) {
return callback(errGlob);
Expand Down
3 changes: 2 additions & 1 deletion lib/process/processSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const assert = require('assert');
const saveSync = require('../helper/saveSync');
const replaceData = require('./replaceData');
const { fileExt, availableTypes, optionsDefault } = require('./defaults');
const config = require('../config/config');

/**
* The synchronous method for process
Expand All @@ -25,7 +26,7 @@ const processSync = (pathString, opts) => {
globString = `{${pathString.join(',')}}`;
}

const globArray = glob.sync(globString, { cwd: options.cwd });
const globArray = glob.sync(globString, { cwd: options.cwd, ignore: config.ignorePatterns });
const cssHtmlFiles = globArray.filter(file => (
fileExt.css.includes(path.extname(file))
|| fileExt.html.includes(path.extname(file))
Expand Down
13 changes: 8 additions & 5 deletions lib/process/replaceData.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,47 @@
const merge = require('lodash.merge');
const rcs = require('rcs-core');
const path = require('path');
const { fileExt } = require('./defaults');

const replaceData = (filePath, fileData, options) => {
let data;

const sourceFile = { sourceFile: filePath };
if (
options.type === 'js' ||
(
options.type === 'auto' &&
fileExt.js.includes(path.extname(filePath))
)
) {
data = rcs.replace.js(fileData, options.parserOptions);
const jsxType = { ecmaFeatures: { jsx: path.extname(filePath).match(/jsx/i) !== null } };
data = rcs.replace.js(fileData, merge(jsxType, options.parserOptions, sourceFile));
} else if (
options.type === 'css' ||
(
options.type === 'auto' &&
fileExt.css.includes(path.extname(filePath))
)
) {
data = rcs.replace.css(fileData);
data = rcs.replace.css(fileData, sourceFile);
} else if (
options.type === 'html' ||
(
options.type === 'auto' &&
fileExt.html.includes(path.extname(filePath))
)
) {
data = rcs.replace.html(fileData);
data = rcs.replace.html(fileData, sourceFile);
} else if (
options.type === 'pug' ||
(
options.type === 'auto' &&
fileExt.pug.includes(path.extname(filePath))
)
) {
data = rcs.replace.pug(fileData);
data = rcs.replace.pug(fileData, sourceFile);
} else {
data = rcs.replace.any(fileData);
data = rcs.replace.any(fileData, sourceFile);
}

return data;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"glob": "^7.1.1",
"json-extra": "^0.5.0",
"lodash.merge": "^4.6.1",
"rcs-core": "^2.6.2",
"minimatch": "^3.0.4",
"rcs-core": "^3.0.0-alpha.0",
"universalify": "^0.1.2"
},
"devDependencies": {
Expand Down
Loading