Skip to content

Commit

Permalink
wip: get-path
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Akimov committed Mar 25, 2018
1 parent 83bfbdf commit 9e33dc2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 39 deletions.
21 changes: 12 additions & 9 deletions lib/create.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';

var prepareEntityData = require('./prepare-entity-data'),
createEntity = require('./create-entity'),
template = require('./template');
const getEntityData = require('./get-entity-data');
const getPath = require('./get-path');
const template = require('./template');
const createEntity = require('./create-entity');

module.exports = function create(entities, levels, techs, options) {
var createRes = prepareEntityData(entities, levels, techs, options);
var entityData = getEntityData(entities, levels, techs, options);

return Promise.all(createRes.map(item =>
template.apply(item).then(tmpl => createEntity(item.path, tmpl, item.levelOptions))
));
return Promise.all(entityData.map(item =>
template.apply(item)
.then(content =>
createEntity(getPath(item), content, item.levelOptions)
)
)
);
};
14 changes: 3 additions & 11 deletions lib/prepare-entity-data.js → lib/get-entity-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
var path = require('path'),
bemNaming = require('@bem/naming'),
BemEntityName = require('@bem/entity-name'),
BemCell = require('@bem/cell'),
braceExpansion = require('brace-expansion'),
scheme = require('@bem/fs-scheme'),
uniq = require('uniq'),
bemConfig = require('bem-config');

Expand Down Expand Up @@ -60,8 +58,6 @@ module.exports = function prepareEntityData(entities, levels, techs, options) {
var rootDir = config.rootSync() || cwd,
level = path.resolve(rootDir, relLevel),
levelOptions = config.levelSync(level) || {},
levelScheme = levelOptions.scheme,
buildPath = scheme(levelScheme).path,
currentTechs = uniq([].concat(levelOptions.techs || [], techs)),
entity;

Expand All @@ -85,21 +81,17 @@ module.exports = function prepareEntityData(entities, levels, techs, options) {
}));

return currentTechs.map(function(tech) {
var pathToFile = buildPath(
new BemCell({ entity: entity, tech: tech }),
levelOptions.schemeOptions || levelOptions),
absPathToFile = path.join(path.resolve(level), pathToFile);

if (options.forceRewrite) {
levelOptions.forceRewrite = options.forceRewrite;
}

return {
path: absPathToFile,
config: config,
levelOptions: levelOptions,
tech: tech,
options: options,
entity: entity
entity: entity,
level: level
};
});
});
Expand Down
16 changes: 16 additions & 0 deletions lib/get-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('path');
const scheme = require('@bem/fs-scheme');
const BemCell = require('@bem/cell');

module.exports = function getPath(item) {
const levelOptions = item.config.levelSync(item.level) || {};
const levelScheme = levelOptions.scheme;
const buildPath = scheme(levelScheme).path;
const pathToFile = buildPath(
new BemCell({ entity: item.entity, tech: item.tech }),
item.levelOptions.schemeOptions || item.levelOptions
);
const absolutePath = path.join(path.resolve(item.level), pathToFile);

return absolutePath;
};
16 changes: 0 additions & 16 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const create = require('..');
const naming = require('@bem/naming');
const EOL = require('os').EOL;
const assert = require('assert');
const stream = require('stream');

const tmpDir = path.join(__dirname, 'tmp');
const initialCwd = process.cwd();
Expand Down Expand Up @@ -798,20 +797,5 @@ describe('bem-tools-create', () => {
}])
);
});

it('should support custom content with pipe', () => {
const content = 'Some piped testing content';
const srcStream = new stream.Readable();
srcStream.push(content);
srcStream.push(null);

return testEntityHelper([{ block: 'b' }], [tmpDir], ['css'],
{ fileContent: srcStream }, [{
name: path.join(tmpDir, 'b', 'b.css'),
content: content
}]
);
});

});
});
7 changes: 4 additions & 3 deletions test/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
const path = require('path');
const mkdirp = require('mkdirp');
const assert = require('assert');
const prepareEntityData = require('../lib/prepare-entity-data');
const getEntityData = require('../lib/get-entity-data');
const getPath = require('../lib/get-path');
const tmpDir = process.cwd();

function testEntityHelper(entities, levels, techs, options, expected) {
const actualPaths = prepareEntityData(entities, levels, techs, options)
.map(item => item.path)
const actualPaths = getEntityData(entities, levels, techs, options)
.map(getPath)
.sort();
const expectdPaths = expected
.map(item => item.path)
Expand Down

0 comments on commit 9e33dc2

Please sign in to comment.