diff --git a/README.md b/README.md index 866e917..6da4d82 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/bemy.js b/bemy.js index 7fa0d20..aa9ff73 100644 --- a/bemy.js +++ b/bemy.js @@ -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 = {}; @@ -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); } @@ -214,7 +218,7 @@ function createFileFromTemplate(fileType, trg, modVal){ } //todo resolve - if (!isOwnConfig) { + if (!ownConfig) { tmpPath = path.join(__dirname, tmpPath); } @@ -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) { @@ -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, '../')); + } +} diff --git a/config.json b/bemy.json similarity index 98% rename from config.json rename to bemy.json index 0570c3f..dea4160 100644 --- a/config.json +++ b/bemy.json @@ -42,5 +42,5 @@ "deps_task": { "files": [ "css" ] }, - "output": false + "debug": false } diff --git a/package.json b/package.json index 0f6ff95..26f269e 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", @@ -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"