Get matched folder from gulp src path using glob - node.js

Consider this which checks for JS files in either of two specific locations:
gulp.src( "#(Branch|Main)/*.js" ).pipe( _do-stuff_ )
How does one get which folder was matched (Main or Branch) for the current file(s)?

Related

How to copy only folders from a directory into another

I'm using ncp plugin to copy some folders from a directory into another, the source directory contains some files as well so I only want to copy the folders in it and their content, and this is what I tried:
async function copyAssets(exportFolderName) {
const assets = glob.sync("**/", { cwd: distPath });
return Promise.all(
assets.map((asset) => {
return ncpPromise(path.join(distPath, asset), path.join(exportPath, exportFolderName), {
clobber: false,
});
})
);
}
What I'm doing here is I get the folder names inside distPath using glob.sync and then I copy each folder and it's content into exportPath + exportFolderName.
My source folder looks like this:
But then I get some weird results:
As you can see the folders were not copied, instead it was their content that was copied.
How can I solve this ?
Thanks in advance,
I just tried to play with 'glob' and 'ncp' modules, and this is my little understanding -
glob.sync('**/') Gets directory names recursively
glob.sync('*/') Gets directory names non recursively
Since you want to exclude files from source directory, but copy all directories as a whole, I think you should go with latter glob.sync
Now, playing with ncp -
ncp(source, destination) Copies all files/directories in source to destination directory
So, I am guessing, the following would work for you -
ncp(path.join(distPath, asset), path.join(exportPath, exportFolderName, asset))
This should create the source asset directory, and then put files into it.

Gulp - Copy multiple files to seperate paths

I am working on a web app that uses Node.js. In this app, I have a Gulp file. I am using Gulp 4. During my build process, I am attempting to copy multiple files to directories at once. My directory structure looks like this:
./
dest/
source/
child/
index.js
index.bak
file.js
README.md
My real directory structure is more involved. However, I am trying to copy ./source/file.js to ./dest/file.js and ./source/child/index.js to ./dest/child/index.js. Notice that I do not want to copy README.md or index.bak over to the ./dest directory. In an attempt to do this, I have the following function:
function copy() {
let files = [
'source/file.js',
'source/child/**/*.*'
];
return gulp
.src(files)
.pipe(gulp.dest('dest'))
;
}
My problem is, everything just gets copied to the dest directory. The directory structure does not get preserved. While would be fine if I could figure out how to copy files to different directories in a single task. I tried the following:
function copy() {
return gulp
.src('source/child/index.js')
.pipe(gulp.dest('dest/child'))
.src('source/file.js')
.pipe(gulp.dest('dest'))
;
}
However, that approach just generates an error that says:
TypeError: gulp.src(...).pipe(...).src is not a function
So, I'm stuck. I'm not sure how to copy multiple files to multiple directories from a single gulp task.
You need to use the base option as mentioned here ref. It will make sure your directory is copied as it is.
function copy() {
let files = [
'source/file.js',
'source/child/**/*.*'
];
return gulp
.src(files, {base: 'source/'})
.pipe(gulp.dest('dest'));
}

Prevent require(...) from looking up modules in the parent directory

The root directory of my Node project is in a directory that itself is a root of another Node project. So both folders contain package.json and node_modules. The problem is that in the inner project, sometimes I require modules not installed in this project. But Node just silently finds them in the parent project's node_modules which leads to annoying surprises. Can I somehow prevent it from doing so? I'd like not to change the directory structure of the projects unless it's the only solution.
Node tries to resolve current module path name and concatenates node_modules to each of it's parent directory. [Source].
You can override this method at the top of your project module and add some logic to exclude the parent directories from the result path array.
//app.js <-- parent project module, should be added at the top
var Module = require('module').Module;
var nodeModulePaths= Module._nodeModulePaths; //backup the original method
Module._nodeModulePaths = function(from) {
var paths = nodeModulePaths.call(this, from); // call the original method
//add your logic here to exclude parent dirs, I did a simple match with current dir
paths = paths.filter(function(path){
return path.match(__dirname)
})
return paths;
};
inspired by this module

Zip Files & Folders With No Base Directory

AWS Lambda requires a zip file that produces a file when it's unzipped.
However, every node.js zip library produces a zip file that contains a base folder, containing the files Lambda needs, which breaks Lambda, resulting in a 'Cannot find module' error.
For example, if I have a index.js file and a node_modules directory in the dist folder, when I use gulp-zip, I get an added root folder when the zip file is unzipped...
gulp.src(['./dist/**/*'])
.pipe(zip('dist.zip'))
.pipe(gulp.dest('./'))
// When unzipped, this results in a "dist" folder containing index.js and node_modules
I've tried 6 node zip libraries and none have a simple way of excluding the base directory.
Any thoughts?
I've used 'node-archiver', which can zip a directory to a destination directory (which I just set as an empty string).
https://github.com/archiverjs/node-archiver#directorydirpath-destpath-data
var archiver = require('archiver');
archive = archive.directory('./directoryToZip/', '' ); //option 2 is the dest
archive.pipe( outZip);
archive.finalize();

Gulp.src does not consider base directory

I have following stucture of the project:
- _build/
- build-tools/
- gulpfile.js
- someFolder/
- excludeFolder/
- index.html
I want to copy all the files except _build and 'excludeFolder' dir to the _build/release directory.
I am using this gulp task:
gulp.src(['*',
'!_build/**/*',
'!build-tools/**/*',
'!excludeFolder/**/*'],{base:'..'})
.pipe(gulp.dest('_build/release'));
How can I command Gulp to start relative path from upper root directory, or any other directory that the gulfile.js is located?
As far as I understand the behavior your looking is cwd, not base
gulp.src([
'**',
'!_build',
'!_build/**',
'!build-tools',
'!build-tools/**',
'!excludeFolder',
'!excludeFolder/**'
],{ cwd:'..' })
.pipe(gulp.dest('_build/release'), { cwd: '..' });
base is a tricky property, which aims to say to gulp where to start copying the files based on the cwd, but that doesn't mean that you can omit the parent folder .. call on gulp.src (your case specifically).

Resources