cpx
cpx copied to clipboard
Any way to ignore/exclude a file or folder?
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.
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.
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?
Try to use rsync --exclude
to copy it is way simpler to use. look at man rsync
.
Same here, I want to copy everything from ./src
except ./src/**/*.test.ts
and except ./src/test/**/*
. How do I do that?..
Try with cpx "src/!(test)/**/*.!(test.ts)" dist
, untested.
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
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?
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
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: https://github.com/isaacs/node-glob/issues/180
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.
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?
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.
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?
I need to do the same, just exlude a 1 or 2 folders inside the src directory.. Looks there is not proper answer yet..
In the end I solved it like this, it's complex, but it works for me:
- 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/')\"
- 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/\"
- 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. :)
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 .
.