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

Any way to ignore/exclude a file or folder? #24

Open
thardy-pd opened this issue Dec 16, 2016 · 15 comments
Open

Any way to ignore/exclude a file or folder? #24

thardy-pd opened this issue Dec 16, 2016 · 15 comments

Comments

@thardy-pd
Copy link

I'm looking for a way to ignore a file or folder during the copy. For instance, if I want to copy all js files in a folder except a particular one. I haven't seen any examples or syntax that uses the ! option that minimatch and most other glob matchers use.

@bruce965
Copy link

bruce965 commented Mar 9, 2017

cpx "src/**/*.!(ts|less)" dist ⇒ Everything in src except *.ts and *.less files.
cpx "src/**/!(banana.txt)" dist ⇒ Everything in src except banana.txt.

!(pattern|pattern|pattern) Matches anything that does not match any of the patterns provided.
https://github.com/isaacs/node-glob#glob-primer

Other tricks might be necessary for particular situations; unfortunately no better example comes to my mind at the moment. Still these two examples could be useful in the ReadMe; the link above would be enough, though.

And I agree with you: it took some effort to understand how to to this thing the first time.

@sahiljain112
Copy link

In my src directory I have a folder which I don't want to copy. I'm unable to exclude it with the ! option. Can you help me out with that?

@EmTee70
Copy link

EmTee70 commented Aug 31, 2018

Try to use rsync --exclude to copy it is way simpler to use. look at man rsync.

@kirillgroshkov
Copy link

Same here, I want to copy everything from ./src except ./src/**/*.test.ts and except ./src/test/**/*. How do I do that?..

@bruce965
Copy link

bruce965 commented Nov 4, 2018

Try with cpx "src/!(test)/**/*.!(test.ts)" dist, untested.

@kirillgroshkov
Copy link

Nothing that I tried worked, so I actually found and switched to https://github.com/sindresorhus/cpy, since it allows to pass more than one glob. cpx served me well for couple of years

@kianmeng-chubb
Copy link

i am facing the same issue too , trying to copy everything except 1 folder and 1 file but does not seems to work with cpx "src/!(test)/**/*.!(test.ts)" dist too...

anyone manage to solve it?

@MohamedLamineAllal
Copy link

MohamedLamineAllal commented Dec 23, 2018

This pattern work: "src/**/!(test)/**/*.!(test.ts)"
Here an example:

/src/**/!(els)/*.scss

/src/style/kit.scss
/src/style/els/some.scss
/src/style/els/two.scss

it will select only /src/style/kit.scss

here an image that show my testing:
https://photos.app.goo.gl/3D1JLmJHVuR4a2SRA

@emertechie
Copy link

Failed to get it to ignore a directory also. Looks like this package is missing an option to pass through the "ignore" option to underlying glob npm package. See: isaacs/node-glob#180

@zeh
Copy link

zeh commented Aug 21, 2019

For some reason, directory ignoring only works if the rest of the pattern can match a specific pattern.

Like, with a folder that is /a/b/c, **/!(b)/** does NOT ignore it (it will match), but /a/!(b)/** does (it won't match), because the /a outside makes it.. more precise? I don't know.

@beatrizsmerino
Copy link

beatrizsmerino commented Aug 14, 2020

Hi!! This not working for me.

'static/src/images/!(icons)/**/*.{jpg,JPG,jpeg,JPEG,png,svg,gif}'

This path I am using to compress the images with node script.
I want choose all folders inside images folder except the icons folder. Any idea?

image

@andrewkfiedler
Copy link

cpx "src/**/*.!(ts|less)" dist

Finally hit a situation where this bit me today, so I thought I'd post the solution for more complicated scenarios.

The above works for files like 'src/main/webapp/blah.ts' but does not for 'src/main/webapp/blah.extra.ts' due to the extra '.' character.

I ended up using this matcher which is more general:

cpx "src/**/*.!(*ts|*less)" dist

The key difference is just putting a wildcard in that parens before the endings you're trying to exclude, that way multiple '.' characters won't accidentally get included.

@robto05
Copy link

robto05 commented Jan 29, 2021

Hi!! This not working for me.

'static/src/images/!(icons)/**/*.{jpg,JPG,jpeg,JPEG,png,svg,gif}'

This path I am using to compress the images with node script.
I want choose all folders inside images folder except the icons folder. Any idea?

image

I need to do the same, just exlude a 1 or 2 folders inside the src directory.. Looks there is not proper answer yet..

@beatrizsmerino
Copy link

beatrizsmerino commented Feb 12, 2021

In the end I solved it like this, it's complex, but it works for me:

  1. I have in the file 'compress-images.js' the function 'compressImages', it has 2 parameters for the paths of the 'input' and 'output'. From the 'package.json' file I call that function to compress all images. node -e \"require('./config/bin/compress-images.js').compressImages('./src/images/**/', './dist/images/')\"
  2. I have another file 'check-folder-remove.sh' that deletes a folder if it finds it. The I remove the folders I wanted to exclude. sh \"./config/bin/check-folder-remove.sh\" \"./dist/images/icons/\"
  3. And again I re-compress the folders that I need to export to a different location. node -e \"require('./config/bin/compress-images.js').compressImages('./src/images/icons/', './dist/images/icons/svg/')\"

package.json

"scripts": {
"min:img": "node -e \"require('./config/bin/compress-images.js').compressImages('./src/images/**/', './dist/images/')\" && sh \"./config/bin/check-folder-remove.sh\" \"./dist/images/icons/\" && node -e \"require('./config/bin/compress-images.js').compressImages('./src/images/icons/', './dist/images/icons/svg/')\"
}

check-folder-remove.sh

#!/usr/bin/env bash

# $1 -> directory path
[ -d "$1" ] && rm -r "$1"; echo "Removed $1" || echo "Not exist $1"

compress-images.js

/**
 * @constant compressImages
 * @type NPM Package
 * @requires compress-images npm run compress-images --save-dev
 * @see {@link https://www.npmjs.com/package/compress-images}
 */
const { compress } = require('compress-images/promise');


/**
 * @function compressImages
 * @description Minify size images compression with extension: jpg/jpeg, svg, png, gif.
 * @param {string} inputPath Path of the images to compress
 * @param {string} outputPath Path of the folder to save the images compressed
 * @requires compress-images - NPM Package compress-images/promise
 * @see {@link https://github.com/semiromid/compress-images}
 */
module.exports.compressImages = (inputPath, outputPath) => {
	const processImages = async(onProgress) => {
		const result = await compress({
			source: `${inputPath}*.{jpg,JPG,jpeg,JPEG,png,svg,gif}`,
			destination: outputPath,
			onProgress,
			enginesSetup: {
				jpg: {
					engine: 'mozjpeg',
					command: [
						'-quality',
						'60'
					],
				},
				png: {
					engine: 'pngquant',
					command: ['--quality=20-50'],
				},
				svg: {
					engine: 'svgo',
					command: ['--multipass'],
				},
				gif: {
					engine: 'gifsicle',
					command: [
						'--colors',
						'64',
						'--use-col=web'
					],
				},
			},
		});

		const { statistics, errors } = result;

		/*
		 * Statistics - all processed images list
		 * errors - all errors happened list
		 */
	};

	processImages((error, statistic, completed) => {
		console.info("-------------");
		if (error) {
			console.info(`Error happen while processing file: ${error}`);

			return;
		}
		if (completed) {
			console.info(`Sucefully processed file: ${completed}`);
		}
		console.info(statistic);
		console.info('-------------');
	});
}

I haven't found a better way, I hope this will help someone. :)

@jdgregson
Copy link

Just leaving a note here in case it helps someone in the future as it's the main result that comes up in Google.

My issue was that I wanted to copy everything except *.ts and *.scss. The first answer accomplishes this almost... however, it doesn't copy files without extensions at all. Sometimes you need to use extensionless files such as CNAME files used to map a GitHub Pages site to a custom domain name.

So to copy all files without a specific extension, but including files that do not have an extension, the syntax should look something like this:

src/**/!(*.ts|*.scss)

Note, however, that this still doesn't copy files that start with ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests