Skip to content
Open
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
21 changes: 9 additions & 12 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,8 @@ module.exports = {
levels: [
{
path: 'level1',
// Смотри https://ru.bem.info/toolbox/sdk/bem-fs-scheme/
scheme: 'nested',
schemeOptions: 'react'

// Смотри https://ru.bem.info/toolbox/sdk/bem-naming/#В-стиле-Гарри-Робертса
// Смотри https://github.com/bem/bem-sdk/tree/master/packages/naming.presets
naming: 'two-dashes'
},

Expand All @@ -350,15 +347,19 @@ module.exports = {

{
path: 'path/to/level2',
// Значение по умолчанию, можно не указывать
scheme: 'nested',

// Смотри https://ru.bem.info/toolbox/sdk/bem-naming/#Собственный-стиль
naming: {
delims: {
elem: '-',
mod: { name: '--', val: '_' }
},
// Значение по умолчанию, можно не указывать
fs: {
delims: { elem: '__', mod: '_' },
pattern: '${layer?${layer}.}blocks/${entity}.${tech}',
scheme: 'nested'
},
wordPattern: '[a-zA-Z0-9]+'
}
}
Expand Down Expand Up @@ -417,16 +418,14 @@ module.exports = {
В корне конфигурационного файла можно задать
[схему именования](https://ru.bem.info/toolbox/sdk/bem-naming/) БЭМ-сущностей
(см. [соглашение по именованию](https://ru.bem.info/methodology/naming-convention/#Соглашение-по-именованию-css-селекторов)) и
[схему файловой структруры](https://ru.bem.info/toolbox/sdk/bem-fs-scheme/)
(значение по умолчанию `nested`).
[схему файловой структруры](https://github.com/bem/bem-sdk/tree/master/packages/naming.cell.stringify)
(значение по умолчанию `fs: { delims: { elem: '__', mod: '_' }, pattern: '${layer?${layer}.}blocks/${entity}.${tech}', scheme: 'nested' }`).

```js
module.exports = {
levels: [
{
path: 'level1',
// Смотри https://ru.bem.info/toolbox/sdk/bem-fs-scheme/
scheme: 'flat',

// Смотри https://ru.bem.info/toolbox/sdk/bem-naming/#В-стиле-Гарри-Робертса
naming: 'two-dashes'
Expand All @@ -442,8 +441,6 @@ module.exports = {

{
path: 'path/to/level2',
// Значение по умолчанию, можно не указывать
scheme: 'nested',

// Смотри https://ru.bem.info/toolbox/sdk/bem-naming/#Собственный-стиль
naming: {
Expand Down
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var create = require('./');

function noOp() {}

/* eslint indent: "off" */
module.exports = function() {
this
.title('Create BEM entity').helpful()
Expand Down
3 changes: 1 addition & 2 deletions lib/create-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
var fs = require('fs'),
path = require('path'),
stream = require('stream'),
bemNaming = require('@bem/naming'),
Promise = require('pinkie-promise'),
bemNaming = require('@bem/sdk.naming.entity'),
mkdirp = require('mkdirp'),
createTree = require('./create-tree'),
relativePath = function(to) { return path.relative(process.cwd(), to); };
Expand Down
1 change: 0 additions & 1 deletion lib/create-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp'),
Promise = require('pinkie-promise'),
relativePath = function(to) { return path.relative(process.cwd(), to); };

function walk(tree, root, options) {
Expand Down
44 changes: 29 additions & 15 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

var path = require('path'),
bemConfig = require('@bem/sdk.config'),
BemEntityName = require('@bem/entity-name'),
BemCell = require('@bem/cell'),
scheme = require('@bem/fs-scheme'),
bemNaming = require('@bem/naming'),
BemEntityName = require('@bem/sdk.entity-name'),
BemCell = require('@bem/sdk.cell'),
bemNamingCellStringify = require('@bem/sdk.naming.cell.stringify'),
bemNamingParse = require('@bem/sdk.naming.entity.parse'),
bemNamingPresets = require('@bem/sdk.naming.presets'),
braceExpansion = require('brace-expansion'),
createEntity = require('./create-entity'),
getTemplate = require('./template'),
uniq = require('uniq'),
Promise = require('pinkie-promise');
uniq = require('uniq');

module.exports = function create(entities, levels, techs, options) {
options || (options = {});
Expand Down Expand Up @@ -60,17 +60,25 @@ module.exports = function create(entities, levels, techs, options) {
return config.level(level).then(function(levelOptions) {
levelOptions || (levelOptions = {});

var levelScheme = levelOptions.scheme,
buildPath = scheme(levelScheme).path,
currentTechs = uniq([].concat(levelOptions.techs || [], techs)),
entity;
var currentTechs = uniq([].concat(levelOptions.techs || [], techs)),
entity,
naming = typeof levelOptions.naming === 'string' ?
bemNamingPresets[levelOptions.naming] :
Object.assign({}, bemNamingPresets.origin, levelOptions.naming);

// NOTICE: for backward compatibility only
// should be eventually removed
if (!levelOptions.naming || !levelOptions.naming.fs || !levelOptions.naming.fs.pattern) {
naming.fs.pattern = '${layer}/${entity}.${tech}';
}

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

entity = bemNaming(levelOptions.naming).parse(match[1]);
entity = bemNamingParse(naming)(match[1]);

if (match[2]) {
currentTechs = uniq(techs.concat(match[2]));
}
Expand Down Expand Up @@ -98,10 +106,16 @@ module.exports = function create(entities, levels, techs, options) {
}));

return Promise.all(currentTechs.map(function(tech) {
var pathToFile = buildPath(
new BemCell({ entity: entity, tech: tech }),
levelOptions.schemeOptions || levelOptions),
absPathToFile = path.join(path.resolve(level), pathToFile),
var pathToFile = bemNamingCellStringify(naming)(
new BemCell({
entity: entity,
tech: tech,
layer: path.relative(process.cwd(), level)
})
),

// FIXME: hardcode about `common`!!!
absPathToFile = path.resolve(pathToFile.replace(/^common\//, '')),
template = options.fileContent || getTemplate(tech, levelOptions);

levelOptions.forceRewrite = options.forceRewrite;
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
"author": "",
"license": "MPL-2.0",
"dependencies": {
"@bem/cell": "^0.2.5",
"@bem/entity-name": "^1.3.2",
"@bem/fs-scheme": "^2.1.1-0",
"@bem/naming": "^2.0.0-5",
"@bem/sdk.config": "0.0.8",
"@bem/sdk.cell": "^0.2.8",
"@bem/sdk.config": "0.0.10",
"@bem/sdk.entity-name": "^0.2.10",
"@bem/sdk.naming.cell.stringify": "0.0.11",
"@bem/sdk.naming.entity": "^0.2.10",
"@bem/sdk.naming.entity.stringify": "^1.1.1",
"@bem/sdk.naming.presets": "^0.2.2",
"brace-expansion": "^1.1.6",
"mkdirp": "^0.5.1",
"pinkie-promise": "^2.0.1",
"uniq": "^1.0.1"
},
"devDependencies": {
"eslint": "^3.16.0",
"eslint-config-pedant": "^0.8.0",
"mocha": "^3.1.0",
"rimraf": "^2.5.4"
"eslint": "^5.5.0",
"eslint-config-pedant": "^1.0.1",
"mocha": "^5.2.0",
"rimraf": "^2.6.2"
}
}
27 changes: 17 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const path = require('path');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const create = require('..');
const naming = require('@bem/naming');
const namingStringify = require('@bem/sdk.naming.entity.stringify');
const namingPresets = require('@bem/sdk.naming.presets');
const EOL = require('os').EOL;
const assert = require('assert');
const stream = require('stream');
Expand All @@ -15,7 +16,9 @@ const initialCwd = process.cwd();

const templates = {
css: function(entity, namingScheme) {
const className = typeof entity === 'string' ? entity : naming(namingScheme).stringify(entity);
const className = typeof entity === 'string' ?
entity :
namingStringify(namingScheme || namingPresets.origin)(entity);

return [
'.' + className + ' {',
Expand Down Expand Up @@ -62,8 +65,8 @@ describe('bem-tools-create', () => {
beforeEach(() => mkdirp.sync(tmpDir));

afterEach(() => {
rimraf.sync(tmpDir);
process.chdir(initialCwd);
rimraf.sync(tmpDir);
});

describe('default scheme and default naming', () => {
Expand All @@ -82,14 +85,14 @@ describe('bem-tools-create', () => {
});

it('should create an block modifier using `nested` scheme and default naming', () => {
return testEntityHelper([{ block: 'b', modName: 'm', modVal: 'v' }], [tmpDir], ['css'], {}, [{
return testEntityHelper([{ block: 'b', mod: { name: 'm', val: 'v' } }], [tmpDir], ['css'], {}, [{
name: path.join(tmpDir, 'b', '_m', 'b_m_v.css'),
content: templates.css('b_m_v')
}]);
});

it('should create an element modifier using `nested` scheme and default naming', () => {
return testEntityHelper([{ block: 'b', elem: 'e', modName: 'em', modVal: 'ev' }], [tmpDir], ['css'], {}, [{
return testEntityHelper([{ block: 'b', elem: 'e', mod: { name: 'em', val: 'ev' } }], [tmpDir], ['css'], {}, [{
name: path.join(tmpDir, 'b', '__e', '_em', 'b__e_em_ev.css'),
content: templates.css('b__e_em_ev')
}]);
Expand All @@ -111,7 +114,7 @@ describe('bem-tools-create', () => {

describe('custom options', () => {
it('should create entities with naming from config', () => {
const entity = { block: 'b', elem: 'e1', modName: 'm1', modVal: 'v1' };
const entity = { block: 'b', elem: 'e1', mod: { name: 'm1', val: 'v1' } };
const namingScheme = {
delims: {
elem: '-',
Expand All @@ -126,9 +129,9 @@ describe('bem-tools-create', () => {
});

it('should create blocks with scheme from config', () => {
const entity = { block: 'b', elem: 'e1', modName: 'm1', modVal: 'v1' };
const entity = { block: 'b', elem: 'e1', mod: { name: 'm1', val: 'v1' } };

return testEntityHelper([entity], [tmpDir], ['css'], { defaults: { scheme: 'flat' } }, [{
return testEntityHelper([entity], [tmpDir], ['css'], { defaults: { naming: { fs: { scheme: 'flat' } } } }, [{
name: path.join(tmpDir, 'b__e1_m1_v1.css'),
content: templates.css(entity)
}]);
Expand Down Expand Up @@ -161,7 +164,7 @@ describe('bem-tools-create', () => {

it('should create entities on levels with provided config', () => {
const levels = [path.join(tmpDir, 'l1'), path.join(tmpDir, 'l2')];
const entity = { block: 'b', elem: 'e1', modName: 'm1', modVal: 'v1' };
const entity = { block: 'b', elem: 'e1', mod: { name: 'm1', val: 'v1' } };
const namingScheme = {
delims: {
elem: '-',
Expand All @@ -177,7 +180,11 @@ describe('bem-tools-create', () => {
},
{
path: levels[1],
scheme: 'flat'
naming: {
fs: {
scheme: 'flat'
}
}
}
]
}
Expand Down