Skip to content

Commit

Permalink
closes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
f0rmat1k committed Apr 4, 2015
1 parent 52a8f62 commit ab7950a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ Options for autotask when it called on deps file. `files` – list of file types
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 version of webstorm you can try to use `/Applications/WebStorm.app/Contents/MacOS/webide` for `editor-open-command`.

#### `bem`
Your BEM options. If you use own `separators` you must set right `allowed-name-symbols-regexp`.
Your BEM options. If you use own `separators` you must set right `allowed-name-symbols-regexp`.

`debug` — to output various information.

[travis-url]: http://travis-ci.org/f0rmat1k/bemy
[travis-image]: http://img.shields.io/travis/f0rmat1k/bemy.svg?branch=master&style=flat
57 changes: 49 additions & 8 deletions bemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ var depsNormalize = require('deps-normalize');

var options = minimist(process.argv.slice(2)),
trgPath = options.f ? path.resolve(options.f) : process.env.PWD,
configPath = options.c ? path.resolve(options.c) : path.join(__dirname, 'config.json'),
prompt = options.p ? options.p.toString().split(/\s/) : options._,
config = JSON.parse(fs.readFileSync(configPath, 'utf-8')),
isOwnConfig = options.c,
bemInfo = require('./bem-info.js')(config);
ownConfig = options.c,
isDebug = options.debug,
config = getConfig(ownConfig);

var bem = config.bem,
if (!config) return;

if (config.debug) isDebug = true;

var bemInfo = require('./bem-info.js')(config),
bem = config.bem,
SHORTCUTS = function(){
var fileTypes = config['file-types'],
shortcuts = {};
Expand Down Expand Up @@ -124,7 +128,7 @@ function rename(nodePath, originNode){
fs.writeFileSync(newChildPath, file);
}

if (config.output === true) console.log('Renamed:\n' + newChildPath + '\n to \n' + currentChildPath);
if (isDebug) console.log('Renamed:\n' + newChildPath + '\n to \n' + currentChildPath);
} else {
rename(newChildPath, originNode);
}
Expand Down Expand Up @@ -214,7 +218,7 @@ function createFileFromTemplate(fileType, trg, modVal){
}

//todo resolve
if (!isOwnConfig) {
if (!ownConfig) {
tmpPath = path.join(__dirname, tmpPath);
}

Expand Down Expand Up @@ -332,7 +336,7 @@ function createFile(file, type, trg, modVal, cursorPos){
if (!fs.existsSync(p)) {
fs.writeFileSync(p, file);

if (config.output === true) console.log('\nCreated:\n' + p);
if (isDebug) console.log('\nCreated:\n' + p);

//if (options.g) gitAddTrg(trg, [p]);
if (options.g) {
Expand Down Expand Up @@ -400,3 +404,40 @@ function parseString(dep) {
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}

function getConfig(ownConfig){
if (ownConfig === true) {
console.error('Error. Path after -c is not specified.');
return;
}

var configPath;

if (ownConfig) {
configPath = path.resolve(ownConfig);
} else {
configPath = getConfigPath(path.dirname(trgPath)) || path.join(__dirname, 'bemy.json');
}

if (isDebug) console.log('Config path: ' + configPath);

try {
var config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
} catch(e) {
console.error('Problems with config:\n' + e);
return;
}

return config;
}

function getConfigPath(dir) {
if (dir === '/') return;

var checkPath = path.resolve(dir, 'bemy.json');
if (fs.existsSync(checkPath)) {
return checkPath;
} else {
return getConfigPath(path.resolve(dir, '../'));
}
}
2 changes: 1 addition & 1 deletion config.json → bemy.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@
"deps_task": {
"files": [ "css" ]
},
"output": false
"debug": false
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bemy",
"description": "Helper for auto-creation BEM directory and file structure",
"description": "CLI helper for auto-generation and renaming BEM structure",
"main": "bemy.js",
"author": "Anton Grischenko <[email protected]>",
"license": "MIT",
Expand All @@ -11,7 +11,7 @@
"type": "git",
"url": "https://github.com/f0rmat1k/bemy.git"
},
"version": "2.2.5",
"version": "3.0.0",
"keywords": [
"bem",
"BEM Tools"
Expand Down

0 comments on commit ab7950a

Please sign in to comment.