From c61ba5423065a8a74618fe3a1336ff240d749fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=83=D1=80=D1=81=D0=B5=D0=BD=D0=BA=D0=BE=20=D0=90?= =?UTF-8?q?=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Sat, 25 Mar 2017 18:01:48 +0300 Subject: [PATCH] Applying context of execution --- cli.js | 4 +- lib/create.js | 53 ++++++++++++++++-- test/test.js | 152 ++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 197 insertions(+), 12 deletions(-) diff --git a/cli.js b/cli.js index 6d6bd51..66313dc 100644 --- a/cli.js +++ b/cli.js @@ -85,8 +85,8 @@ module.exports = function() { return create(args.entities, opts.level, techs, options).then(noOp); } - opts.block && create([{ - block: opts.block[0], + (opts.block || opts.elem || opts.mod) && create([{ + block: opts.block && opts.block[0], elem: opts.elem && opts.elem[0], modName: opts.mod && opts.mod[0], modVal: opts.val ? opts.val[0] : Boolean(opts.mod) diff --git a/lib/create.js b/lib/create.js index 653d3b5..526cb72 100644 --- a/lib/create.js +++ b/lib/create.js @@ -63,18 +63,59 @@ module.exports = function create(entities, levels, techs, options) { var levelScheme = levelOptions.scheme, buildPath = scheme(levelScheme).path, currentTechs = uniq([].concat(levelOptions.techs || [], techs)), - entity; + entity, + pathInLevel, + fullPathPrefix, + namingPrefix; if (isFileGlob) { var file = path.basename(filepathOrInput), // split for entity key and tech (by first dot) - match = file.match(/^([^.]+)(?:\.(.+))?$/); + match = file.match(/^([^.]+)(?:\.(.+))?$/), + namingOpts = levelOptions.naming; + + // parse string to entity + entity = bemNaming(namingOpts).parse(match[1]); + + // if not parsed and we are inside level directory + if (!entity && cwd.indexOf(level) === 0) { + // first two parts of path prefix in level + pathInLevel = path.relative(level, cwd).split(path.sep).slice(0, 2); + fullPathPrefix = pathInLevel.join(''); + + // parse only full prefix + namingPrefix = bemNaming(namingOpts).parse(fullPathPrefix); + // if prefix parsed and elem was found in (elem must be in second dir) + if (namingPrefix && namingPrefix.elem) { + // then parse string with prefix (to add something in block's elem) + entity = bemNaming(namingOpts).parse(fullPathPrefix + match[1]); + } + // if was unsuccessfull then try to add into block only (first part) + entity || (entity = bemNaming(namingOpts).parse(pathInLevel[0] + match[1])); + } - entity = bemNaming(levelOptions.naming).parse(match[1]); if (match[2]) { currentTechs = uniq(techs.concat(match[2])); } } else { + // if block was not set and we are inside level directory + if (!filepathOrInput.block && cwd.indexOf(level) === 0) { + // first two parts of path prefix in level + pathInLevel = path.relative(level, cwd).split(path.sep).slice(0, 2); + // if we are not set elem + if (!filepathOrInput.elem) { + namingPrefix = bemNaming(namingOpts).parse(pathInLevel.join('')); + // if prefix parsed and elem was found in (there must be elem in two dirs) + if (namingPrefix && namingPrefix.elem) { + filepathOrInput.block = namingPrefix.block; + filepathOrInput.elem = namingPrefix.elem; + } + } + if (!filepathOrInput.block) { + namingPrefix = bemNaming(namingOpts).parse(pathInLevel[0]); + namingPrefix && (filepathOrInput.block = namingPrefix.block); + } + } entity = BemEntityName.create(filepathOrInput); } @@ -100,7 +141,11 @@ module.exports = function create(entities, levels, techs, options) { })); })); - })).then(flatten); + })) + .then(flatten) + .catch(err => { + console.log('Error occured: ', err); + }); }; function flatten(arr) { diff --git a/test/test.js b/test/test.js index 3662684..ee7e402 100644 --- a/test/test.js +++ b/test/test.js @@ -62,8 +62,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', () => { @@ -714,19 +714,159 @@ describe('bem-tools-create', () => { }); describe('respect context', () => { - it.skip('should get block from context', () => { + function runInFakeDir(dir) { + const fakeCwd = path.join(tmpDir, dir); + mkdirp.sync(fakeCwd); + process.chdir(fakeCwd); + } + describe('with string entities', () => { + it('should create elem in block context', () => { + runInFakeDir('bl1'); - }); + return testEntityHelper('__e1', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '__e1', 'bl1__e1.t1') } + ]); + }); + + it('should create block in level from block context', () => { + runInFakeDir('bl1'); + + return testEntityHelper('bl2__e1', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl2', '__e1', 'bl2__e1.t1') } + ]); + }); + + it('should create block in level from any directory in block context', () => { + runInFakeDir('bl1/someDir'); + + return testEntityHelper('bl3__e1', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl3', '__e1', 'bl3__e1.t1') } + ]); + }); + + it('should create mod in block context', () => { + runInFakeDir('bl1'); + + return testEntityHelper('_mod1', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '_mod1', 'bl1_mod1.t1') } + ]); + }); - it.skip('should get block and elem from context', () => { + it('should create mod with value in block context', () => { + runInFakeDir('bl1'); + return testEntityHelper('_mod1_val1', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '_mod1', 'bl1_mod1_val1.t1') } + ]); + }); + + it('should create mod for block from mod context', () => { + runInFakeDir(path.join('bl1', '_mod1')); + + return testEntityHelper('_mod2', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '_mod2', 'bl1_mod2.t1') } + ]); + }); + + it('should create mod with value for block from mod context', () => { + runInFakeDir(path.join('bl1', '_mod1')); + + return testEntityHelper('_mod2_val1', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '_mod2', 'bl1_mod2_val1.t1') } + ]); + }); + + it('should create mod for elem from elem context', () => { + runInFakeDir(path.join('bl1', '__elem1')); + + return testEntityHelper('_mod2_val1', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '__elem1', '_mod2', 'bl1__elem1_mod2_val1.t1') } + ]); + }); + + it('should create elem with mod in block from elem context', () => { + runInFakeDir(path.join('bl1', '__elem1')); + + return testEntityHelper('__elem2_mod1_val1', tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '__elem2', '_mod1', 'bl1__elem2_mod1_val1.t1') } + ]); + }); }); - it.skip('should get modName from context', () => { + describe('with arguments entities', () => { + it('should create elem in block context', () => { + runInFakeDir('bl1'); + + return testEntityHelper({ elem: 'e1' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '__e1', 'bl1__e1.t1') } + ]); + }); + + it('should create block in level from block context', () => { + runInFakeDir('bl1'); + + return testEntityHelper({ block: 'bl2', elem: 'e1' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl2', '__e1', 'bl2__e1.t1') } + ]); + }); + + it('should create block in level from any directory in block context', () => { + runInFakeDir('bl1/someDir'); + return testEntityHelper({ block: 'bl3', elem: 'e1' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl3', '__e1', 'bl3__e1.t1') } + ]); + }); + + it('should create mod in block context', () => { + runInFakeDir('bl1'); + + return testEntityHelper({ modName: 'mod1' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '_mod1', 'bl1_mod1.t1') } + ]); + }); + + it('should create mod with value in block context', () => { + runInFakeDir('bl1'); + + return testEntityHelper({ modName: 'mod1', modVal: 'val1' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '_mod1', 'bl1_mod1_val1.t1') } + ]); + }); + + it('should create mod for block from mod context', () => { + runInFakeDir(path.join('bl1', '_mod1')); + + return testEntityHelper({ modName: 'mod2' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '_mod2', 'bl1_mod2.t1') } + ]); + }); + + it('should create mod with value for block from mod context', () => { + runInFakeDir(path.join('bl1', '_mod1')); + + return testEntityHelper({ modName: 'mod2', modVal: 'val1' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '_mod2', 'bl1_mod2_val1.t1') } + ]); + }); + + it('should create mod for elem from elem context', () => { + runInFakeDir(path.join('bl1', '__elem1')); + + return testEntityHelper({ modName: 'mod2', modVal: 'val1' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '__elem1', '_mod2', 'bl1__elem1_mod2_val1.t1') } + ]); + }); + + it('should create elem with mod in block from elem context', () => { + runInFakeDir(path.join('bl1', '__elem1')); + + return testEntityHelper({ elem: 'elem2', modName: 'mod1', modVal: 'val1' }, tmpDir, ['t1'], {}, [ + { name: path.join(tmpDir, 'bl1', '__elem2', '_mod1', 'bl1__elem2_mod1_val1.t1') } + ]); + }); }); - // modVal if cwd is inside mod }); describe('command line arguments support', () => {