From e04e555705fedc76987f6df8270655d39d49d591 Mon Sep 17 00:00:00 2001 From: Alexandr Shleyko Date: Sun, 25 Mar 2018 16:59:55 +0300 Subject: [PATCH] Brand new bem rm --- cli.js | 129 +++++++++++++++++++++----------------------------- index.js | 1 + lib/remove.js | 50 +++++++++---------- 3 files changed, 79 insertions(+), 101 deletions(-) diff --git a/cli.js b/cli.js index 443ce90..df296a7 100644 --- a/cli.js +++ b/cli.js @@ -1,92 +1,69 @@ 'use strict'; -var EOL = require('os').EOL, - bemNaming = require('bem-naming'), - stream = require('through2'), - util = require('bem-tools-find/lib/util'), - remove = require('./lib/remove'); +var remove = require('./'); -/** - * Executes find process with given cli options and arguments - * @param {Object} opts - cli options - * @param {Object} args - cli arguments - */ -function execute(opts, args) { - var conditions = util.conditionsFromBEMItems(args.entity); - conditions.push(util.conditionsFromOptions(opts)); +function noOp() { } - remove(conditions) - .pipe(report()) - .pipe(process.stdout); -} - -/** - * Returns stream for print all removed files and their total count - * @returns {Stream} - */ -function report() { - var count = 0; - return stream.obj(function(item, enc, cb) { - this.push('removed: ' + item + EOL); - count++; - cb(); - }, function(cb) { - this.push(count + ' files were removed' + EOL); - cb(); - }); -} - -module.exports = function() { - return this - .title('BEM Tool for removing BEM entity files') - .helpful() - .completable() - .arg() - .name('entity') - .title('entity') - .val(function(value) { - if (bemNaming.validate(value)) { - return bemNaming.parse(value); - } else { - return this.reject('Passed argument is not valid BEM entity'); - } - }) - .arr() +module.exports = function () { + this + .title('Remove BEM entity').helpful() + .opt() + .name('level').short('l').long('level') + .title('level directory path') + .end() + .opt() + .name('block').short('b').long('block') + .title('block name, required') + .arr() .end() .opt() - .name('level') - .title('Name of level(s)') - .short('l') - .long('level') - .arr() + .name('elem').short('e').long('elem') + .title('element name') + .arr() .end() .opt() - .name('block') - .title('Name of block(s)') - .short('b') - .long('block') - .arr() + .name('mod').short('m').long('mod') + .title('modifier name') + .arr() .end() .opt() - .name('element') - .title('Name of element(s)') - .short('e') - .long('element') - .arr() + .name('val').short('v').long('val') + .title('modifier value') + .arr() .end() .opt() - .name('modifier') - .title('Name of modifier(s)') - .short('m') - .long('mod') - .arr() + .name('tech').short('t').long('remove-tech') + .title('remove tech') + .arr() .end() .opt() - .name('tech') - .title('Name of tech(s)') - .short('t') - .long('tech') - .arr() + .name('leaveTech').short('lt').long('leave-tech') + .title('leave tech') + .arr() .end() - .act(execute); + .opt() + .name('entities').title('Entities') + .arr() + .end() + .act(function (opts, args) { + var options = {}, + techs = opts.tech || []; + + if (opts.leaveTech) { + // we intending better to leave tech then to delete + techs = techs.filter(tech => !opts.leaveTech.includes(tech)); + } + + if (args.entities) { + return remove(args.entities, opts.level, techs, options).then(noOp); // wtf is entities + } + + opts.block && remove([{ + block: opts.block[0], + elem: opts.elem && opts.elem[0], + modName: opts.mod && opts.mod[0], + modVal: opts.val ? opts.val[0] : Boolean(opts.mod) + }], opts.level, techs, options).then(noOp); + }) + .end(); }; diff --git a/index.js b/index.js index d405960..14d45f9 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,3 @@ 'use strict'; + module.exports = require('./lib/remove'); diff --git a/lib/remove.js b/lib/remove.js index 43b56ba..c12d937 100644 --- a/lib/remove.js +++ b/lib/remove.js @@ -1,29 +1,29 @@ 'use strict'; -var fs = require('fs'), - path = require('path'), - _ = require('lodash'), - fsExtra = require('fs-extra'), - find = require('bem-tools-find'), - stream = require('through2'); -module.exports = function(conditions) { - return find(conditions).pipe(remove()); -}; +const getEntityData = require('./get-entity-data'); +const getPath = require('./get-path'); +const removeEntity = require('./remove-entity'); +const _ = require('lodash'); +const path = require('path'); +const fs = require('fs'); +const util = require('util'); +const readdir = util.promisify(fs.readdir); +const rmdir = util.promisify(fs.rmdir); + +module.exports = function remove(entities, levels, techs, options) { + const entities = getEntityData(entities, levels, techs, options); -function remove() { - var folders = []; - return stream.obj(function(item, enc, cb) { - var filePath = item.path; - folders.push(path.dirname(filePath)); - this.push(filePath); - fsExtra.remove(filePath, cb); - }, function(cb) { - _.uniq(folders) - .forEach(function(folder) { - if (fs.readdirSync(folder).length === 0) { - fsExtra.removeSync(folder); + return Promise.all(entities.map(item => + removePath(getPath(item)) + )) + .then(pathes => pathes.map(path.dirname)) + .then(_.uniq) + .then(folders => Promise.all(folders.map(folder => + readdir(folder) + .then(files => { + if (files.length === 0) { + return rmdir(folder); } - }); - cb(); - }); -} + }) + ))); +};