Skip to content

Commit

Permalink
Added some test, fixed some bugs, added support for elem deps file
Browse files Browse the repository at this point in the history
  • Loading branch information
f0rmat1k committed Mar 4, 2015
1 parent a16cc1e commit 68ae568
Show file tree
Hide file tree
Showing 18 changed files with 173 additions and 163 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
node_modules/
temp/
.idea
test/
demo/
atlassian-ide-plugin.xml

Expand Down
49 changes: 24 additions & 25 deletions bem-info.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
var fs = require('fs');
var path = require('path');

// todo write to blond (bem-walk)
module.exports = function(trgPath){
module.exports = function(trgPath, isFile){
var info = {};

info.stat = fs.statSync(trgPath);
info.type = getTargetTypeByStat(info.stat, trgPath);
info.isDir = info.stat.isDirectory();
info.isFile = info.stat.isFile();
info.dirName = getDirNameByPath(trgPath);
try { info.stat = fs.statSync(trgPath); } catch (e) { }

if (!info.isFile) {
info.isBlock = isBlock(trgPath);
info.isElem = isElem(trgPath);
info.isMod = isMod(trgPath);
}
info.isFile = info.stat && info.stat.isFile() || isFile;
info.isDir = info.stat && info.stat.isDirectory() || !info.isFile;
info.type = getTargetType(info.isFile, trgPath);
info.dirName = getDirNameByPath(trgPath, info.isFile);

info.isBlock = isBlock(info.dirName);
info.isElem = isElem(info.dirName);
info.isMod = isMod(info.dirName);

info.blockName = getBlockName(trgPath, info.isBlock);
info.blockName = getBlockName(trgPath, info.isBlock, info.isFile);
info.elemName = getElemName(info.isElem, info.isMod, info.dirName, trgPath);
info.modName = getModName(info.isMod, info.dirName);
info.bemName = getBemName(info.blockName, info.elemName, info.modName);

return info;
};

function getTargetTypeByStat(stat, trgPath){
return stat.isFile() ? detectFileType(trgPath) : detectDirType(trgPath);
function getTargetType(isFile, trgPath){
return isFile ? detectFileType(trgPath) : detectDirType(trgPath);
}

function getDirNameByPath(p){
return path.basename(p);
function getDirNameByPath(p, isFile){
return isFile ? path.basename(path.dirname(p)) : path.basename(p);
}

function isBlock(trgPath){
return /\/[^_][-a-z0-9]+$/i.test(trgPath);
function isBlock(dirName){
return !/^_/i.test(dirName);
}

function isElem(trgPath){
return /__[-a-z0-9]+$/ig.test(trgPath);
function isElem(dirName){
return /^(__)[-a-z0-9]+\/?$/ig.test(dirName);
}

function isMod(trgPath){
return /\/_[-a-z0-9]+$/ig.test(trgPath);
return /^_[-a-z0-9]+$/ig.test(trgPath);
}

function getBemName(blockName, elemName, modName){
return blockName + elemName + modName;
}

function getBlockName(trgPath, isBlock){
if (isBlock) return trgPath.match(/[-a-z0-9]+$/i)[0];
function getBlockName(trgPath, isBlock, isFile){
if (isFile) trgPath = path.dirname(trgPath);
if (isBlock) return trgPath.match(/([-a-z0-9]+)\/?$/i)[1];

var dirName = path.dirname(trgPath),
baseName = path.basename(dirName);
Expand Down Expand Up @@ -97,4 +96,4 @@ function detectDirType(targetDir){
} else dirType = 'blockDir';

return dirType;
}
}
61 changes: 3 additions & 58 deletions bemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,69 +24,12 @@ var BEM_INFO = bemInfo(trgPath),
},
tasks = {
auto: DEFAULT_ACTIONS[BEM_INFO.type],
create: startCreating.bind(this, prompt),
rename: renameBemNode.bind(this, prompt[0], trgPath)
create: startCreating.bind(this, prompt)
};

var task = options.t || 'auto';
tasks[task]();

function renameBemNode(newName, nodePath, basename){
if (!valdateBemName(newName)) {
console.error('Invalid name: ' + newName);
return;
}

var info = bemInfo(nodePath);

basename = basename || {
type: info.type,
name : newName
};

if (info.isFile) {

}

return;

var info = bemInfo(nodePath),
nodeName;

if (info.isBlock) {
nodeName = info.blockName;
} else if (info.isElem) {
nodeName = info.elemName
} else if (info.isMod) {
nodeName = info.modName
}

var nodePath2 = nodePath;
//rename own files
fs.readdirSync(nodePath2).forEach(function(node){

var nodePath = path.join(nodePath2, node);
var info = bemInfo(nodePath);
if (info.isFile) {
var oldFileName = path.basename(nodePath),
newFileName = oldFileName.replace(BEM_INFO.blockName, newName),
newPath = nodePath.replace(oldFileName, newFileName);
console.log(newPath);
fs.renameSync(nodePath, newPath);
} else {
renameBemNode(newName, nodePath);
}
});

//rename block name
//renameDir(nodePath, newName, BEM_INFO.blockName);
}

function renameDir(nodePath, newName, oldName){
var newPath = nodePath.replace(oldName, newName);
fs.renameSync(nodePath, newPath);
}

function valdateBemName(name){
return !/[^-a-z0-9]/ig.test(name);
}
Expand Down Expand Up @@ -134,6 +77,8 @@ function createNode(nodeObj){
modVal = nodeObj.modVal ? '_' + nodeObj.modVal : '';

if (nodeObj.elem) {
if (BEM_INFO.isElem) return;

nodePath = path.join(blockDir, '__' + nodeObj.elem);

if (!fs.existsSync(nodePath)) {
Expand Down
4 changes: 0 additions & 4 deletions demo/__huy/_bag/test__huy_bag_a.css

This file was deleted.

4 changes: 0 additions & 4 deletions demo/__huy/test__huy.css

This file was deleted.

4 changes: 0 additions & 4 deletions demo/__item/_selected/test__item_selected_yes.css

This file was deleted.

4 changes: 0 additions & 4 deletions demo/__item/test__item.css

This file was deleted.

4 changes: 0 additions & 4 deletions demo/__olo/_yo/test__olo_yo_bb.css

This file was deleted.

4 changes: 0 additions & 4 deletions demo/__olo/test__olo.css

This file was deleted.

4 changes: 0 additions & 4 deletions demo/_lol/test_lol_val.css

This file was deleted.

4 changes: 0 additions & 4 deletions demo/_sick/test_sick.css

This file was deleted.

5 changes: 0 additions & 5 deletions demo/test.bh.js

This file was deleted.

4 changes: 0 additions & 4 deletions demo/test.css

This file was deleted.

9 changes: 0 additions & 9 deletions demo/test.deps.js

This file was deleted.

16 changes: 0 additions & 16 deletions demo/test.js

This file was deleted.

12 changes: 0 additions & 12 deletions demo/test.priv.js

This file was deleted.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
{
"name": "bemy",
"description": "Helper for auto-creation BEM dir and file structure",
"main": "bemy.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/f0rmat1k/bemy.git"
},
"version": "1.2.10",
"version": "1.3.0",
"keywords": [
"bem",
"BEM Tools"
],
"dependencies": {
"deps-normalize": "^2.3.1",
"minimist": "^1.1.0"
},
"devDependencies": {
"mocha": "^2.1.0",
"should": "^5.0.1"
}
}
Loading

0 comments on commit 68ae568

Please sign in to comment.