Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brand New BEM tools rm #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 53 additions & 76 deletions cli.js
Original file line number Diff line number Diff line change
@@ -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();
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
'use strict';

module.exports = require('./lib/remove');
50 changes: 25 additions & 25 deletions lib/remove.js
Original file line number Diff line number Diff line change
@@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like there's not enough files in commit? ;)

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();
});
}
})
)));
};