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

feat: add ignore parameter #6

Merged
merged 2 commits into from
Sep 3, 2019
Merged
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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Merlin is an extension of [imagemin](https://www.npmjs.com/package/imagemin) that makes it _super-easy_ to automatically, efficiently compress GIF, JPG and PNG graphics.

It’s “super-easy” for two reasons:
It’s “super-easy” for two reasons:

1. Setup is simple and there’s no confusion around what needs to be done how, where, when and why. Install, run, done – forever.

Expand All @@ -18,7 +18,7 @@ Install Merlin in respective Node project:
npm i -D @sum.cumo/imagemin-merlin
```

### 2a) Set up manual optimization
### 2a) Set up manual optimization

For manual use, add the following in the `scripts` section of the project’s package.json:

Expand All @@ -36,6 +36,8 @@ There’s a `--folder` option that allows to set a particular folder for compres

`--dry` is an optional parameter to run Merlin in “dry mode.” All changed files can then be inspected under `/tmp/imagemin-merlin`.

`--ignore` is an optional parameter to let Merlin ignore the specified paths. Multiple paths has to be seperated by a comma.

### 2b) Set up automatic optimization

For automated use Merlin should then be triggered through [Git hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) or a related tool like [Husky](https://github.com/typicode/husky) (`npm i -D husky`), for example on `pre-commit`. For that, using Husky as an example, the following may be added to package.json (main level):
Expand Down Expand Up @@ -76,18 +78,17 @@ That last piece is useful since Merlin compresses losslessly, so that there’s

## What does Merlin _not_ do?

Merlin is no substitute for image fine-tuning and micro-optimization. That is really difficult to do in an automated fashion, because that type of compression requires weighing quality and performance, which is [context-dependent](https://meiert.com/en/blog/understanding-image-compression/). In its most extreme form, when maximum quality at maximum performance is required from each graphic, micro-optimization is even hard to do manually.
Merlin is no substitute for image fine-tuning and micro-optimization. That is really difficult to do in an automated fashion, because that type of compression requires weighing quality and performance, which is [context-dependent](https://meiert.com/en/blog/understanding-image-compression/). In its most extreme form, when maximum quality at maximum performance is required from each graphic, micro-optimization is even hard to do manually.

The point is: Micro-optimization still needs to be taken care of through complementary means, whether manually or through other tools (well including other packages from the [imagemin family](https://github.com/imagemin)). Merlin simply solves the problem that images are checked in or go live that are not compressed _at all_.

## What’s next?

Some new features we’re working on or think about:
Some new features we’re working on or think about:

* Give the option to configure the underlying imagemin plugins (somewhat prepared for but not completed yet).
* Observe .gitignore files when Merlin doesn’t run as `--staged`.
* Offer an `ignore` parameter to filter undesired folders and files.
* Support project structures in which the project’s .git folder is not at the same level as its package.json – at the moment, automatic mode doesn’t work in these cases.
* Support project structures in which the project’s .git folder is not at the same level as its package.json – at the moment, automatic mode doesn’t work in these cases.

Thoughts, suggestions, fixes? Please file an [issue](https://github.com/sumcumo/imagemin-merlin/issues/new) or send a pull request – thank you!

Expand Down
20 changes: 18 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ module.exports = async argv => {
rimraf.sync('/tmp/imagemin-merlin')
}

let ignorePaths = []

if(argv.ignore){
ignorePaths = argv.ignore.split(',')
}

// search for staged files
if(argv.staged){
sgf('A', async function(err, results){
Expand All @@ -24,9 +30,14 @@ module.exports = async argv => {

let didRun = false

const filteredResults = results
let filteredResults = results
.filter(result => result.filename.match(regex))

ignorePaths.forEach(ignorePath => {
filteredResults = filteredResults
.filter(result => !result.filename.match(new RegExp(ignorePath)))
})

for (let index = 0; index < filteredResults.length; index++) {
const result = filteredResults[index];
didRun = true
Expand All @@ -42,9 +53,14 @@ module.exports = async argv => {
folder = argv.folder
}

const files = find.fileSync(regex, folder)
let files = find.fileSync(regex, folder)
let didRun = false

ignorePaths.forEach(ignorePath => {
files = files
.filter(file => !file.match(new RegExp(ignorePath)))
})

for (let index = 0; index < files.length; index++) {
const file = files[index]

Expand Down