Skip to content

Commit

Permalink
Deep renaming Closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
f0rmat1k committed Mar 21, 2015
1 parent d5a753e commit 9f78e60
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 322 deletions.
90 changes: 55 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,94 @@
#Bemy
[![Build Status][travis-image]][travis-url]
Bemy is a CLI helper for auto-generation BEM structure. Especialy usefull with webstorm (external tools). It allows to generate folder and file structure using deps.js file with one command (or hotkey). For all file types taking templates with including BEM names into placeholders. You can tune it and you can add your own file types.
Bemy is a CLI helper for auto-generation and renaming BEM structure. Especialy usefull with webstorm (external tools). It allows to generate folder and file structure using deps.js file with one command (or hotkey). For all file types taking templates with including BEM names into placeholders. You can tune it and you can add your own file types.
Bemy can run three usable tasks: 'create', 'rename' and 'auto';

##Install
```bash
npm i -g bemy
```
##Usage
```bash
bemy [options]
bemy -t [task] -f [path] [options]
```
###CLI options

When you call bemy on files it is like you call bemy on the folder contains this file. So this two variants are equal: `-f ~/testBlock/__elem` and `-f ~/testBlock/__elem/testBlock__elem.bh.js`.

###Shared CLI options
`-t [task name]` — name of the called task. default: 'auto';
`-f [path]` — path to BEM node (folder or file);
`-p [file list]` — file types list for task of `creation`. Available following file types: `-p "css js deps priv bh"`. Also you can use short notation `p c j b d`. You can add you own file types and shortcuts at config.json.
`-g` — adding into git for created files (calls `git add` for each file);
`-c [config path]` — path of your own config json file. By default using config.json from bemy directory;
`-o` — to open the file after creation. This command configured in config.json in section `editor-open-command`. Default value is `wstorm {{file-path}}:{{line-number}}`. See more details at below in section `Configuring`.
`-g` — adding into git for created or renamed files (calls `git add` for each file);
`-c [config path]` — path of your own config json file. By default used config.json from bemy directory;

###The task of creation
Takes arguments with file types and creates files using templates.

####CLI of task of creation:
`bemy -t create -f [file path] -p "[file types]"`, where `file path` — is a path to BEM node (folder or file), `file types` — list of needed files separated by space.
####CLI for task of creation:
`bemy -t create -f [path] -p "[file types]"`, where `file path` — is a path to BEM node (folder or file), `file types` — list of needed files separated by space.
Example:
Command: `bemy -t create -f ~/testBlock/__elem -p "css js"`
Result: In the folder `~/testBlock/__elem` was added two files: `testBlock__elem.js` and `testBlock__elem.css`.

When you use bemy on files supposed to use bemy on the folder containg this file. So this two variants are equal: `-f ~/testBlock/__elem` and `-f ~/testBlock/__elem/testBlock__elem.bh.js`.
####Options
`-o` — to open the file after creation. This command configured in config.json in section `editor-open-command`. Default value is `wstorm {{file-path}}:{{line-number}}`. See more details at below in section `Configuring`.;
`-p [file list]` — file types list. Available following file types: `-p "css js deps priv bh"`. Also you can use short notation `p c j b d`. You can add you own file types and shortcuts at config.json.

####Placeholders in templates
There are following placeholders: `{{blockName}}`, `{{elemName}}`, `{{modName}}`, `{{modVal}}` and `{{cursor}}`. When files creating this entries will be replaced with relevant part of BEM node name. And `{{cursor}}` will be deleted and used for setting cursor line number (see more at Configuring section).
For example, default css template contain:
```
.{{blockName}}{{elemName}}{{modName}}{{modVal}}
{
{{cursor}}
}
```
, so resulted file will contain:
```css
.testBlock__elem {
/* curor will be here, if you use right configured -o key */
}
```

An example of using bemy with `external tools` of webstorm:
An example of using bemy with `external tools` of webstorm for the task of creation:
![](https://cloud.githubusercontent.com/assets/769992/6725632/0232f4ee-ce2e-11e4-942e-7845381663ed.png)
Don't forget to configure hotkey for task running (e.g. `ctrl + c`) at `keymap` section.

###Task of renaming
Recursively renames current node and its children. `-d` turn on deep mode and the contents of the files will also be renamed. Deep rename used `rename` param from config file so you should to configure it for you own file types. `rename` renames only described files and valid directories (e.g. mod folder in mod folder isn't valid).

####CLI for task of renaming:
`bemy -t rename -f [path] -p [new name] -d`

####Options
`-d` — turn on deep mode with renaming of files content;
`-p` — new BEM node name;

An example of using bemy with `external tools` of webstorm for the task of renaming:
![](https://cloud.githubusercontent.com/assets/769992/6766361/e3006d96-d025-11e4-948e-1f11a663f2ea.png)

###Autotask
Call default action depend on BEM node. Currently work following variants:
1. If target is deps-file, creates described elems\mods\elemMods folder structure. And depend on options of config.json also creates elems\mods\elemMods files. By default it's css files. Se `Configuring` section for more details.
2. Otherwise call create task with default options (equal `-t create -f [file path] -p "css"``). Default file types for autotask configurable at config.json.
2. Otherwise call create task with default options (equal `-t create -f [path] -p "css"`). Default file types for autotask configurable at config.json.

####CLI of autotask:
`bemy -f [file path]`, where `file path` — it's a path to BEM node.
####CLI for autotask
`bemy -f [path]`

An example of using bemy with `external tools` of webstorm:
An example of using bemy with `external tools` of webstorm for the autotask:
![](https://cloud.githubusercontent.com/assets/769992/6725778/23a5188a-ce30-11e4-828d-0d590fb26e08.png)
Don't forget to configure hotkey for task running (e.g. `ctrl + a`) at `keymap` section.

###Confgiring
`config.json` is in bemy root folder.

####Sections
`suffixes` — a list of shotrcuts and relevant file extensions. Shotrcuts using in `-p` key.
`file-templates` — a list of shortcuts with relevant pathes to templates.
`deps_task` – options for autotask when it called on deps file. `files` – list of files to be created in addition to folders.
#####`file-types`
Description of the used file types.
`suffix` used by 'create' when forming file and by 'rename' for files validation (`rename` renames only described files);
`shortcuts` — list of short that you can use after `-p` key in Task of creation;
`rename` – mask for deep renaming. {{bemNode}} will be replaced to new node name. You can also use an array of masks;
`template` — path to template of file type. Teamplate used when any files are created.
There are following placeholders: `{{blockName}}`, `{{elemName}}`, `{{modName}}`, `{{modVal}}` and `{{cursor}}`. When files are created this entries will be replaced with relevant part of BEM node name. And `{{cursor}}` will be deleted and used for setting cursor line number (see more at Configuring section).
For example, default css template contain:
```
.{{blockName}}{{elemName}}{{modName}}{{modVal}}
{
{{cursor}}
}
```
, so resulted file will contain:
```css
.testBlock__elem {
/* curor will be here, if you use right configured -o key */
}
```
`deps_task` – options for autotask when it called on deps file. `files` – list of file types to be created in addition to folders.
`editor-open-command` — command to be called after creating the file. There are two placeholders: 1) {{file-path}} to be replaced with relevant file path. 2) {{line-number}} will be taken from {{cursor}} position of relevant template. Default command is `wstorm {{file-path}}:{{line-number}}`, so if you use webstorm you need to create CLI launcher at webstorm with same name (Tools / Create Command-line Lanucher). If u use old wersion of webstorm you can try to use `/Applications/WebStorm.app/Contents/MacOS/webide` for `editor-open-command`.
`bem` — your BEM options. If you using own `separators` you must set right `allowed-name-symbols-regexp`.
`bem` — your BEM options. If you use own `separators` you must set right `allowed-name-symbols-regexp`.

[travis-url]: http://travis-ci.org/f0rmat1k/bemy
[travis-image]: http://img.shields.io/travis/f0rmat1k/bemy.svg?branch=master&style=flat
81 changes: 42 additions & 39 deletions bem-info.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
var fs = require('fs');
var path = require('path');
//todo сепараторы кастомной длины
module.exports = function(config){
return function(trgPath, isFile){
var info = {},
separators = config.bem.separators,
allowedSymbols = config.bem['allowed-name-symbols-regexp'];

module.exports = function(options){
var info = {},
trgPath = options.trgPath,
isFile = options.isFile,
separators = (options.bem && options.bem.separators) || { elem: '__', mod: '_', modVal: '_'},
allowedSymbols = options.bem && options.bem['allowed-name-symbols-regexp'] || '[-a-z0-9]';
if (!trgPath) throw new Error('Required path');

if (!trgPath) throw new Error('Required path');
try { info.stat = fs.statSync(trgPath); } catch (e) { }

try { info.stat = fs.statSync(trgPath); } catch (e) { }
info.isFile = info.stat && info.stat.isFile() || isFile || false;
info.isDir = info.stat && info.stat.isDirectory() || !info.isFile;
info.type = getTargetType(info.isFile, trgPath, config);
info.dirPath = getDirPath(trgPath, info.isFile);
info.dirName = getDirNameByPath(info.dirPath);
info.fileName = getFileNameByPath(trgPath, info.isFile);

info.isFile = info.stat && info.stat.isFile() || isFile || false;
info.isDir = info.stat && info.stat.isDirectory() || !info.isFile;
info.type = getTargetType(info.isFile, trgPath, options.bem);
info.dirPath = getDirPath(trgPath, info.isFile);
info.dirName = getDirNameByPath(info.dirPath);
info.fileName = getFileNameByPath(trgPath, info.isFile);
info.isBlock = isBlock(info.dirName, separators, config.bem);
info.isElem = isElem(info.dirName, separators, allowedSymbols);
info.isMod = isMod(info.dirName, separators, allowedSymbols);

info.isBlock = isBlock(info.dirName, separators, options.bem);
info.isElem = isElem(info.dirName, separators, allowedSymbols);
info.isMod = isMod(info.dirName, separators, allowedSymbols);
info.nodeType = info.isBlock ? 'block' : info.isElem ? 'elem' : info.isMod ? 'mod' : undefined;

info.nodeType = info.isBlock ? 'block' : info.isElem ? 'elem' : info.isMod ? 'mod' : undefined;
info.blockName = getBlockName(trgPath, info.isBlock, info.isFile, config.bem, allowedSymbols);
info.elemName = getElemName(info.isElem, info.isMod, info.dirName, info.dirPath, separators);
info.modName = getModName(info.isMod, info.dirName);
info.bemName = getBemName(info.blockName, info.elemName, info.modName, config.bem);
info.ownInfo = getOwnInfo(info, separators, allowedSymbols);

info.blockName = getBlockName(trgPath, info.isBlock, info.isFile, options.bem, allowedSymbols);
info.elemName = getElemName(info.isElem, info.isMod, info.dirName, trgPath, options.bem);
info.modName = getModName(info.isMod, info.dirName);
info.bemName = getBemName(info.blockName, info.elemName, info.modName, options.bem);
info.ownInfo = getOwnInfo(info, separators, allowedSymbols);

return info;
return info;
};
};

function getOwnInfo(info, separators, allowedSymbols){
Expand Down Expand Up @@ -60,8 +60,8 @@ function getFileNameByPath(trgPath, isFile){
return isFile ? path.basename(trgPath) : '';
}

function getTargetType(isFile, trgPath, bem){
return isFile ? detectFileType(trgPath) : detectDirType(trgPath, bem);
function getTargetType(isFile, trgPath, config){
return isFile ? detectFileType(trgPath, config) : detectDirType(trgPath, config.bem.separators);
}

function getDirPath(trgPath, isFile){
Expand Down Expand Up @@ -109,12 +109,12 @@ function getBlockName(trgPath, isBlock, isFile, bem, allowedSymbols){
return baseName;
}

function getElemName(isElem, isMod, dirName, trgPath, bem){
function getElemName(isElem, isMod, dirName, trgPath, separators){
if (isElem) return dirName;

if (isMod) {
var parentDir = path.basename(path.resolve(trgPath, '../')),
elemSeparator = bem && bem.separators.elem || '__',
elemSeparator = separators.elem,
elemRegExp = new RegExp('^(' + elemSeparator + ')', 'ig'),
parentIsElem = elemRegExp.test(parentDir);

Expand All @@ -128,22 +128,21 @@ function getModName(isMod, dirName){
return isMod ? dirName : '';
}

//todo
function detectFileType(targetFile) {
function detectFileType(targetFile, config) {
var filename = path.basename(targetFile);
var fileTypes = Object.keys(config['file-types']);

if (/\.deps\./.test(filename)) return 'deps';
if (/\.css$/.test(filename)) return 'css';
if (/\.priv\.js$/.test(filename)) return 'priv';
if (/\.bh\.js$/.test(filename)) return 'bh';
if (/\.js$/.test(filename)) return 'js';
for (var i = 0; i < fileTypes.length; i++) {
var regExp = new RegExp(escapeRegExp(config['file-types'][fileTypes[i]].suffix) + '$');
if (regExp.test(filename)) return fileTypes[i];
}
}

function detectDirType(targetDir, bem){
function detectDirType(targetDir, separators){
var dirName = path.basename(targetDir),
dirType,
elemRegExp = new RegExp(bem && bem.separators.elem || '__'),
modRegExp = new RegExp(bem && bem.separators.mod || '_');
elemRegExp = new RegExp(separators.elem),
modRegExp = new RegExp(separators.mod);

if (elemRegExp.test(dirName)) {
dirType = 'elemDir';
Expand All @@ -153,3 +152,7 @@ function detectDirType(targetDir, bem){

return dirType;
}

function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
Loading

0 comments on commit 9f78e60

Please sign in to comment.