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
4 changes: 2 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 49 additions & 4 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down
152 changes: 146 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down