Skip to content

Commit

Permalink
Update bem-naming to support react naming scheme (close #30). (#39)
Browse files Browse the repository at this point in the history
Update bem-fs-scheme (close #31).
  • Loading branch information
veged authored and tadatuta committed Mar 25, 2017
1 parent a696627 commit 9889c68
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 33 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/tmp/
12 changes: 8 additions & 4 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,10 @@ module.exports = {

// Смотри https://ru.bem.info/toolbox/sdk/bem-naming/#Собственный-стиль
naming: {
elem: '-',
mod: { name: '--', val: '_' },
delims: {
elem: '-',
mod: { name: '--', val: '_' }
},
wordPattern: '[a-zA-Z0-9]+'
}
}
Expand Down Expand Up @@ -424,8 +426,10 @@ module.exports = {

// Смотри https://ru.bem.info/toolbox/sdk/bem-naming/#Собственный-стиль
naming: {
elem: '-',
mod: { name: '--', val: '_' },
delims: {
elem: '-',
mod: { name: '--', val: '_' }
},
wordPattern: '[a-zA-Z0-9]+'
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/create-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

var fs = require('fs'),
path = require('path'),
bemNaming = require('bem-naming'),
bemNaming = require('@bem/naming'),
Promise = require('pinkie-promise'),
mkdirp = require('mkdirp'),
createTree = require('./create-tree');
createTree = require('./create-tree'),
relativePath = function(to) { return path.relative(process.cwd(), to); };

module.exports = function(entity, fileName, template, options) {
return new Promise(function(resolve, reject) {
Expand All @@ -14,7 +15,7 @@ module.exports = function(entity, fileName, template, options) {

fs.exists(fileName, function(exists) {
if (isFile && exists) {
options.noWarn || console.log('File ' + fileName + ' already exists.');
options.noWarn || console.log('File ' + relativePath(fileName) + ' already exists.');
return resolve(fileName);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/create-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
Promise = require('pinkie-promise');
Promise = require('pinkie-promise'),
relativePath = function(to) { return path.relative(process.cwd(), to); };

function walk(tree, root, options) {
return new Promise(function(rslv, rjct) {
Expand All @@ -23,7 +24,7 @@ function walk(tree, root, options) {

fs.exists(pathToFile, function(exists) {
if (exists) {
options.noWarn || console.log('File ' + pathToFile + ' already exists.');
options.noWarn || console.log('File ' + relativePath(pathToFile) + ' already exists.');
return resolve(file);
}

Expand Down
30 changes: 18 additions & 12 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

var path = require('path'),
bemConfig = require('bem-config'),
scheme = require('bem-fs-scheme'),
bemNaming = require('bem-naming'),
BemEntityName = require('@bem/entity-name'),
BemCell = require('@bem/cell'),
scheme = require('@bem/fs-scheme'),
bemNaming = require('@bem/naming'),
braceExpansion = require('brace-expansion'),
createEntity = require('./create-entity'),
getTemplate = require('./template'),
Expand Down Expand Up @@ -56,10 +58,8 @@ module.exports = function create(entities, levels, techs, options) {
return config.level(level).then(function(levelOptions) {
levelOptions || (levelOptions = {});

var namingOpts = levelOptions.naming,
levelScheme = levelOptions.scheme,
var levelScheme = levelOptions.scheme,
buildPath = scheme(levelScheme).path,
entity = filepathOrInput,

pluginConfLevels = Object.keys(pluginConf.levels || {})
.reduce(function(acc, relPluginConfLevel) {
Expand All @@ -73,16 +73,20 @@ module.exports = function create(entities, levels, techs, options) {

pluginTechs = pluginConf.techs || [],
currentTechs = uniq([].concat(pluginConfLevelTechs || pluginTechs, techs)),
templatesOpts = Object.assign({}, pluginConf, pluginConfLevel);
templatesOpts = Object.assign({}, pluginConf, pluginConfLevel),
entity;

if (isFileGlob) {
var file = path.basename(filepathOrInput),
splitted = file.split('.');
// split for entity key and tech (by first dot)
match = file.match(/^([^.]+)(?:\.(.+))?$/);

entity = bemNaming(namingOpts).parse(splitted.shift());
if (splitted.join('.')) {
currentTechs = uniq(techs.concat(splitted.join('.')));
entity = bemNaming(levelOptions.naming).parse(match[1]);
if (match[2]) {
currentTechs = uniq(techs.concat(match[2]));
}
} else {
entity = BemEntityName.create(filepathOrInput);
}

options.onlyTech && (currentTechs = options.onlyTech);
Expand All @@ -92,11 +96,13 @@ module.exports = function create(entities, levels, techs, options) {
}));

return Promise.all(currentTechs.map(function(tech) {
var pathToFile = buildPath(entity, tech, { naming: namingOpts }),
var pathToFile = buildPath(
new BemCell({ entity: entity, tech: tech }),
levelOptions),
absPathToFile = path.join(path.resolve(level), pathToFile),
template = getTemplate(tech, templatesOpts);

return createEntity(entity, absPathToFile, template, { naming: namingOpts });
return createEntity(entity, absPathToFile, template, levelOptions);
}));
});
});
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
"author": "",
"license": "MPL-2.0",
"dependencies": {
"@bem/cell": "^0.2.5",
"@bem/entity-name": "^1.3.2",
"@bem/fs-scheme": "^2.1.0",
"@bem/naming": "^2.0.0",
"bem-config": "^3.1.0",
"bem-fs-scheme": "^1.0.0",
"bem-naming": "^1.0.1",
"brace-expansion": "^1.1.6",
"mkdirp": "^0.5.1",
"pinkie-promise": "^2.0.1",
Expand Down
24 changes: 14 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ const path = require('path');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const create = require('..');
const naming = require('bem-naming');
const naming = require('@bem/naming');
const EOL = require('os').EOL;
const assert = require('assert');

const tmpDir = path.join(__dirname, 'tmp');
const initialCwd = process.cwd();
Expand All @@ -29,9 +30,10 @@ function testEntityHelper(entities, levels, techs, options, expected) {
expected.forEach(file => {
let actualContent;
const createdIndex = created.indexOf(file.name);
const relativeFileName = path.relative(initialCwd, file.name);

if (createdIndex === -1) {
throw new Error(`There is no ${file.name} in result returned`);
throw new Error(`There is no ${relativeFileName} in result returned`);
}

if (typeof file.content === 'undefined') {
Expand All @@ -41,12 +43,10 @@ function testEntityHelper(entities, levels, techs, options, expected) {
try {
actualContent = fs.readFileSync(file.name, 'utf8');
} catch (err) {
throw new Error(`${file.name} was not created`);
throw new Error(`${relativeFileName} was not created`);
}

if (actualContent !== file.content) {
throw new Error(`${file.name} content is not correct`);
}
assert.equal(actualContent, file.content, `${relativeFileName} content is not correct`);

created.splice(createdIndex, 1);
});
Expand Down Expand Up @@ -113,8 +113,10 @@ describe('bem-tools-create', () => {
it('should create entities with naming from config', () => {
const entity = { block: 'b', elem: 'e1', modName: 'm1', modVal: 'v1' };
const namingScheme = {
elem: '-',
mod: { name: '--', val: '_' }
delims: {
elem: '-',
mod: { name: '--', val: '_' }
}
};

return testEntityHelper([entity], [tmpDir], ['css'], { defaults: { naming: namingScheme } }, [{
Expand Down Expand Up @@ -161,8 +163,10 @@ describe('bem-tools-create', () => {
const levels = [path.join(tmpDir, 'l1'), path.join(tmpDir, 'l2')];
const entity = { block: 'b', elem: 'e1', modName: 'm1', modVal: 'v1' };
const namingScheme = {
elem: '-',
mod: { name: '--', val: '_' }
delims: {
elem: '-',
mod: { name: '--', val: '_' }
}
};
const opts = {
defaults: {
Expand Down

0 comments on commit 9889c68

Please sign in to comment.