-
Notifications
You must be signed in to change notification settings - Fork 1
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
dustyo-O
wants to merge
1
commit into
bem-tools:master
Choose a base branch
from
dustyo-O:brand-new-bem-rm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./lib/remove'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
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(); | ||
}); | ||
} | ||
}) | ||
))); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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? ;)