diff --git a/README.ru.md b/README.ru.md index 3ad345a..dca29aa 100644 --- a/README.ru.md +++ b/README.ru.md @@ -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' }, @@ -350,8 +347,6 @@ module.exports = { { path: 'path/to/level2', - // Значение по умолчанию, можно не указывать - scheme: 'nested', // Смотри https://ru.bem.info/toolbox/sdk/bem-naming/#Собственный-стиль naming: { @@ -359,6 +354,12 @@ module.exports = { elem: '-', mod: { name: '--', val: '_' } }, + // Значение по умолчанию, можно не указывать + fs: { + delims: { elem: '__', mod: '_' }, + pattern: '${layer?${layer}.}blocks/${entity}.${tech}', + scheme: 'nested' + }, wordPattern: '[a-zA-Z0-9]+' } } @@ -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' @@ -442,8 +441,6 @@ module.exports = { { path: 'path/to/level2', - // Значение по умолчанию, можно не указывать - scheme: 'nested', // Смотри https://ru.bem.info/toolbox/sdk/bem-naming/#Собственный-стиль naming: { diff --git a/cli.js b/cli.js index 6d6bd51..6279d7c 100644 --- a/cli.js +++ b/cli.js @@ -4,6 +4,7 @@ var create = require('./'); function noOp() {} +/* eslint indent: "off" */ module.exports = function() { this .title('Create BEM entity').helpful() diff --git a/lib/create-entity.js b/lib/create-entity.js index b813721..f166845 100644 --- a/lib/create-entity.js +++ b/lib/create-entity.js @@ -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); }; diff --git a/lib/create-tree.js b/lib/create-tree.js index e31a39d..5b223b6 100644 --- a/lib/create-tree.js +++ b/lib/create-tree.js @@ -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) { diff --git a/lib/create.js b/lib/create.js index cf48b8f..4cef5ef 100644 --- a/lib/create.js +++ b/lib/create.js @@ -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 = {}); @@ -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])); } @@ -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; diff --git a/package.json b/package.json index 6df68b4..8b66fb1 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/test.js b/test/test.js index 2e01b1d..0b04b0f 100644 --- a/test/test.js +++ b/test/test.js @@ -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'); @@ -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 + ' {', @@ -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', () => { @@ -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') }]); @@ -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: '-', @@ -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) }]); @@ -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: '-', @@ -177,7 +180,11 @@ describe('bem-tools-create', () => { }, { path: levels[1], - scheme: 'flat' + naming: { + fs: { + scheme: 'flat' + } + } } ] }